# Add outputs of a function together, react

**URL:** <https://forum.freecodecamp.org/t/add-outputs-of-a-function-together-react/381765>\
**Category:** General\
**Created:** [May 1, 2020, 2:09am UTC](https://forum.freecodecamp.org/t/add-outputs-of-a-function-together-react/381765 "2020-05-01T02:09:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Imstupidpleasehelp](https://avatars.discourse-cdn.com/v4/letter/i/7ab992/32.png) [@Imstupidpleasehelp](https://forum.freecodecamp.org/u/Imstupidpleasehelp)\
**Post date:** [May 1, 2020, 2:09am UTC](https://forum.freecodecamp.org/t/add-outputs-of-a-function-together-react/381765/1 "2020-05-01T02:09:21Z")

</div>

Hello everyone, I am stuck on the very last step of this code.  
I am trying to get the value of all elements with a certain className. (check) And then turn those strings into numbers (check) and then add all of those numbers together (now) then set that number as a state (will do)

```auto
var pricevar = document.getElementsByClassName("cartprice");
      var i;
      let pricearray = [];
      for (i = 0; i < pricevar.length; i++) {
        pricearray[i] = pricevar[i].textContent;
      }
      var string = JSON.stringify(pricearray);
      string.replace(/"/g); //remove whitespace and "" 
      
//Make them into numbers 
      for (const element of pricearray) {
        let numberarray = parseInt(element);
        console.log(numberarray)
      
      }

```

Right now things are working fine. It is logging the numbers to the console. I am wondering how exactly will I be able to add those numbers together and display the total?  
I hope this is a simple solution that I am not seeing. Thank you in advance for your time.

---

<div class="post-metadata">

**Author:** ![Naomi](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/naomi/32/422181_2.png) [@Naomi](https://forum.freecodecamp.org/u/Naomi)\
**Post date:** [May 1, 2020, 2:12am UTC](https://forum.freecodecamp.org/t/add-outputs-of-a-function-together-react/381765/2 "2020-05-01T02:12:50Z")

</div>

If `console.log(numberarray)` is logging the number correctly, then you should be able to create another variable (say `number`, for example) and add `numberarray` to that variable with each iteration of your `for` loop.

Does that help?

---

<div class="post-metadata">

**Author:** ![Imstupidpleasehelp](https://avatars.discourse-cdn.com/v4/letter/i/7ab992/32.png) [@Imstupidpleasehelp](https://forum.freecodecamp.org/u/Imstupidpleasehelp)\
**Post date:** [May 1, 2020, 2:22am UTC](https://forum.freecodecamp.org/t/add-outputs-of-a-function-together-react/381765/3 "2020-05-01T02:22:49Z")

</div>

Ah sorry I wasn’t being so clear. It is logging the numbers but it is logging them all separately. So when I assigned them to a variable it only gave me the first number instead of all of them added together.  
The numberarray is logging like this

```auto
14
35

```

---

<div class="post-metadata">

**Author:** ![Naomi](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/naomi/32/422181_2.png) [@Naomi](https://forum.freecodecamp.org/u/Naomi)\
**Post date:** [May 1, 2020, 2:43am UTC](https://forum.freecodecamp.org/t/add-outputs-of-a-function-together-react/381765/4 "2020-05-01T02:43:26Z")

</div>

Because with each loop, you are overwriting `numberarray` with that specific number.  
Let’s try this:  
Before (and outside of) `for (const element of pricearray) {` loop, establish `numberarray` as a variable equaling an empty array.  
Then in your loop, take your `parseInt(element)` (which is returning the correct numbers, just separately right?) and add the number it returns into your `numberarray`. There are a couple of array commands you can use to accomplish this.

---

<div class="post-metadata">

**Author:** ![Imstupidpleasehelp](https://avatars.discourse-cdn.com/v4/letter/i/7ab992/32.png) [@Imstupidpleasehelp](https://forum.freecodecamp.org/u/Imstupidpleasehelp)\
**Post date:** [May 1, 2020, 3:59am UTC](https://forum.freecodecamp.org/t/add-outputs-of-a-function-together-react/381765/5 "2020-05-01T03:59:13Z")

</div>

```auto
let numberarray = []
      for (const element of pricearray) {
        let pricenumbers = parseInt(element);
        numberarray.push(pricenumbers)
      console.log(numberarray)
      }

```

So this is what I came up with. It seems to work because I get an array of 2 items with the two numbers as I want them. So now I just need to add them together.  
Thank you for your help!

---

<div class="post-metadata">

**Author:** ![Naomi](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/naomi/32/422181_2.png) [@Naomi](https://forum.freecodecamp.org/u/Naomi)\
**Post date:** [May 1, 2020, 4:03am UTC](https://forum.freecodecamp.org/t/add-outputs-of-a-function-together-react/381765/6 "2020-05-01T04:03:46Z")

</div>

Glad I could help! Nice work!

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 6:56pm UTC](https://forum.freecodecamp.org/t/add-outputs-of-a-function-together-react/381765/7 "2021-06-01T18:56:07Z")

</div>

Closed on June 1, 2021, 6:56pm UTC


