Question 1: Why is my second solution not working?
//console.log(str.slice(0,num)+"...";) // works
//return str.slice(0,num)+"..."; // works
console.log("".concat(str.slice(0,num),"...")); // doesn't work
return "".concat(str.slice(0,num),"..."); // doesn't work
Question 2: Easy way not always best way
I use a lot of methods in my solutions. And that makes me feel a bit like I’m cheating. Shouldn’t my solution look like the “advanced solution” anymore? Maybe I’ll learn a lot more if I use fewer methods, because now I’m not building a fundamental basis?
This is de advanced solution:
function truncateString(str, num) {
if (str.length <= num) {
return str;
} else {
return str.slice(0, num > 3 ? num - 3 : num) + '...';
}
}