Implement the Truncate String Algorithm - Implement the Truncate a String Algorithm

Tell us what’s happening:

I don’t know what the issue could be.. or I the slice method is not what I should use?

Your code so far

function truncateString(str, num){
  if(str.length > num){
    let visibleString = str.slice(0, num)
    let nonVisibleString = ".".repeat(str.length - num)
    let truncatedString = visibleString  nonVisibleString
    return truncatedString
  } else {
    return str
  }
}

console.log(truncateString("samuel", 1))

Your browser information:

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

Challenge Information:

Implement the Truncate String Algorithm - Implement the Truncate a String Algorithm

Welcome to the forum @morgansamuel960

There is a message in the console.

SyntaxError: unknown: Missing semicolon. (5:39)

  3 |     let visibleString = str.slice(0, num)
  4 |     let nonVisibleString = ".".repeat(str.length - num)
> 5 |     let truncatedString = visibleString  nonVisibleString
    |                                        ^
  6 |     return truncatedString
  7 |   } else {
  8 |     return str

See if it can help you debug.

Happy coding

1 Like

Hi @morgansamuel960! You have a typo here:

Also, the instructions ask you to add "...", not a number of dots equal to the string length, Hope that helps :blush:

1 Like

nice catch, thanks!!

1 Like

you saved me from future errors!!:sweat_smile:

1 Like

Hi @morgansamuel960!
the instruction says that only 3 dots should be added so the repeat method should repeat the “.“ only three times when the length of the str is greater than the num to replace masked part of the string
”.”.repeat(3)

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge. How to Help Someone with Their Code Using the Socratic Method

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

1 Like