Skip to content
Advertisement

How to add arrays with the same months in a loop?

Array Months

JavaScript

Array Values of Months

JavaScript

So, I wanted to add the number that falls in the same month

JavaScript

Desired Output

JavaScript

Code:

JavaScript

Output:

JavaScript

Please help me figure this out. Thank you.

Advertisement

Answer

Your problem is that you are not considering that on the 2nd time you arrive at a month that already exists you need to add it to the existing value … here is an example to get you through with all:

JavaScript

Will return:

JavaScript

Edit — I have edited my code and added the 0 for the numbers to be 2 figures for each month.

Just want to emphasize there are plenty more ways to do this! 🙂

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement