Truncate a String helpp

is my code right?there are two challenges i cannot clear

Your code so far


function truncateString(str, num) {
  // Clear out that junk in your trunk
  var spl = str.split('');
  var newarr = [];
  for (let i = 0;i < num;i++)
  {
    newarr.push(spl[i]);
    
  }
  var newstr =newarr.join('') + '...' 
  return newstr;
}

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

Your browser information:

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

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

What do the failing tests say?

truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length) should return "A-tisket a-tasket A green and yellow basket".

truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length + 2) should return "A-tisket a-tasket A green and yellow basket".

You should not add ... to the string if you did not truncate it, but you always add it before returning.

1 Like

okay i passed it but that required a couple of if-else statements in the end,hope that was what was required