Code Wars - Calculate Average
Breaking down code war challenges in a simple and understandable way.
The Problem Statement
Write a function that calculates the average of the numbers in a given list.
For example, let the array be [1,2,3,4,5]. The average will be 3.
The formula for average is the "total of items/ no.of items".
If we sum the total of the array it will come to 15 and the number of items in the array is 5
If we divide the total i.e; 15 by the number of items i.e 5, we get the average of 3.
Keep this concept in mind. We will refer back to this later in this post.
Analysis -breaking down the question
We need to write a function that takes an array and returns the average of the array.
Here list refers to an array.
We will use the for loop to solve this challenge. So let’s see the syntax for For loop.
The Syntax for solving the problem
for( var i=0; i<array.length; i++ ){
//Your code goes here
}
The Solution
Let’s create a basic function
function find_average() {
}
The function takes an array as a parameter. So add the array to the function as below.
function find_average(array) {
}
Let’s talk about For Loop in plain English for a moment.
For Loop: It loops through every item in the array till the specified limit and executes the code between these {} parenthesis.
for( var i=0; i<array.length; i++ ){
//Your code goes here
}
What happening here is:
- Var i=0 - We are declaring a variable i and assigning a value 0 to the variable.
- The second statement runs only if the condition is true. Otherwise, it will stop i.e; it runs till i is less than array. length
- Statement 3 will increment the initial variable i.e; i
Let’s put the for loop into the function.
function find_average(array) {
for (let i = 0; i < array.length; i++) {
}
}
If you remember the example in the problem statement section, we need two things total of the array and the number of items.
For that, we need two variables to store that information as shown below
function find_average(array) {
var total =0;
var count = 0;
for (let i = 0; i < array.length; i++) {
}
}
Since we don’t know what’s in the array and how long it will be, we will assign both total and count to 0.
We can use these two variables to get the desired result as below.
function find_average(array) {
var total =0;
var count = 0;
for (let i = 0; i < array.length; i++) {
total += array[i];
count++;
}
}
Finally, we can return the average as shown below
function find_average(array) {
var total =0;
var count = 0;
for (let i = 0; i < array.length; i++) {
total += array[i];
count++;
}
return total/count;
}
Here, total += array[i] is equal to total = total + array [i]. array[i] refers to the current element in the array. Since it is in the loop current element will be 1,2,3,4,5.
Try to console log the array[i] to see it for yourself.
function find_average(array) {
var total =0;
var count = 0;
for (let i = 0; i < array.length; i++) {
console.log(array[i]) //1 2 3 4 5
total += array[i];
count++;
}
return total/count;
}
find_average([1,2,3,4,5])
You will understand this concept in detail with the help of an example.
For example, let the array be [1,2,3,4,5].
Before the loop starts the value of the total and count will be 0.
When the loop starts it will add each item to the total until i is less than array. length.
Here's how it will look:
// Before loop - total:0, count:0.
// For the first iteration - total: 0+1 =1, count:0 (because javascript counting starts from 0 ) but for the sake of our understanding, we will set count =1.
//Second iteration - total: 1+2 = 3, count:2
//Third Iteration - total:3+3 =6, count:3, and so on until the total is 15 and the count is 5 (refer to above example)
function find_average(array) {
var total =0;
var count = 0;
for (let i = 0; i < array.length; i++) {
console.log(array[i]) //1 2 3 4 5
total += array[i];
count++;
}
return total/count;
}
find_average([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,15]) //result 8
Here, we called the function and passed an array. It calculated the average of an array as 8.
Alternate Method if any
Method 1
function find_average(array) {
// your code here
var total = 0;
var count = 0;
array.forEach(function (item, _index) {
total += item;
count++;
});
return total / count;
}
Method 2
function find_average(array) {
const output = array.reduce((prev, curr) => prev + curr,0);
return output / array.length;
}
I suggest you go through the alternate methods and find how they work by yourself. If you find it hard to understand, let me know in the comments.
Final Words
Loops might be confusing at first but give it a try a few times until you understand it better.
If you come this far and liked it, show some appreciation by liking, commenting your opinions, or sharing this with your network. Someone might find it helpful