For the Right Price explainer


#1

The aim of this puzzle: Create the underAHundred function and use it as a filter

Walkthrough of the solution: The underAHundred function should take a hotel review object stored in hotel and then it will return whether the .price of that hotel is less than 100. That test is written as hotel.price < 100, and that should be used as the return value.

To use the underAHundred function as a filter for the grasslandHotels, use the .filter() method on it. The argument of the .filter() is a callback function, underAHundred, without parentheses after it. grasslandHotels.filter(underAHundred) will return a copy of the grasslandHotels but only the ones that have a .price property less than 100.

Sample code solution:
(Tap below to reveal)

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

let grasslandHotels = getData('Grassland', findHotels);

function underAHundred(hotel) {
  return hotel.price < 100;
}

let affordableHotels = grasslandHotels.filter(underAHundred);
console.log('Average Rating in Grassland under $100:');
console.log(averageRating(affordableHotels));

JavaScript Concepts: Callback Functions, Calling Functions, Code Block (function), .filter(), import