Truncate a String: I didn't understand the if condition

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?
  return str
  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.

Challenge: Truncate a String

Link to the challenge:

What happened to the function definition?

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.

it’s a number

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

I just console.log(typeof num) and it said number, but in one of test the params is .length is that converted to number internally?

this is the parameter
the length of a string is a number

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.