0
Can anyone explain me this code, especially the loop part!
function titleCase(str) { let newStr = str.split(' ') let updateStr = [] for (let i in newStr) { updateStr[i] = newStr[i][0].toUpperCase() + newStr[i].slice(1).toLowerCase() } return updateStr.join(' ') } console.log(titleCase("I'm a little tea pot"));
1 ответ
+ 2
Line 1 The code takes the string,
Line 2 Split the string where there is a space (splits into words), it becomes a list
Line 3 Creates an empty list
Line 4 Loops an through the word list
Line 5 Changes the First letter of each word into Upper Case, and the second to lower case and store in variable updateStr
Line 6 It returns the list. Where the first and second letter are separated by space
Line 7 Prints out the result of the code.
* I hope this helps you understand