0

JS Array in VAR

Hey, I want to count the length of an array with the help of a for loop. How could I do that? I need the VAR total to have the Value 20. Counted from myArr. Thank you so much! // The Code i tried var myArr = [ 2, 3, 4, 5, 6]; var total = 0; for (var i = 0; i < myArr.length; i++){ console.log(total) }

22nd Sep 2021, 8:20 AM
Deniz Lier
Deniz Lier - avatar
8 Answers
+ 4
Dear Deniz Lier, You can try this one: //js const myArr = [2,3,4,5,6]; let total = 0; for(let i = 0; i < myArr.length; i++) { total += myArr[i]; } console.log(total); In this case using the "for loop", you are able to get the array items one by one, and add them to the total; after job done, you will console the result! In other words, this happens: array defined, total is 0; loop starts: total = total + 2, // 0 + 2 = 2, total = total + 3, // 2 + 3 = 5, total = total + 4, // 5 + 4 = 9, total = total + 5, // 9 + 5 = 14, total = total + 6, // 14 + 6 = 20; loop finished and total = 20 console.log(total); // consoles 20 The other easier way is using the "for of loop", which exactly does the same job: //js const myArr = [2,3,4,5,6]; let total = 0; for(let sum of myArr) { total += sum; } console.log(total); Hope it worked! Any questions? I'm here to answer!
22nd Sep 2021, 1:03 PM
🇮🇱 Radin Masiha 🇮🇱
🇮🇱 Radin Masiha 🇮🇱 - avatar
+ 3
Welcome Deniz Lier, It's allways nice to help each other, and make programing easier, better, and nicer! As I see you like to learn, and like to find newer ways! There are several ways to get array values using different kind of loops. Here are two others: //1st way const arr = [2,3,4]; let total = 0; for(let sum in arr) { total += arr[sum]; } console.log(total) //logs 9; //2nd way const arr = [2,3,4]; let total = 0; arr.forEach(sum => { total += sum; }); console.log(total); //logs 9 in the js console But the best way is "for of"; the way that I allways recommend. Hope it helped you to understand arrays and loops better! Have a good day!
22nd Sep 2021, 9:56 PM
🇮🇱 Radin Masiha 🇮🇱
🇮🇱 Radin Masiha 🇮🇱 - avatar
+ 2
just increment the variable for (var i = 0; i < myArr.length; i++){ total++; } console.log(total);
22nd Sep 2021, 8:30 AM
Toni Isotalo
Toni Isotalo - avatar
+ 2
Radin Masiha thank you so much for this detailed answer!!! This was exactly what I needed. Also thank you for showing me such a better method for this
22nd Sep 2021, 9:30 PM
Deniz Lier
Deniz Lier - avatar
+ 1
Yo should increment "total" in the loop and print the value of total outside of the loop. Here, var myArr = [ 2, 3, 4, 5, 6]; var total = 0; for (var i = 0; i < myArr.length; i++){ total++ //console.log(total) } console.log(total) I hope, it helps. Happy coding!
22nd Sep 2021, 8:30 AM
mesarthim
mesarthim - avatar
+ 1
mesarthim Toni Isotalo thank you for the fast answer If I do total++ and do the log outside of the loop it just shows me "5". I need to take all values and calculate them with each other that the total value is 20 = 6+5+4+3+2
22nd Sep 2021, 8:47 AM
Deniz Lier
Deniz Lier - avatar
+ 1
Radin Masiha thank you so much I really appreciate your hard effort to help me code better!
26th Sep 2021, 10:01 PM
Deniz Lier
Deniz Lier - avatar
0
Answer
26th Aug 2022, 9:26 AM
Thotappaiah G P
Thotappaiah G P - avatar