Basic Algorithm Scripting: Truncate a String"

Hello.

I am struggling with " Basic Algorithm Scripting: Truncate a String". I recieve proper results, but
I can’t pass the challenge. Am I missing something?


function truncateString(str, num) {


let arr=str.substring(0,num);
arr+="...";
return arr;
}

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

Your browser information:

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

Challenge: Truncate a String

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

what should happen if str is "Hello" and num is 24?
what should be the returned value?

I know what your function does in that case, but is that what the challenge description tell you to return in that case?

also, don’t return a console.log
if you write return console.log() then the output of your function is undefined
you can put the function call in a console.log if you want
console.log(truncateString("Hello", 25))

Yeah, I’m aware of that. In original post I didn’t included console.log(), I did it in reply to you to show what is a result of arguments (“Hello”, 25). My original code doesn’t include console.log(), as you can see at the beginning of thread :slight_smile:

Challenge tells me to return the truncated string with a ... ending, even if i change code like this I can’t pass the challenge:

function truncateString(str, num) {

str=str.substring(0,num);

str+="...";

return str;

}

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

do you always have to truncate the string?

Truncate a string (first argument) if it is longer than the given maximum string length (second argument). Return the truncated string with a ... ending.

also note this test case:

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

there are not the three dots

Thank you very much. Now i know what was missing. :slight_smile: