Basic Algorithm Scripting: Truncate a String Pls help with solution

Basic Algorithm Scripting: Truncate a String Pls help with solution
0

#1

I do not understand what goes wrong(
my solution below
my code don’t work with 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". and I do not know why

function truncateString(str, num) {
  // Clear out that junk in your trunk
  // let regex = new RegExp('^\\w{num}\/ig');
  let regex = new RegExp("^.{" + num + "}");
  let res = str.match(regex);
  if (num < str.length) {
    return res + '...';
  } else {
    return res + '';
  }
  
}

#2

Why length plus two?
The whole string is only having “length” characters in it.
Does your code work with the length? Does it work with length plus one?


#3

hi,
this is my solution. i hope is right…

function truncateString(str, num) {
// Clear out that junk in your trunk

let string="";
if (num>=str.length){
return str;
}
else { string= str.substring(0,num) }

return string+"…";

}