Truncate a String July 2018

Truncate a String July 2018
0.0 0

#1

Tell us what’s happening:
Hey, I struggled with this one as I wasn’t sure what It was asking me to do, none the less all I could find was solutions on the web. Having put some of those in the console it still doesn’t pass. If there a problem with it? or has it been updated and requires additional thinking? Btw the solution code was from the “Get a Hint” button

Your code so far

function truncateString(str, num) {
// Clear out that junk in your trunk
if (str.length > num && num > 3) {
return str.slice(0, (num - 3)) + ‘…’;
} else if (str.length > num && num <= 3) {
return str.slice(0, num) + ‘…’;
} else {
return str;
}

}

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


function truncateString(str, num) {
  // Clear out that junk in your trunk
  if (str.length > num && num > 3) {
    return str.slice(0, (num - 3)) + '...';
  } else if (str.length > num && num <= 3) {
    return str.slice(0, num) + '...';
  } else {
    return str;
  }

}

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

Your browser information:

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

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


#2

If the length of the string is less than the number, you set the length of the string equal to the number given excluding the dots. so the first test case would return

A-tisket…

instead of your

A-tis…


#3

Thanks solved it. Id wish these set give abit more detail as when you start googling certain logic to complete it, you find all different solutions that people have done.

function truncateString(str, num) {
// Clear out that junk in your trunk
if (str.length > num) {
return str.slice(0, num) + ‘…’;
} else if (str.length > num && num <= 3) {
return str.slice(0, num) + ‘…’;
} else {
return str;
}

}

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

Cheers again:)