JavaScript Sum and Average

Photo by Andrew Neel on Unsplash

JavaScript Sum and Average

·

4 min read

I was working on some homework earlier and the following questions were a part of the lab:

Create an array called numbers with the following values: 4, 8, 2, 9, 10, 12, 3, 5

Use a loop to calculate average. Output the average using document.writeln()

Use a loop to calculate sum. Output the final sum using document.writeln()

At first blush, this doesn't seem too difficult. First, we'll make the array:

// Create an array called numbers with the following values: 
// 4, 8, 2, 9, 10, 12, 3, 5

const numbers = [4,8,2,9,10,12,3,5]

Easy enough. Next step - calculate the average. I actually skipped this and went to the sum first, since in order to calculate the average you need to know the sum, so we'll do that here as well.

I was a little stumped at first, but eventually worked it out like this:

// Use a loop to calculate sum. 
// Output the final sum using document.writeln()

let y = 0;
for (let i = 0; i < numbers.length; i++) {
    y = numbers[i] + y;
}
document.writeln(y);

Let's break that down. First, we set the variable y to equal 0 (I chose "y" here because this question comes after the average question, in which I'm going to use the x variable, and I wanted to keep my variables alphabetical).

Then we start our loop, set i to 0, and iterate while i is less than the length of the numbers array. While that's happening, we're taking y (which is equal to 0 at this point) and adding the value of each location of the array to it.

To break it down - while i = 0, it's pointing at the first number in the array, in this case, 4. We then add 4 to x, meaning x = 4. The i variable then iterates up to 1, so i = 1 and it's now pointing at the second number in the array, which is 8. We then add 8 to x, which is currently 4. This makes x = 12. This continues in this fashion until the entire array has been looped through, and our sum is 53.

Now that we have code to figure out the sum of the array, we can work on the average. The average is simply the sum divided by the number of items used to get the sum. In this case, we have a sum of 53, which we achieved by adding 8 numbers. So we'll divide our sum of 53 by 8, resulting in 6.625

The code for that looks like this:

// Use a loop to calculate average. 
// Output the average using document.writeln()

var x = 0;
for (let i = 0; i < numbers.length; i++) {
    x = numbers[i] + x;
}
var avg = x / numbers.length;
document.writeln(avg);

This is just some quick math that I thought up while I was working through these problems, I'm almost certain there is a better/more efficient way to handle this but this will get the job done for now.

Here's the final look:

// Create an array called numbers with the following values: 
// 4, 8, 2, 9, 10, 12, 3, 5

const numbers = [4,8,2,9,10,12,3,5]

// Use a loop to calculate average. 
// Output the average using document.writeln()

var x = 0;
for (let i = 0; i < numbers.length; i++) {
    x = numbers[i] + x;
}
var avg = x / numbers.length;
document.writeln(avg); // Outputs 6.625

// Use a loop to calculate sum. 
// Output the final sum using document.writeln()

let y = 0;
for (let i = 0; i < numbers.length; i++) {
    y = numbers[i] + y;
}
document.writeln(y); // Outputs 53

I've been saving all my work for this class, and I'm hoping to circle back to it all when I finish the semester to see if I would work through it differently.

Also, I know that you're not meant to repeat yourself when coding. Finding the sum twice in this case is unnecessary, I can simply set a "sum" variable and use that for both the average and sum sections. I only went about it this way because I wanted the extra typing time to help build muscle memory.

My next post will likely be related to a Java lab, so stay tuned for that! Until then, thank you so much for taking the time to read this, and please leave any comments below if you have any ideas for how to optimize this!