Submission Error

Tell us what’s happening:

On the console i get the right answer but it will not pass it when i run the code

Your code so far


function titleCase(str) {
str = str.toLowerCase(); 
let word = str.split(" ");
let res = "";
for (var i = 0; i < word.length; i++)
{
  var temp = [];
  temp = word[i].split("");
  temp[0] = temp[0].toUpperCase();
  temp = temp.join("");
  res += temp + " ";

}
//console.log(word);
console.log(res);
return res;
}

titleCase("sHoRt AnD sToUt");

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36.

Challenge: Title Case a Sentence

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/title-case-a-sentence

The last line of your loop, you add a space. The string you return is off a little.

So your I'm A Little Teapot has a space character at the end, not matching the test.

2 Likes

Welcome, Deadmaus.

If you open your devtools, you can see that you are returning 6 iterations of that string.

If you work from there, I am sure you can fix it. Hope this helps.

2 Likes

I fixed the problem. Thank you @snowmonkey

function titleCase(str) {
  str = str.toLowerCase(); 
  let word = str.split(" ");
  let res = [];
  for (var i = 0; i < word.length; i++)
  {
    var temp = [];
    temp = word[i].split("");
    temp[0] = temp[0].toUpperCase();
    temp = temp.join("");
    res.push(temp);

  }
  //console.log(word);
  res = res.join(" ");
  console.log(res);
  return res;
}

titleCase("sHoRt AnD sToUt");
1 Like