-5
const str = 'i have learned something new today';

const arr = str.split(" ");

for (var i = 0; i < arr.length; i++) {
    arr[i] = arr[i].charAt(0).toUpperCase() + arr[i].slice(1);

}
const str2 = arr.join(" ");
console.log(str2);

Above is a simple loop but i did'nt get this arr[i] part of the code and i need a deeper understanding for it.

when the code says “array[i]” it is referring to the for loop var “i”. So then the for loop is checking to see if the value of “i” is greater than the value of the var called “largest”. As soon as “i” is greater than largest, the value of largest gets set to that number.

Above is the explanation i get from the search but i am not satisfied with it?

Mushroomator
  • 6,516
  • 1
  • 10
  • 27
  • 6
    You should really be starting with some introductory tutorials on the JavaScript language. In particular, any part about "arrays". – David Dec 06 '22 at 23:30
  • 1
    [Documentation about arrays](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/Arrays). – Andy Dec 07 '22 at 00:07
  • See [What does this symbol mean in JavaScript?](/q/9549780/4642212) and the documentation on MDN about [expressions and operators](//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators). The [ES spec](//tc39.es/ecma262), [JS docs](//developer.mozilla.org/en/docs/Web/JavaScript/Reference), and [JS tutorials](//developer.mozilla.org/en/docs/Web/JavaScript/Guide) are all available to you. – Sebastian Simon Dec 07 '22 at 00:10

1 Answers1

0

No. You got the understanding wrong Bro. These little lines of code first separate each word of the sentence, then convert every first letter of the word into an uppercase, and lastly merge them together. The output ends up to be:

I Have Learned Something New Today.

const arr = str.split(" ");
for (var i = 0; i < arr.length; i++) {//other codes}

The code above result to:

  • arr = ['i', 'have', 'learned', 'something', 'new', 'today'];
  • The i is the counter (0, 1, 2, 3,..., 6 ).
  • arr[i] fetches any of the index i in the array. meaning If i = 1, then arr[1] is have and new when arr[4].

W3school JavaScript JavaScript Info Tutorial