Get What You Ask For explainer


#1

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


Get what you ask for
#2

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?


#4

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


#5

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


#6

What am I missing?


#7

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


#8

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


#9

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:


#10

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


#11

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


#12

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


#13

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

:slight_smile: