Title Case a Sentence (using for of loop)

Can someone help explain why the for of loop doesnt work in this case?

2

The loop is working, however the assignment word = ... is changing the value of the variable iterator and not the value inside the iterable.

Just as example you can see how the original array is untouched at the end of the iteration while the values are actually updated (and stored in a new array):

const a = [10, 20, 30];
let n = [];

for(val of a) {
  val = val + 1;
  n.push(val)
}

console.log(a, n) // [ 10, 20, 30 ] [ 11, 21, 31 ]

There is a built in methods to iterate over an array and update the current value, Array.prototype.map().

Hope it helps :+1:

Hi @umn2024 , I would like to help you with this problem.

Firstly, for of loop works in such a way that, it creates a loop iterating over an array. On each iteration each index’s value will be assigned to the variable(in your case variable word).
For detailed explanation : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of

In your above solution, you have assigned values to the variable word, it is taking changes. Words are converted to title case. There’s no problem with that.

But you’re not reassigning or pushing converted words to wordArray .

Now the solution is so simple, take a new array, and push the converted words to that array.

Finally replace the wordArray with your new array in your return statement.

You’ll get title cased sentence.

All the best

:slightly_smiling_face: