Truncate a String problems with solving

Hey,

Can somebody help me with this?

I’ve been doing a couple of exercises that worked on the console but don’t work for solving the excercise.

Thank you.

This is the code:

Your code so far


function truncateString(str, num) {
  let str1 = str.split('')
  let result = [];

  for(let i=0; i<num; i++) {
    result.push(str1[i]);
  }
  let f ="";
  f = result.join('');
  console.log(f+'...');

}

truncateString("A-tisket a-tasket A green and yellow basket", 8);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/truncate-a-string

your function is not returning anything
returning something with return is different from using console.log()

hey :wave:
You don’t need to split the given string. You should check if it is longer than the num argument and if so, then you need to take the first n chars from str (you can use String’s .splice() method here) and append to it '...'.
If str is not longer than num, then simply return str.

Thank you I understand that point, however, why my code still works and doesn’t solve the problem? I used console.log() for debugging.

Do you have a return statement in the code you are using to try to pass the challenge?

You don’t append '...' to strings which are longer than n. You can add something like

if (str.length > 8) f += '...';

And at the end you need to add the return statement to your function, something like:

return f;