I don’t need help for this challenge, per se.
I came up with an alternate solution to the provided answers (it seemed the most logical way to do it to me), and I am just wondering if there are any drawbacks to the way I did it?
Thanks.
My solution:
function truncateString(str, num) {
var truncStr = str.slice(0, num);
if (num < str.length) {
truncStr += "...";
}
return truncStr;
}
truncateString("A-tisket a-tasket A green and yellow basket", 8);
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/truncate-a-string
I think I know what you mean, but two of the requirements for the challenge were:
truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length)
should return “A-tisket a-tasket A green and yellow basket”.
truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length + 2)
should return “A-tisket a-tasket A green and yellow basket”.
Only truncated strings were to have the dots, so I had to add the extra code to ‘remove them’ from those two possibilities.
Okay, I see what you’re saying. I guess I sort of approached it backwards.
Thanks. I appreciate the help – I don’t want to be picking up bad habits so I thought I’d check.