It turns out that I run this code in the Chrome Dev Tools
function titleCase(str) {
var newStr = str.split(" ");
//TeSt 10 array newStr
console.log(newStr);
for (var i = 0; i < newStr.length; i++){
// TeSt 20 1era letra de cada palabra
newStr.push(newStr[i][0].toUpperCase())
}
console.log(newStr)
}
titleCase("I´m a little tea pot");
Well, immediately afterwards it jumps to me: debugging connection was closed. Reason: Render process gone
From what I read is that there is an infinite loop. However, my only loop has an end, when it reaches the value of newStr.length
It is infinite because you push to the same array every iteration.
If you have 5 elements in the array, and when entering the first one you add 1, you have 6 elements. When you enter the 2nd you will have 7 elements, when you enter the 3rd, you will have 8 elements... and so on infinitely
To fix it, make
push
a temporary array:However, it would be making you an array of Uppercase, not words starting with uppercase.
To do this, you can concatenate it with
tempArr.push(newStr[i][0].toUpperCase() + newStr[i].substring(1))