Basic Algorithm Scripting - Truncate a String

Tell us what’s happening:
I’ve passed this exercise after playing around with it for awhile, but I’m not seeing quite where I tripped up in my original attempt.

With this passed into the function:

truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length)

I could not pass with this code:

function truncateString(str, num) {
  if (num > str.length) {
    return str;
  } else {
    return str.slice(0, num) + "...";
  }
}

but this worked:

function truncateString(str, num) {
  if (str.length > num) {
    return str.slice(0, num) + "...";
  } else {
    return str;
  }

}

Any hints as to where my logic was faulty?

Thank you!

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36

Challenge: Basic Algorithm Scripting - Truncate a String

Link to the challenge:

I had this post up for 2 minutes and realized exactly where my pain point was. Ah, progress!

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.