Tell us what’s happening:
I have been having hard luck with algo. For this one, I had to check the hint to do it as I couldn’t understand the if statement which says *
‘if it is longer than the given maximum string length (second argument).’
I thought because truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length) has .length which isn’t number so my below code with make sense.
*
**Your code so far**
if(isNaN(num)){ //is not a number?
console.log(num)
return str
}else{
let newWord = str.slice(0, num)
str = newWord+"..."
return str;
}
truncateString("A-tisket a-tasket A green and yellow basket", 8);
**Your browser information:**
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36.
Anyways, the challenge is asking you to cut off the string at num characters. You should return the string if it has num or fewer characters and return the first portion of the string with … added if it’s too long.
var str = "Hello";
var strLength = str.length;
console.log(typeof strLength); // number
console.log(strLength); // 5
also, isNaN check if the value is NaN, which is a specific value
Also, remember the instructions
Truncate a string (first argument) if it is longer than the given maximum string length (second argument). Return the truncated string with a ... ending.
what happens in your code is always the else statement, so you are always adding ... at the end of the string