Truncate a String wont work

Tell us what’s happening:
I keep running this code and multiple others which I got from other sites but its not checking the first two requirements

Your code so far


function truncateString(str, num) {
  if (str.length <= num) {
    return str;
  } else {
    return str.slice(0, num > 3 ? num - 3 : num) + '...';
  }
}

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

Your browser information:

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

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

Is this code that you wrote yourself or code that you found online? If I remember correctly, the challenge used to be different and this solution would have solved the old requirements, but not the challenge as it is currently written.

I got it online :frowning:

If you share your attempted solutions, I can help you figure out how to get the rest of the way through.

I tried to do it with this one but it didnt work so I went online


function truncateString(str, num) {

var opStr = function(str, num1){
var res = ‘’;
res = str.slice(0, num1) + ‘…’;
return res;
};

if (num > 3 && str.length > num ){

return opStr(str, num-3);

}else if( num <= 3 && num >0 && str.length > num) {

return opStr(str,num);

}else{

return str;

}

}
truncateString(“A-tisket a-tasket A green and yellow basket”, 11);

Why are you subtracting 3 from num?

What Can I do to make it work :sob: :

Hey, try using your own code for these challenges. What’s the point of us fixing somebody else’s code for you? At least give us your own code so you’ll learn something from it.

This challenge is very easy, took me a few minutes. Break down the steps you need to take and follow them.

This is my solution, hope it helps:

function truncateString(str, num) {
var newString;
var newNum;
if (str.length > num) {
newNum = str.length - num;
newString = str.slice(0, -newNum) + “…”;
} else {
return str;
}
return newString;
}

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

I used substring() Method and worked fine too.


function truncateString(str, num) {
  let myStr = "";
  if(str.length > num){
    return myStr = str.substring(0, num) + "...";
  } else {
    return str;
  }
}
truncateString("A-tisket a-tasket A green and yellow basket", 8)

it can be simplified in one line:


function truncateString(str, num) {
  return str.length > num ? str.substring(0, num) + "..." : str;
}

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

@WheySkills and @KernellKillo

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Thank you.