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

1 Like
Add it to the List exercise
#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).

3 Likes
#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

#6

Hi! Can anyone help (or give me the code!) for this exercise? I’ve been working on it for a good couple of hours and no success!

1 Like
#7

I’ve moved your post to the explainer for this puzzle. The 1st post on this page has the solution code.

–Frankie

#8


I think it can’t be finish with this var instead of let in this function. I stuck:-(

#9


Code still doesn’t work… I stepped away from it for a week, hoping it was just frustration…but I can’t see how I’m supposed to get it to loop through each element with the current array of commands.

#10

This puzzle will still complete with var or let. Inside the For Loop, the variable that will be looped through should be hotelList instead if findHotels. The hotelList is an array, and findHotels is a function.

–Frankie

#11

When you have a return inside of a function, it says, “Compute this line, and then use that value as the final output of this function.” That means, any lines of code after that return ... will be ignored.

Try deleting the 1st return ratings in your code, and keep just the final one.

–Frankie

#12

This puzzle has probably been the most frustrating thus far jeez, also I wish you would stop patronizing users Everytime a quiz is failed with “you’ll get it next time” … Seriously!

1 Like
#14

De esta manera funciono.

#15

This exercise is not right, so very frustrating! Fix it and let me know when it’s fixed. I’ll be learning to code on Momo until you do