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


#3

again, UI does not let me use

for (let x of y)
work had to use

for (var x of y)

image


#4

I have the same as Dennis —> it’s impossible to get the “let” instead of the “var” so I can’t proceed.
Totally frustrates me at the moment. Very itchy to solve on a smartphone.

Also: until now I managed to follow this course. This episode though I don’t get at all. It feels like a jump from 0 to 100 (if that expression makes sense in English).


#5

We have plans to make the For Loop change it’s template depending on what the expected solution code is. However, this puzzle should still complete using var in the loop.

–Frankie