Everything works as expected. Does not pass the test. Why?

Tell us what’s happening:

This is the code i have.
Everything works as expected.

Why does it not pass the test?


function titleCase(str) {
var wordArr = str.toLowerCase().split(' ')
var result = '';

wordArr.forEach(word => {
  result += word.charAt(0).toUpperCase() + word.slice(1) + ' '
})

return result
}

titleCase("i'm a little tea pot"); // I'm A Little Tea Pot
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:85.0) Gecko/20100101 Firefox/85.0.

Challenge: Title Case a Sentence

Link to the challenge:

Are you supposed to be addding a space to the end of all your strings? Because your result will always end in a space.

Just figured :slight_smile:

Changed the code to:

function titleCase(str) {
  var wordArr = str.toLowerCase().split(' ')
  var result = [];
  
  wordArr.forEach(word => {
    result.push(word.charAt(0).toUpperCase() + word.slice(1))
  })
  return result.join(' ')
}

Thank you for answering!

Good job on working through the problem! Happy coding.

1 Like