Can anyone explain me this code, especially the loop part! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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"));

17th Sep 2022, 8:40 PM
Josué Varela
Josué Varela - avatar
1 Answer
+ 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
18th Sep 2022, 7:29 AM
Abdul Muhaimin Salay
Abdul Muhaimin Salay - avatar