It Will Average Out explainer


#1

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