# Truncate a String July 2018

Truncate a String July 2018
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

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;
}

}

``````
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;
}

}

``````

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`.

#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…

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;
}

}