Title Case a Sentences

Title Case a Sentences
0

#1

**Good day fellow campers

I tested code below in the console and it shows the desired result but only the first two test passed while the below did not:

titleCase(“sHoRt AnD sToUt”) should return Short And Stout.
titleCase(“HERE IS MY HANDLE HERE IS MY SPOUT”) should return Here Is My Handle Here Is My Spout.**
although on the console , i got the correct result. please what could be wrong with my code this time.

Your code so far


function titleCase(str) {
  let lowerCaseStr= str.toLowerCase();
  var String = str.split(" ");
  var strArr = [];
  for (var i = 0; i < String.length; i++) {
    strArr.push(String[i].toLowerCase().charAt(0).toUpperCase() + String[i].slice(1));
  }
  
  return strArr.join(" ");
}

titleCase("I'm a little tea pot");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36.

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


#2

String i believe is a Class? You might want to pick a variable name that has a lesser change chance of conflicting with the class name. temp or something else would be a good idea. May not affect your result, but you’re asking for trouble later if you make this a habit.


#3

To answer your problem, that :point_up: line is not pushing what you think you’re pushing. The function String.prototype.toLowerCase() does not mutate/change the original string. So you’re wasting space by performing a toLowerCase() and then capitalizing the string.

The second part of that line just copies everything after the first char, which may or may not be all lowercase.


#4

But “String” is not the same as “string”. That was why i capitalise the first letter. even at that, i have change the variable totally and the code still did not pass the remaining two test.


#5

Thanks. i just figured out i should remove toLowerCase on that line.
It worked.


#6

I’m not saying they are the same. But I am saying it goes against best practices for Javascript.

As for the second part, the toLowerCase should be applied elsewhere.


#7

ok. i get that.
Thanks