The aim of this puzzle: Add each hotel rating to the ratings
array
Walkthrough of the solution: The getRatings()
function is supposed to take an array of hotel review objects stored in hotelList
and then return
an array of just the ratings for those hotels. The ratings
array starts empty, and it will be returned at the end of the function, but between those steps, all of the ratings need to be added.
Add a for...of
loop in the middle of the function definition. That loop should loop through each element of hotelList
. Inside the loop, the rating should be added to the array using .push()
. To add something to the ratings
array, you use ratings.push(...)
. The thing you want to .push()
is the element.rating
.
Sample code solution:
(Tap below to reveal)
import { getData, findHotels } from 'grasshopper.reviews';
function getRatings(hotelList) {
let ratings = [];
for (var element of hotelList) {
ratings.push(element.rating);
}
return ratings;
}
let grasslandHotels = getData('Grassland', findHotels);
console.log('Ratings Array:');
console.log(getRatings(grasslandHotels));
JavaScript Concepts: Callback Functions, Calling Functions, Code Block (for loop, function), console.log()
, import
, .push()
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);
};