In the "Truncate a string" excersie. Why does trunSrt = str; doesn't work

function truncateString(str, num) {
 var retStr = [];
 var trunStr = [];
 var len = str.length; 
  if ( num > 3 && num < len ){
  retStr = str.slice(0, num-3);
  trunStr =  retStr .concat("..."); 
    return trunStr;
}else if ( num < 3){
  retStr = str.slice(0, num);
  trunStr =  retStr .concat("..."); 
  return trunStr;
}else{
   **//trunSrt = str;  return trunSrt;** I struggle 45 min to release I just have to return the str instead of assigning the Srt trunSrt and then returning the same.
return str;
  }
}
truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length);

I’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

markdown_Forums

1 Like

@VickkyMevawala,
I don’t really understand from what you wrote where you are having a problem.
From reading your code, ti seems that if the if case is true, you are returning a string, since you are concatenating retStr and in the other case, you are returning an empty array(since that is what you have initialized trunStr with).

I think that is the root of your problem. Make sure to read the instructions of the exercise carefully so you understand what you need to do for each case.

I’ve edited your post again for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

markdown_Forums

Thank you Ben, Will take care of the same from next time onwards.

Thanks, Randell, Will take care of the same next time onwards.

Hey Tomer,
I found the problem, I was assigning the value to var trunSrt but missed to put a return below it.

1 Like