Get What You Ask For explainer

The aim of this puzzle: Get the hotel reviews from Grassland and log them all to the console

Walkthrough of the solution: The getData() and findHotels functions are imported. getData() is used to access a database of reviews, and the findHotels function is used as a callback in getData() to retrieve only hotel reviews. Each hotel review is stored as an object with 4 properties: type, city, price, and rating. The printHotel() function will take a hotel review and log all of its properties to the console.

The 1st argument of the getDat() function is the name of a city. The puzzle asks to change this from 'Hopalot' to 'Grassland'. The result of getData() is stored in grasslandHotels. These Grassland hotel reviews can be printed using the printHotel function. Apply the .forEach() method to grasslandHotels, and use the printHotel callback as the argument.

Sample code solution:
(Tap below to reveal)

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

function printHotel(hotel) {
  console.log('___HOTEL_INFO___');
  console.log('type: ' + hotel.type);
  console.log('city: ' + hotel.city);
  console.log('price: ' + hotel.price);
  console.log('rating: ' + hotel.rating);
}

let grasslandHotels = getData('Grassland', findHotels);
grasslandHotels.forEach(printHotel);

JavaScript Concepts: Callback Functions, Calling Functions, Code Block (function), console.log(), .forEach(), 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);
};
2 Likes

Help. I have attempted this and did before checking here. However, I cant build this exact code, or I’m going something wrong.

I changed the Hopalot to Grassland no issue.

Attempted to change the callback function. But no matter what I do there ends up being an empty argument at the end. What am I missing?

1 Like

I cant seem to figure this one right. I looked at the hint and put that in but it would not allow me to put it 8n exactly. The hint made sense.

Help

1 Like

When a function like printHotel is created, there are 2 different buttons generated: printHotel and printHotel(). The parentheses at the end mean “run this function”. The button without the parentheses is just a “reference” to the function, like a variable that stores the code without running it. The callback uses the one without parentheses, because it’s like inserting the code from one function into another.

If you scroll the button keyboard to the side, you should see a grey colored printHotel key without parentheses.

–Frankie

1 Like

What am I missing?

1 Like

The 2nd argument of getData() should be the callback function: findHotels.

Then on a separate line, you can call .forEach(printHotel) on the grasslandHotels

–Frankie

Ah okay thank you so much. I’ll be playing with this in the playground soon

I think I am having the same problem as other people but am not quite getting the explanations!

One minute it seems to make sense then it’s gone :see_no_evil:

1 Like

The getData() function takes 2 arguments: A string that’s the name of a city, and a function that instructs what kind of data to get.

Since we want the grasslandHotels variables to store information about hotels in Grassland, we use the string 'Grassland' as the 1st argument. The 2nd argument is the name of a callback function: findHotels.

That will give you: let grasslandHotels = getData('Grassland', findHotels). And grasslandHotels will be an array filled with hotel review objects.

Next, you can use .forEach() on that array: grasslandHotels.forEach(). Inside the .forEach() you can use printHotel as the callback. That will run each array element through the printHotel() function. You can also think of this as applying the printHotel function to each element.

–Frankie

2 Likes

The solution code is not correct. The actual solution code is as follows (I have only referenced the last function call on the page as the rest does not change).

2 Likes

The code in your screenshot looks correct and matches the solution code in the original post. As long as the import statement is still at the top and no other code has been added, this should pass.

–Frankie

1 Like

Great idea if users could have the option “open this code in the playground”!

:slight_smile:

4 Likes

Hi,

Many thanks for your support.
But, I think the solution provided seems incorrect.
Please see below.
Could you be kind to correct it?
That will be a great help for us all neophytes in coding.!
Thank you once again!
Screenshot_20181217-190633_Chrome|557x499

1 Like

It seems your screenshot didn’t upload properly, so I can’t see your code.

–Frankie

Please see attached.

1 Like

The code screenshot you attached is from the original post in this thread which will solve the puzzle. You may have to scroll to the right to see all of the code, but if you copy this code into the app, it should work. If it’s not, could you post a screenshot of the code you typed inside the app?

–Frankie

1 Like

Thank you.
I need to scroll to the right to see the full coding.

Im stuck in here for a long time. I have the same problem as everywhan else. Can samewhan show what to do.

Hey there, happy to help!

Can you post a screenshot of your code? I can take a look and point out anything that needs to be fixed.

Ben

Hi! So I’m having a problem with printHotels. I’m doing the solution code, but when I put printHotels with my code, there is a blank next to it that is t there in the solution. What should I fill it with?