Truncate a String problem output

Help, im a bit stuck in this problem, i don’t know why the for loop surpasses the max number

  **Your code so far**

function truncateString(str, num) {
let text = str.split("");
let result = [];


if (str.length >= num){
for (let i = 0; i < num; i++){
  result.push(text[i])
}
result = result.join("") + "...";
console.log(result);
}else{
result = str;
}

//console.log(result);
return result;
}

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

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

  **Your browser information:**

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

Challenge: Truncate a String

Link to the challenge:

If the number is equal to the length of the string, do you want to be putting ‘…’ at the end? You should only be running the code inside this block if num is greater than the length of the string.

1 Like

ahh i missed that one, thank you for pointing it out, it’s driving me crazy for an hour now.

No problem!

And sorry, I said this wrong. I meant ‘if the length of the string is greater than num’. :joy:

yeah I got the logic already right yesterday, on that code you mentioned and it passed the test. :ok_hand:

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