function titleCase(str) {
let minStr = str.toLowerCase().split(' ');
console.log(minStr)
let result = [];
for (let i=0 ; i < minStr.length; i++) {
let tabWord = minStr[i].split('').splice(0,1,minStr[i][0].toUpperCase());
console.log(tabWord)
}
//console.log(minStr)
}
titleCase("I'm a little tea pot");
the result is:
and if I do that:
let tabWord = minStr[i].split('')
tabWord.splice(0,1,minStr[i][0].toUpperCase());
the result is: it’s what I need
and if I do that:
let tabWord = minStr[i].split('')
let tabUpper = tabWord.splice(0,1,minStr[i][0].toUpperCase());
console.log(tabUpper)
Whyyy this differences?
So I come back to the second step and I add join():
let tabWord = minStr[i].split('')
tabWord.splice(0,1,minStr[i][0].toUpperCase()).join('');
this change anything:
but if I do that:
let tabWord = minStr[i].split('')
tabWord.splice(0,1,minStr[i][0].toUpperCase());
let stringUpper = tabWord.join('')
console.log(stringUpper)
That’s works !!!
Whyyyy?
Could you please say to me what mistakes of thought I make in each case ?
console.log(tabWord.splice(0,0,minStr[i][0].toUpperCase()).join('')) gave me the only the lower case letter. i then did a little reading on splice and found:
Hi Jeremy, my pleasure to work with you again.
It’s a problem that I hadn’t well understood in last september. Today I rework this execice and I wonder the same question.
So I hope I will finally well understans.
It’s weird for me because with let tabWord = minStr[i].split('') we transform a string to a table. So it’s weird that it’s not possible to make the reverse. Table to string.
Splice returns the values that were removed. You can’t call join after a splice and get the remaining letters all joined up… With splice.join you get the removed letters joined up.
Join makes a new string. That new string needs to be saved somewhere.
Ok I well understand for splice. So for sum up , can I say that it’s due to the splice methode ?
because when I look the FCC solution 2:
function titleCase(str) {
return str
.toLowerCase()
.split(" ")
.map(val => val.replace(val.charAt(0), val.charAt(0).toUpperCase()))
.join(" ");
}
titleCase("I'm a little tea pot");
here the methodes’ actions are put one behind the other and as map just tranforme the string it’s possible to use join at the end.
I don’t know if I have rigth to make this comparing because in this case str still a string from begining to the end.