There are enough comments in the code below.
The right letter gets capitalized, but the replacement goes wrong.
function titleCase(str) {
// make all leters toLowerCase and convert str to array
let arr = str.toLowerCase().split(' ');
let arr1 = [];
for (var i = 0; i < arr.length; i++) {
// take the first letter of every element and turn it to upperCase
let replace = arr[i].charAt(0).toUpperCase();
console.log('replace ', replace);
// This log shows that the right letter gets capitalized
// but it gets assigned at the begining of the array
console.log(arr);
// delete the first letter of every element and replace it...
arr1 = arr.splice(arr[i].charAt(0), 1, replace);
// the replacement did not succeed
console.log('charAt(0) ', arr[i].charAt(0));
}
return arr1.join(' ')
}
titleCase("I'm a little tea pot");
Your browser information:
User Agent is: Chrome.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/title-case-a-sentence/