Truncate a string - algorithm

So, trying to solve this algorithm challenge as many ways as possible. I know I can simplify this and just use .slice() but I’m trying to get creative and use as much of what I’ve learned so far as possible, even if it’s not necessarily the best way. Here’s one that ALMOST solves all the test cases, but it fails the fourth check:

  • truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length + 2) should return the string A-tisket a-tasket A green and yellow basket .

function truncateString(str, num) {
if (str.length === num) {
return str;
} else {
let newStrArray = [ ];
for (var i = 0; i < num; i++) {
return newStrArray.join("") + “…”;

What am I missing? I know there are other methods I can use to pass all the challenges, but how would I get this one to work? (or will it not?)

Challenge: Truncate a String

Link to the challenge:

In that test case the if is functionally

if(str.length === (str.length +2))

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