Truncate a String - Failing

Tell us what’s happening:
Hello - I can’t see why below is failing 2 test cases. Can anyone see it? thx!!

Your code so far


function truncateString(str, num) {
  // Clear out that junk in your trunk

  var re = new RegExp('^.{0,' + num + '}');
  console.log(re);

  
  if (str.length > num) {
    console.log(str.match(re)+'...');
    return str.match(re)+'...';

  }
  else
  console.log(str.match(re));
  return str.match(re);

  
//  return str;
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36.

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

Hi
You just need to return str in else part.
You don’t need to match it against regex.
That should work

str.match(re) is an array. In the other block you force it to a string when you add the dots, but in the elsenothing change it to a string

1 Like

THanks!! That worked. Console.log doesn’t show me that !! :frowning:

I used this to check: http://pythontutor.com/javascript.html#mode=edit