Basic Algorithm Scripting - Title Case a Sentence

Tell us what’s happening:

Hello all. This is my second time asking for help here, however unlike the first time this time I simply want to ask… why is my code not working (i.e. being registered as correct) when the console window shows that it does work. I am truly at a loss here :frowning:

Important:

  • 2 of the 3 tests pass just fine, that is
  • Passed:titleCase("sHoRt AnD sToUt") should return the string Short And Stout.
  • Passed:titleCase("HERE IS MY HANDLE HERE IS MY SPOUT") should return the string Here Is My Handle Here Is My Spout.
  • this one does not however:
  • Failed:titleCase("I'm a little tea pot") should return the string I'm A Little Tea Pot.

Even though, like I have just said, the console window literally shows this:

I’m A LittLe Tea Pot

(no, there is no empty space at the end).

I have skimmed through some of the answers here, but most people used some “weird” and “strange” esoteric code which I have had no chance to meet, seeing as I have simply gone through the tutorials as they came.

I will share the glorious code that I have come up with, as well as the full console view of the last test.

The console window shows:

THE CONSOLE view of ‘myArrReg1’ array:
[ ‘I'm’, ‘a’, ‘little’, ‘tea’, ‘pot’ ]
This logs the FIRST WORD:
I’m
This logs the FIRST LETTER of the THIRD WORD:
l
This logs the tempStr string:
I’m A LittLe Tea Pot

Your code so far

function titleCase(str) {

  let myRegex1 = /\w+'*\w*/g;
  let myArrReg1 = str.match(myRegex1);
  console.log("THE CONSOLE view of 'myArrReg1' array:");
  console.log(myArrReg1);

  let myArrCapital = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "'"];
  let myArrNotCapital = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "'"];
  let tempStr = "";

  console.log("This logs the FIRST WORD:");
  console.log(myArrReg1[0]); // It logs the FIRST WORD.
  console.log("This logs the FIRST LETTER of the THIRD WORD:");
  console.log(myArrReg1[2][0]); // It logs the FIRST LETTER of the THIRD WORD.

  for (let k = 0; k < myArrReg1.length; k++) { // this is a loop for the array/SENTENCE
    for (let i = 0; i < myArrReg1[k].length; i++) { // this is a loop for WORDS
      for (let j = 0; j < myArrNotCapital.length; j++) {
        if (myArrReg1[k][i] == myArrCapital[j] && myArrReg1[k][i] == myArrReg1[k][0]) {
          tempStr += myArrCapital[j];
        } else if (myArrReg1[k][i] == myArrNotCapital[j] && myArrReg1[k][i] == myArrReg1[k][0]) {
          tempStr += myArrCapital[j];
        } else if (myArrReg1[k][i] == myArrCapital[j] && myArrReg1[k][i] !== myArrReg1[k][0]) {
          tempStr += myArrNotCapital[j];
        } else if (myArrReg1[k][i] == myArrNotCapital[j] && myArrReg1[k][i] !== myArrReg1[k][0]) {
          tempStr += myArrNotCapital[j];
        }
      }      
    }
    if (k < myArrReg1.length - 1) { // this means that space will be added 4 times for "I'm a little tea pot"
      tempStr += " ";
    } else {
      tempStr += "";
    }    
  }

  console.log("This logs the tempStr string:");
  console.log(tempStr); 
  
  str = tempStr;
  return str;
}
// titleCase("HERE IS MY HANDLE HERE IS MY SPOUT");
// titleCase("sHoRt AnD sToUt");
titleCase("I'm a little tea pot");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36

Challenge Information:

Basic Algorithm Scripting - Title Case a Sentence

Notice that both L in the little are capitalized in what function returns.

Oh my God, am I blind!? Thank you so much, this is facepalm-worthy honestly, but oh well, back to coding.