function titleCase(str) {
let minStr = str.toLowerCase();
let tabWords = minStr.split(' ')
console.log(tabWords)
let result = [];
for (let i = 0; i < tabWords.length; i++) {
let firstLetter = tabWords[i][0].toUpperCase();
let tword = tabWords[i].split('')
tword.splice(0, 1, firstLetter)
tword = tword.join('');
result.push(tword);
console.log(result)
}
return result.join(' ')
}
titleCase("I'm a little tea pot");
In regard to this lines:
let tword = tabWords[i].split('')
tword.splice(0, 1, firstLetter)
tword = tword.join('');
Why is not possible to do that ?
let tword = tabWords[i].split('').splice(0, 1, firstLetter).join('');
As I said, splicedeletes whatever you specify, and it returns what you deleted. If you deleted the first item, that’s what it returns. The splice function returns an array, so you can run join in that just fine, but that array only contains that item you deleted – you are saying “delete one item in the array tword at index 0 and replace it with whatever firstLetter is”.
You want to run join on the array you modified (tword), not the return value of the splice operation