Add It to the List explainer


#1

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 (let 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()