Question: I have made a for each loop to access numbers from a JSON array, the array will be dynamic and forever changing and so I have isolated the numbers inside a variable (the variable is inside the loop). My question is: How do I sum up the numbers that are in the variable.

I tried doing a for loop inside the loop but it didn't work.

it looks kind of like this:

```
 const arrayhours = Array.from(json_obj);
 arrayhours.forEach(e=>{ 
              const hours = parseFloat(e.hoursWorked);

console.log(hours); //returns for example 1.25 9 5 8 7 as seperate objects.
});

```

![image of console log](https://i.stack.imgur.com/Pj9XT.png)

Answer: 
You can use `Array.reduce()` for getting sum of numbers in an array.

```
const arrayhours = Array.from(json_obj);
arrayhours.forEach(e=>{ 
  const hours = parseFloat(e.hoursWorked);
  console.log(hours); //returns for example 1.25 9 5 8 7 as seperate objects.
});
const sum = arrayhours.reduce((acc, val) => acc + parseFloat(val.hoursWorked), 0)

```
Initialize the variable with `let hours = 0` before the loop and then use `+=` to add something (instead of `=`) to assign something.

Finally move the `console.log` out of the loop:

```
const arrayhours = Array.from(json_obj);
let hours = 0;
arrayhours.forEach(e => { 
  const hours += parseFloat(e.hoursWorked);
});
console.log(hours);

```