Kindly help me why test cases are failing while out put is same as given in the question?

Tell us what’s happening:
Describe your issue in detail here.

  **Your code so far**

function truncateString(str, num) {
if(str.length===num){
return str;
}
else{
 let newl=str.length-num;
 return (str.substring(0,num)+"....")
}
}

console.log(truncateString("Peter Piper picked a peck of pickled peppers", 11));
  **Your browser information:**

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

Challenge: Truncate a String

Link to the challenge:

Hello there.

Do you have a question?

If so, please edit your post to include it in the Tell us what’s happening section.

Learning to describe problems is an important part of learning how to code.

Also, the more information you give us, the more likely we are to be able to help.

1 Like

Hi @i_curious !

You have two issues.

No.1:
Carefully reread the directions here
Return the truncated string with a ... ending.

Then take a look at what you wrote in the else clause.

No.2:
You need to look carefully at this test case

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

You should notice that the string length is shorter than the num argument.
The challenge wants you to only truncate the string if the str length is greater than the num argument.

For this test case, your code should return the string.
Not the truncated string.

You need to modify your code to account for this test case.

Hope that helps!

Sidenote: It doesn’t look like you are using this here so you can probably just delete it

I know that asking for help on the forum can be hard but describing the problem is an important skill for developers.

Please try to keep that in mind for future posts.

Thanks! :grinning:

Thanks @jwilkins.oboe , it’s working now.

1 Like

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