# Split Up Evenly explainer

The aim of this puzzle: Complete the `average()` function definition so that it returns the average value of the `array`

Walkthrough of the solution: The average (also called the “arithmetic mean”) of a set of numbers is calculated by adding all the numbers up and the dividing the total by the number of values used.

The function definition starts by creating a variable called `total`. Since nothing has been added up yet, it starts at 0. Next, there’s a `for...of` loop that goes through each `element` of the `array`. Inside that loop, the `element` should be added to the `total` using the `+=` operator. Once the loop is done, the `total` will be the sum of all the numbers in the `array`. To `return` the average, the `total` should be divided by the number of values. The number of values is the `.length` of the `array`, so the `return` value should be `total / array.length`.

Sample code solution:
(Tap below to reveal)

``````import { getData, findHotels } from 'grasshopper.reviews';
import { getRatings } from 'myFunctions';

function average(array) {
let total = 0;
for (let element of array) {
total += element;
}
return total/array.length;
}

let grasslandRatings = getRatings(getData('Grassland', findHotels));
console.log(grasslandRatings);
console.log('The average is:');
console.log(average(grasslandRatings));
``````

JavaScript Concepts: Arithmetic Operator (/), Assignment Operator (+=), Callback Functions, Calling Functions, Code Block (for loop, function), `console.log()`, `import`, `.length`
Additional Code (hidden code that runs before the puzzle’s code):

``````let _hotelReviews = [
{
type: 'Hopaday Inn',
city: 'Hopalot',
price: 138,
rating: 4.0
},
{
type: 'Hopaday Inn',
city: 'Hopalot',
price: 78,
rating: 3.8
},
{
type: 'Hopaday Inn',
city: 'Hoptropolis',
price: 86,
rating: 2.4
},
{
type: 'Hopaday Inn',
city: 'Hoptropolis',
price: 126,
rating: 4.4
},
{
type: 'Hopaday Inn',
city: 'Grassland',
price: 138,
rating: 3.0
},
{
type: 'Hopaday Inn',
city: 'Grassland',
price: 245,
rating: 4.6
},
{
type: 'The Grassy Suites',
city: 'Hopalot',
price: 189,
rating: 4.4
},
{
type: 'The Grassy Suites',
city: 'Hopalot',
price: 111,
rating: 2.4
},
{
type: 'The Grassy Suites',
city: 'Hoptropolis',
price: 171,
rating: 2.8
},
{
type: 'The Grassy Suites',
city: 'Hoptropolis',
price: 191,
rating: 2.8
},
{
type: 'The Grassy Suites',
city: 'Grassland',
price: 265,
rating: 4.1
},
{
type: 'The Grassy Suites',
city: 'Grassland',
price: 186,
rating: 3.5
},
{
type: 'Hopton Inn',
city: 'Hopalot',
price: 160,
rating: 2.4
},
{
type: 'Hopton Inn',
city: 'Hoptropolis',
price: 226,
rating: 4.5
},
{
type: 'Hopton Inn',
city: 'Hoptropolis',
price: 192,
rating: 3.5
},
{
type: 'Hopton Inn',
city: 'Grassland',
price: 149,
rating: 4.9
}
];

const getData = (city, afunction) => {
afunction(city.trim().charAt(0).toUpperCase() + city.trim().toLowerCase().slice(1));
};

const findHotels = city => {
_hotelReviews.filter( review => review.city === city);
};

const getRatings = hotelList => {
hotelList.map(hotel => hotel.rating)
};
``````
16 Likes

Hi

Why does Let Total=0? What is the use of this part of the code?

Thanks

3 Likes

The Let means that the total of zero is ‘just for now’ in this function. You then need to start adding numbers together by adding += them to the Total, which starts out as 0.

8 Likes

I think I did somehow

7 Likes

I’m still having a hard time understanding the role of ‘Element’. Is it a function or a variable? That’s part of what kept me hung up on this puzzle.

4 Likes

When the for of loop iterates through the array, each item in the array is assigned to the temporary variable `element`. You can rename this variable if you want, for example, `num` or `taco`.

The 1st time the loop runs, `element` is whatever the first item in the array is. The 2nd time, `element` represents the 2nd item, etc…

Hope this helps!
Ben

9 Likes

What does it exactly happen if I code this way?

I’m stacked guy I need some help here

Hey, your code looks ok to me. Does any message pop up when you try to run code?

–Ben

2 Likes

Yah man now I’m on intro to interview at bhaji stage*

Hi, what does the ‘myfunction’ do. What is it where is it defined. Also, what is getData is that a js standard parameter and where is it defined same with getRatings? Likewise where is array defined. Why is there a period between grasshopper.reviews?

element is a js defined command that knows to Look through all the array elements. Correct? It’s not a variable.
Thank you

1 Like

Hey there,

The `import` keyword is useful for importing variables and functions that are declared in separate files. In Grasshopper, this allows us to use code that is not relevant to the lesson being taught, while also hiding it so that the puzzle’s code is kept short and easier to read, especially on devices with limited screen space.

`element` is the looping variable for the for loop. Its name is arbitrary, so you can rename it to whatever you like. For example:

``````for (let element of exampleArray) {
console.log(element);
}
``````

is the same as

``````for (let taco of exampleArray) {
console.log(taco);
}
``````

Hope this clears things up.

• Ben
4 Likes

I passed the last one quick after ur help idk what more they want me to do

1 Like

Why does the result of the average is 4.0200000000000005, instead of 4.02?

1 Like

It’s a simple question with a complicated answer. This video explains it much better than I can!

2 Likes

Does it work? You might want to console.log(average(getRatings))

1 Like

I don’t get it i did Evreything i was supposed to do but it says just says stuck heres some help i am now frustrated

what do we do with the if statment?

1 Like

the code written is wrong: there has to be an if statement.