It Will Average Out explainer

The aim of this puzzle: Complete the averageRating() function definition so that it returns the average rating for a list of hotels. Then find the average rating for Hoptropolis hotels.

Walkthrough of the solution: The average() and getRatings() functions that you created in the previous puzzles are imported so that you can use them here.

The averageRating() function definition will take a list of reviews stored in hotelList. Then it uses the getRatings() function to get an array of just the ratings of those hotels and stores it in ratings. To calculate the average of the ratings, you can use your average() function. Instead of returning 0, the function should return average(ratings).

The hoptropolisHotels variable is storing an array of all the hotels in Hoptropolis. To get the average rating, you can use averageRating(hoptropolisHotels). In order to see the output of that call, you need to use a console.log(). That whole command should go at the bottom of your code.

Sample code solution:
(Tap below to reveal)

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

function averageRating(hotelList) {
  let ratings = getRatings(hotelList);
  return average(ratings);
}

let grasslandHotels = getData('Grassland', findHotels);
console.log('Average Grassland hotel rating:');
console.log(averageRating(grasslandHotels));

let hoptropolisHotels = getData('Hoptropolis', findHotels);
console.log('Average Hoptropolis hotel rating:');
console.log(averageRating(hoptropolisHotels));

JavaScript Concepts: Callback Functions, Calling Functions, Code Block (function), import
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 average = array => {
   array.reduce((a, b) => a + b) / array.length
};

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

I have placed the soloution the puzzle asks for in the code correctly. I even revealed the soloution just to check my work. Do you know what is wrong?

1 Like

Hey there, it looks like your averageRating() function is returning 0, instead of returning average(ratings). Changing that line of code should solve the puzzle.

Hope this helps!
Ben

2 Likes

Hi,
I can’t figure out this puzzle “It will Average Out”
Need help. Spend a couple hours trying to solve it.

Hey there, take a look at the solution code posted at the top of this thread. If that’s not helpful, try posting a screenshot of your code and I can take a look.

Ben

I’ve complete the puzzle “It will average out” and my code shows it is the same as what its supposed to look like but it won’t let me continue.

1 Like

I have the same problem. There seems to be a bug in this example. My solution output matches, and the code matches the example solution above but it still doesn’t accept the answer.

To bad its unskippable.

@Genie_Dinsmore @Nick_Gibson

Hey there, post a screenshot of your code and I’ll take a look.

Ben


I did it all right?

Hey there, try changing your return statement in the function to just return average(ratings).

1 Like

I have no idea we’re to put console. Log

Look at the structure of the Grassland let & console.logs and use that same structure for Hoptropolis.

I need help

Hey there, on the last line, change console.log(getRatings(hoptropolisHotels)) to console.log(averageRating(hoptropolisHotels))

Hope this helps!
Ben

It so easy just see in deep

This looks right. What am I missing?

You wrote ‘hoptropolisHotels’ as a string in the last console.log. You should not use a string but the grey ‘hoptropolisHotels’ provided by the app where you select the different codes.


would u mind looking at my coding
I dont know whats wrong with it

In the return statement remove hoptropolisHotels.