Basic Algorithm Scripting - Title Case a Sentence

I don’t know why my code is giving me an error. I take a string and change it to an array. Then I iterate through the array and for each word I capitalize the first letter. I then join the array back together at the end. Any help would be appreciated!

  **Your code so far**
function titleCase(str) {
let newStr = str.toLowerCase();
let nStr = newStr.split(" ");
for (let i = 0; i < str.length; i++) {
  nStr[i][0].toUpperCase;
}
return nStr.join(" ");
}

console.log(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/103.0.0.0 Safari/537.36

Challenge: Basic Algorithm Scripting - Title Case a Sentence

Link to the challenge:

Two issues

  1. You are never calling toUpperCase

  2. You cannot mutate a string, only replace them

Ah I see, thank you!

Why does it say that it can’t read 0?
function titleCase(str) {
let newStr = str.toLowerCase();
let nStr = newStr.split(" “);
for (let i = 0; i < str.length; i++) {
let upper = nStr[i][0].toUpperCase;
nStr.splice(0,1,upper);
}
return nStr.join(” ");
}

You don’t need to use some fancy methods. Just replace the entry at index i with the new string.

Ok, I will try it out. Thanks!

Ok, I understand solution 1 in the problem, but I still don’t get why my solution isn’t working. I keep getting an error that newStr[i][0].splice isn’t a function, and I don’t know why. This is what I have so far:

function titleCase(str) {
  let newStr = str.toLowerCase().split(" ");
  for (let i in str) {
    let upper = newStr[i][0].toUpperCase();
    newStr[i][0].splice(0, 1, upper);
  }
  return newStr.join(" ");
}

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

Why are you splicing? Don’t. Splice is not a string method.

Do not use splice.

This is not a string. This is an array of strings. Your name should be less confusing to help you see what’s going on.

You are calling splice on the first character of the word (string) in the array at index i. You can’t do that. You need to replace the whole word at index i.

Ah that makes sense. Thank you!