At Any Rate explainer

The aim of this puzzle: Log all the hotel ratings of grasslandHotels to the console

Walkthrough of the solution: The grasslandHotels variable stores an array of hotel review objects. A normal for...of loop can be used to log the .rating property of each element. At the bottom of your code, create a new for...of loop that goes through each element of grasslandHotels. Inside the loop, the .rating property of each element should get printed using console.log().

Sample code solution:
(Tap below to reveal)

import { getData, findHotels } from 'grasshopper.reviews';

let grasslandHotels = getData('Grassland', findHotels);
for (var element of grasslandHotels) {
  console.log(element.rating);
}

JavaScript Concepts: Callback Functions, Calling Functions, Code Block (for loop), console.log(), import
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);
};
6 Likes

The UI would not allow me to do
for (let x of y)
I had to use to get the code to pass
for (var x of y)
see attached
image

6 Likes

Thanks for flagging!

–Frankie

4 Likes

I get stuck same problem here. And can not mo e on. Please help

1 Like

For this puzzle, you can pass using var in the For Loop. If you’re still unable to pass, you can post a screenshot of your code here.

–Frankie

1 Like

Hi, also stuck with this and for some reason it won’t let me choose the “let” to change the “var”.

Here’s a screenshot of my code and also followed the corrected code.

1 Like

You should be okay leaving the element as a var.

Inside the console.log(), it should be printing the rating property of the element, but it’s using a string that says ‘element.rating’. Try deleting the entire string so that the quote marks are gone. Then tap the element key and attach the .rating to it.

–Frankie

2 Likes

So I think this is the right code but it’s not letting me so I’m not sure

2 Likes

Hey there, it looks like you’re printing the string 'element.rating', instead of the variable element and its property rating.

Try deleting the string and instead tapping on the element key on the keyboard, followed by the .rating key.

Hope this helps!
Ben

2 Likes

Dont understand if it has the values why it wont import the ratings unless I have to create the list manually

1 Like

So basically grasslandHotels is a list that contains the names of hotels and those hotels again contain information about them ( price , ratings, etc)

So if I say I had to access the ratings of an hotel called ‘GrassHotel’ I would type GrassHotel.rating

In the above puzzle you are told to take every hotel in the grasslandHotels list and print the ratings of every hotel.

So you would type

for (let element of grasslandHotels) {
console.log(element.rating);
}

So now ‘element’ is basically all the hotels in the list and printing element.rating would do the job!

I hope you understand. Enlighten me if you think I’m wrong.

4 Likes

If it isn’t letting you input .rating after the element in the console log(it’s greyed out after inputting element) make sure to click on the element variable again to highlight it. It automatically deselects the variable when you enter a variable into the console.log parenthesis.

1 Like

Thank you sooo much for sharing!!!

2 Likes

1 Like

I did same exact code but it’s not letting me to pass the level

1 Like

I’m not able to pass this level please help




1 Like

I’m not able to pass this level please help





1 Like

Me too I am stuck help me lol do you we able to passed

1 Like

1 Like

I av done as supposed to,written the solution well,but when i run it tells me to try updating my parenthesis for the loop in element,am stuck really