# Summing certain elements from multiple arrays - Beginner level

**URL:** <https://forum.kirupa.com/t/summing-certain-elements-from-multiple-arrays-beginner-level/636008>\
**Category:** Uncategorized\
**Created:** [February 16, 2017, 4:40am UTC](https://forum.kirupa.com/t/summing-certain-elements-from-multiple-arrays-beginner-level/636008 "2017-02-16T04:40:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Baca](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/david_baca/32/7987_2.png) [@David\_Baca](https://forum.kirupa.com/u/David_Baca)\
**Post date:** [February 16, 2017, 4:40am UTC](https://forum.kirupa.com/t/summing-certain-elements-from-multiple-arrays-beginner-level/636008/1 "2017-02-16T04:40:51Z")

</div>

If I had four arrays that contained sales data for 4 different sales regions, each array has 4 indexes, one for each sales quarter.  
ex. var region1 = [1000, 1500, 1200, 3500];  
// [Q1, Q2, Q3, Q4]

1. Can I use a loop to sum sales by quarter for all regions? If so, how do I pass the array’s into the function to ensure the correct indices are being summed?  
Result:  
Total company sales for all regions-  
Q1: $10000  
Q2: $8000  
etc…

2. If I am adding total sales, is the best approach to push all data to a new array and sum?

Thank you for any assistance you can provide.

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [February 16, 2017, 4:07pm UTC](https://forum.kirupa.com/t/summing-certain-elements-from-multiple-arrays-beginner-level/636008/2 "2017-02-16T16:07:59Z")

</div>

Yes, you’ll want to use a loop. 2, in fact. One loop for the regions and one loop for the quarters.

Passing the arrays into the function and using the correct indices aren’t really related. But to pass the arrays in, you basically have two choices. You can use a function with a variable number of parameters, accessing the parameters as an array (or, rather, array-like object) through the special `arguments` object. Or you can use a single parameter that is an array that you create and pass in when calling the function.

```auto
function salesSum () {
  var regions = arguments;
  // ...
}
salesSum(region1, region2, regionN);

```

```auto
function salesSum (regions) {
  // ...
}
salesSum([region1, region2, regionN]);

```

Once you have the regions array, for summing you keep track of the index through loop variables - pretty standard stuff. The trick is the nested looping. You need a loop to go through each region, then a loop to go through each quarter. The result will be a new array containing the sums.

For the sake of simplicity, we’ll assume all quarters arrays are the same length so we don’t have to worry about error checking around cases where they might be different or incomplete (maybe region1 never provided Q4 results or something).

```auto
function salesSum () {
  var regions = arguments;
  var sum = []; // finals sales totals

  for (var i = 0; i < regions.length; i++) {
    var sales = regions[i];

    for (var j = 0; j < sales.length; j++) {
      if (sum[j] == undefined) {
        sum[j] = 0; // for first value, init to 0 so we dont get NaN adding to undefined
      }
      sum[j] += sales[j]; // add for each quarter
    }
  }
  return sum;
}

var region1 = [1000, 1500, 1200, 3500];
var region2 = [1001, 1501, 1201, 3501];
var sum = salesSum(region1, region2); // [2001, 3001, 2401, 7001]

```

One thing you may have noticed is an additional check added for undefined, which is what the array elements will be when we haven’t added anything to them yet. This is needed because if you add anything to the default undefined value, you’ll get a NaN value which will mess up the results. So we make sure that if the array element is undefined, we start it at 0. This could also be done when sume was created, doing something like `sum = [0, 0, 0, 0]`, but this approach is more flexible because it could potentially be used for sales data that includes more than just 4 quarters. It adds the 0 whenever its needed rather than assuming to know how many we need to start.

---

<div class="post-metadata">

**Author:** ![David\_Baca](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/david_baca/32/7987_2.png) [@David\_Baca](https://forum.kirupa.com/u/David_Baca)\
**Post date:** [February 17, 2017, 1:14am UTC](https://forum.kirupa.com/t/summing-certain-elements-from-multiple-arrays-beginner-level/636008/3 "2017-02-17T01:14:08Z")

</div>

Thank you very much for your help. I just sat down to hammer it out. I may hit you up again if that is okay?. I have been doing HTML/CSS and Wordpress so this is my first crack at an actual programming language. All of our reading and videos I can totally follow for the most part, but the assignments are at a slightly higher level.

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [February 17, 2017, 1:30am UTC](https://forum.kirupa.com/t/summing-certain-elements-from-multiple-arrays-beginner-level/636008/4 "2017-02-17T01:30:20Z")

</div>

You’re learning JavaScript, I assume?

---

<div class="post-metadata">

**Author:** ![David\_Baca](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/david_baca/32/7987_2.png) [@David\_Baca](https://forum.kirupa.com/u/David_Baca)\
**Post date:** [February 17, 2017, 3:06am UTC](https://forum.kirupa.com/t/summing-certain-elements-from-multiple-arrays-beginner-level/636008/5 "2017-02-17T03:06:23Z")

</div>

Yes, I am trying to learn it

---

<div class="post-metadata">

**Author:** ![David\_Baca](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/david_baca/32/7987_2.png) [@David\_Baca](https://forum.kirupa.com/u/David_Baca)\
**Post date:** [February 17, 2017, 4:55am UTC](https://forum.kirupa.com/t/summing-certain-elements-from-multiple-arrays-beginner-level/636008/6 "2017-02-17T04:55:33Z")

</div>

Hey bud. Here is my final code that is in adherence to the assignment requirements:

“use strict”;

var region1 = [1540, 1130, 1580, 1105];  
var region2 = [2010, 1168, 2305, 4102];  
var region3 = [2450, 1847, 2710, 2391];  
var region4 = [1845, 1491, 1284, 1575];  
var region5 = [2120, 1767, 1599, 3888];

//calculating quarterly sales  
function salesSum () {  
var regions = arguments; // use JS built in argument object  
var sum = []; // finals sales totals

for (var i = 0; i \< regions.length; i++) {  
var sales = regions[i];

```
for (var j = 0; j < sales.length; j++) {
  if (sum[j] === undefined) {
    sum[j] = 0; // for first value, init to 0 so we dont get NaN adding to undefined
  }
  sum[j] += sales[j]; // summing quarter sales
}

```

}  
return sum;  
}

var sum = salesSum(region1, region2, region3, region4, region5);

var quarterOneSales = sum[0];  
var quarterTwoSales = sum[1];  
var quarterThreeSales = sum[2];  
var quarterFourSales = sum[3];

//Quarterly sales window alert

window.alert(“Sales by Quarter” + ‘\n’ + "Q1: " + quarterOneSales + ‘\n’ + "Q2: " + quarterTwoSales+ ‘\n’ + "Q3: " + quarterThreeSales + ‘\n’ + "Q4: " + quarterFourSales);

//calculating regional sales  
function regionSum(region){  
var total = 0;

```
for (var i = 0; i < region.length; i++) {
total += region[i];

}
return total;

```

}

var regOneSales = regionSum(region1);  
var regTwoSales = regionSum(region2);  
var regThreeSales = regionSum(region3);  
var regFourSales = regionSum(region4);  
var regFiveSales = regionSum(region5);

//Sales by Region window alert

window.alert(“Sales by Region” + ‘\n’ + "Region 1: " + regOneSales + ‘\n’ + "Region 2: " + regTwoSales + ‘\n’ + "Region 3: " + regThreeSales + ‘\n’ + "Region 4: " + regFourSales + ‘\n’ + "Region 5: " + regFiveSales);

//Total Company Sales

function totalSales(regionOne, regionTwo, regionThree, regionFour, regionFive){  
var allRegions = regionOne.concat(regionTwo, regionThree, regionFour, regionFive);  
var total = 0;  
for (var i = 0; i \< allRegions.length; i++) {  
total += allRegions[i];  
}  
return total;  
}

var totalSales = totalSales(region1, region2, region3, region4, region5);

//Total Sales window alert  
window.alert("Total Sales: " + totalSales);

Let me know what you think.

Thank you.

Do you mind helping me some more? I really want to get this stuff down, but I need a lil push now and again so that it becomes more natural to me.

V/r,  
David
