Add It to the List explainer

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 Likes

again, UI does not let me use

for (let x of y)
work had to use

for (var x of y)

image

2 Likes

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

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

1 Like

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

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

–Frankie


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


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.

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

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

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

De esta manera funciono.

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

1 Like

This worked for me if any of you guys are still stuck

1 Like

I still am lost you are talking to me like a lifelong programmer

Hey there, if you’re still having issues after reading the other posts in this thread, try posting a screenshot of your code. I can take a look at it and point out anything that needs to be changed.

Thanks,
Ben

The code is good its just your line location took me a minute to figure out

Although it accepted the code above i think it’s an added in work around because my example is blank and does not match the example

Hey there! Post a screenshot of your code and I can take a look.

Thanks,
Ben

I figured the problem out, finally, with a little help from the program itself.

1 Like