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
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: 98,
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: 185,
rating: 4.1
},
{
type: 'The Grassy Suites',
city: 'Grassland',
price: 86,
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: 189,
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 averageRating = hotelList => {
hotelList.map(hotel => hotel.rating).reduce((a, b) => a + b) / hotelList.length
};