Truncate String Excersize!

Tell us what’s happening:

Describe your issue in detail here.
I have no idea why this is not working. From testing with console.log(), the function prints out all the test cases perfectly. What am I doing wrong?

Your code so far

function truncateString(str, num) {
  let finalString = " ";
  if(str.length > num){
    for(let i = 0; i< num; i++){
    finalString = finalString + str[i];
    }
    console.log(finalString + "...");
    return finalString + "...";
  } 
  else if(str.length == num){
    console.log(str);
    return str;

  }
  else {
    console.log(str);
    return str;
  }
   
 }

Please post a link to the challenge.

I have fixed your formatting below:

function truncateString(str, num) {
  let finalString = " "; // Why a space?
  if (str.length > num) {
    for (let i = 0; i < num; i++) {
      finalString = finalString + str[i];
    }
    console.log(finalString + "...");
    return finalString + "...";
  } else if (str.length == num) {
    console.log(str);
    return str;
  } else {
    console.log(str);
    return str;
  }
 }

Thankyou! I’ve edited the post with a link to the challenge. Apologies, I’m new to the forums and freecodecamp.
Heres the link here too https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/truncate-a-string .

Did you see this comment. I think that is the only issue with your code passing

1 Like

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