Hello, im trying to make a small name summary function depending on the size of the elements container, here's what I have;
function shorten_text(str, size){
size = size.match( /[0-9]*/ );
var endValue = Math.floor( Number(size) / 10 );
var number;
var newStr;
for ( number = 0; number <= endValue; number++ ) {
if( str[number].length != 0 ) {
newStr += str[number];
}
}
return newStr + '...';
}
shorten_text('Phil Jackson', '94px');
// output should be 'Phil Jack...'
What I seem to get is undefinedundef... can anyone see where I am going wrong?
EDIT:
revised code based on comments below for anyone who is googling for such function:
function shorten_text(str, size){
size = parseInt(size);
var endValue = Math.floor(size / 10);
if( str.length > endValue ) {
return str.substring(0, endValue) + '...';
}else{
return str;
}
}
SCREEN SHOT: