Rosetta Code Challenges - Word wrap

Tell us what’s happening:

Hi

My code seems to be returning the correct number of lines and not going over the limit in the console, but its not passing the tests which state should return x lines
any pointers would be appreciated

Your code so far

function wrap(text, limit, wrappedText = '') {
 
  if (text.length <= limit) {
    wrappedText += text
    return wrappedText
  }
  else {
        const lastIndex = text.lastIndexOf(' ', limit)
       
        wrappedText += `${text.slice(0, lastIndex)}\n`
      
        return wrap(text.slice(lastIndex+1), limit, wrappedText)
  }
}

const text = 'Wrap text using a more sophisticated algorithm such as the Knuth and Plass TeX algorithm. If your language provides this, you get easy extra credit, but you must reference documentation indicating that the algorithm is something better than a simple minimum length algorithm.'

console.log(wrap(text, 15))

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36

Challenge Information:

Rosetta Code Challenges - Word wrap

Welcome back to the forum @gazdean

This part of the code is not quite right.

You cannot combine template literal syntax with a new line character that way.

Happy coding

thanks for the reply, i will take another look at it