I’ve been doing a couple of exercises that worked on the console but don’t work for solving the excercise.
Thank you.
This is the code:
Your code so far
function truncateString(str, num) {
let str1 = str.split('')
let result = [];
for(let i=0; i<num; i++) {
result.push(str1[i]);
}
let f ="";
f = result.join('');
console.log(f+'...');
}
truncateString("A-tisket a-tasket A green and yellow basket", 8);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36.
hey
You don’t need to split the given string. You should check if it is longer than the num argument and if so, then you need to take the first n chars from str (you can use String’s .splice() method here) and append to it '...'.
If str is not longer than num, then simply return str.