Summing certain elements from multiple arrays - Beginner level

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.

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.

function salesSum () {
  var regions = arguments;
  // ...
}
salesSum(region1, region2, regionN);
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).

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.

1 Like

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.

You’re learning JavaScript, I assume?

Yes, I am trying to learn it

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