Truncate String Exercise

So I am having some issues with this exercise. It meets three out of the six requirements to pass. I am doing my best to not look at the solution. Here is the exercise:

Truncate a String
Truncate a string (first argument) if it is longer than the given maximum string length (second argument). Return the truncated string with a “…” ending.

It runs these tests:

I am passing the first two and the last test with my code.

Tests
Passed:truncateString(“A-tisket a-tasket A green and yellow basket”, 8) should return the string A-tisket…
Passed:truncateString(“Peter Piper picked a peck of pickled peppers”, 11) should return the string Peter Piper…
Failed:truncateString(“A-tisket a-tasket A green and yellow basket”, “A-tisket a-tasket A green and yellow basket”.length) should return the string A-tisket a-tasket A green and yellow basket.
Failed:truncateString(“A-tisket a-tasket A green and yellow basket”, “A-tisket a-tasket A green and yellow basket”.length + 2) should return the string A-tisket a-tasket A green and yellow basket.
Failed:truncateString(“A-”, 1) should return the string A…
Passed:truncateString(“Absolutely Longer”, 2) should return the string Ab…

Here is my code:

function truncateString(str, num) {
  let newStr = '';
  for (let i = 0; i < str.length; i++) {
  if (str[i].length <  num) {
    newStr = str.slice(str[i], num) + "...";
  }
  }
  return newStr;
  
}

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

console.log(truncateString("Peter Piper picked a peck of pickled peppers", 11));

Can anyone help me figure out where I am going wrong?

What is the loop doing for you?

I tried writing it without the loop at first.

function truncateString(str, num) {
  let newStr = '';
  if (str.length <  num) {
    newStr = str.slice(str.length, num) + "...";
  }
  return newStr;
  
}

But the console log was blank showing no results. I wrote the for loop just as an experiment and it was showing better progress.

I’d ditch the loop.

What should happen if the string is not too long?

What should happen if the string is too long?

ha great questions. I have no idea. I’ve been learning Javascript for about a month. Before this the last time I wrote code for anything was in college when I wrote actionScript3 to make interactive flash animations.

This isn’t a JavaScript question. Its a question about the instructions. What do the instructions say?

Yeah you were right. After reading the instructions again it made more sense what I was doing. Then I came up wit this.

function truncateString(str, num) {
  let newStr = '';
  if (str.length > num) {
    newStr = str.slice(str, num) + "...";
  } else {
    return str;
  }
  return newStr;

}

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

console.log(truncateString("Peter Piper picked a peck of pickled peppers", 11));

It passed all the tests.

Huzzah!

1 Like

Thank you. I appreciate you challenging me like that.

1 Like