Truncate A string(Problem)

function truncateString(str, num) {
  // Clear out that junk in your trunk
  let Str = str;
  let len = num;
  let sliced_major = Str.slice(len,);
  //console.log(sliced_major);
  let sliced_minor = Str.slice(0,len);
  //console.log(sliced_minor);
  if(Str.length > len){
    Str = `${sliced_minor}...`;
    
  }
  else{
    return false;
  }
  return Str;
}

console.log(truncateString("A-tisket a-tasket A green and yellow basket", 8));

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

Almost made it but stuck at these two tests.

Hello @topcoder!

The problem here is that they are asking you to truncate something larger than your string. Let’s say, if you have “Hello”, it’s length is 5, but the last character position(in this case “o”) is 4 (because you start at 0).

You should consider cases when they ask you to truncate outside of the string bounds.

Hope this helps!

so I have to test the condition if its more than 5 in your example.

The tests say you should return the entire string if being asked to truncate more than the string length, that’s it.

It worked,thanks for your time

1 Like