Title Case a Sentence bug(?)

Tell us what’s happening:
I know this code is not the best one because I did not use string methods before JS. The code passes for every conditions but not the “sHoRt AnD sToUt”. Although this does not pass, it works! I even split it to an array to see if there is a whitespace but there is not. Again I know this is not the best code and I can use more eligible codes but I want to know why it does not pass. Thanks.

Your code so far

var upperCase = [];
var slice = [];
var lowerCase = [];
var processedStringArray = [];
var processedString;
var finalProcess;
function titleCase(str) {
  var arrayOfStrings = str.split(/\s+/g);
  for(var i = 0; i < arrayOfStrings.length; i++) {
    upperCase[i] = arrayOfStrings[i].charAt(0).toUpperCase();
    slice[i] = arrayOfStrings[i].slice(1);
    lowerCase[i] = slice[i].toLowerCase();
    processedStringArray[i] = upperCase[i] + lowerCase[i]; 
    processedString = processedStringArray.toString();
    finalProcess = processedString.replace(/,/g , " ");
  return finalProcess;

titleCase("sHoRt AnD sToUt");

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36.

Link to the challenge:

The problem is that you’re using global variables. If you move all of your variables to be inside of your function, it works. Make this a habit that you follow because global variables will introduce a lot of bugs into your code.

1 Like

One of your global variables is causing the issue. The FCC tests run consecutively and any global variables retain their values after each test runs. Because of this, finalProcess is ‘Short And Stout Tea Pot’ when the test case of titleCase(“sHoRt AnD sToUt”) runs. Move your variables inside your function.


Thank you very much it works!

Thank you, it works!