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