Truncate a String Issue

Tell us what’s happening:

Hi All, I have console logged each truncation for this challenge with the below method and the following prints:

A-tisket…
Peter Piper…
A…
Ab…

This looks to match the answers required in the test panel to pass the challenge, but but when I run the tests it doesn’t pass on these 4. I just wondered where I am going wrong, the method is different to the given solution but to me the outcome looks the same in the console as the test panel. Even when I have copied and pasted the actual solution from the hints it doesn’t work for the first 2 answers.

The solution in the hints, with (num - 3) produces the following which isn’t the specified number of characters plus ‘…’

A-tis…
Peter Pi…

Thanks for helping
Alphie

Your code so far


function truncateString(str, num) {
  if (str.length > num ) {
    let newstring = str.slice(0, num).concat('...');
    console.log(newstring);
    return
    newstring;
  } else {
    return str;
  }
}

truncateString("A-tisket a-tasket A green and yellow basket", 8);
truncateString("Peter Piper picked a peck of pickled peppers", 11);
truncateString("A-", 1);
truncateString("Absolutely Longer", 2);

Your browser information:

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

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

Hi.
I checked your code on FCC and it’s okay.
Try to reload the page and run the tests again.

Tibor

Thanks Tibor, I’ve just tried refreshing, reset the test to rewrite the code, logged out and back in again and tried to submit from another browser but it still shows the same.

Why do you have newstring on a separate line after your return? This causes your function to return undefined.

Hi Randell, that is the problem didn’t even think that would cause the issue. Won’t forget that one in anytime soon. Thanks for the help