Basic Algorithm Scripting - Title Case a Sentence

please can anyone help me explain
the role of
the for…in loop in this code .
i dont really understand how the loop works on things challenge.

please i need enlightenment.

Your code so far

function titleCase(str) {
  
  const newTitle = str.split(" ");
  const updatedTitle = [];
  for (let st in newTitle) {
    updatedTitle[st] = newTitle[st][0].toUpperCase() + newTitle[st].slice(1).toLowerCase();
  }
  return updatedTitle.join(" ");
}
  


console.log(titleCase("I'm a little tea pot"));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.54

Challenge: Basic Algorithm Scripting - Title Case a Sentence

Link to the challenge:

The for...in loop iterates over enumerable properties (in this case, indices) of an object (in this case, an array). On each iteration, a variable (in this case, st) is assigned the value of one of these properties (in this case, an index). The loop then executes its block of code for each property.

In this specific example, on each iteration of the loop, st is assigned one of the indices of the newTitle array.

Here is a link to a freeCodeCamp video: for in / for of - Beau teaches JavaScript - YouTube

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.