Add It to the List explainer

This is how to do it correctly

Hi,
Pls find below my code

function getRatings(hotelList,___){

let ratings = [
‘8’,
‘5’,
‘6’,
‘2’,


]
;
The message

Blockquote
Extra items have been added to the ratings array, remove those items so the array is empty.

Blockquote
I know what that means but no delete, up, backspace key in my keyboard can get rid of the last “___” on bottom of the array afore said. Is there anything wrong with th UI?
Regards

Hey there,

Use the for loop to push each rating into the array, rather than directly adding elements to the array. Take a look at the solution code at the top of this thread.

Ben

I moved return ratings; to the end of function getRatings and it worked with var

I solved this puzzle, let me know if you are still stuck

it took me a second, but I realized that I was pushing the values into the array but had the “return” statement midway through the argument :joy::joy::laughing::laughing:

2 Likes

Frankie, hello I keep getting this message 'E. Error message can not read property '% element. rating undefined explain please.

Hey there, post a screenshot of your code and I’ll take a look.

Ben explain to me 'property not defined and, how to correct it thank you

Please send a screenshot of it…

Someone please help with this, I’ve tried everything I can think of and cant get the list of ratings to print out & of course every time get told to change something else.! And since the topic’s closed I cant read what help’s already been given.
Feeling really dumb right now :frowning: Screenshot_20191218-192321|281x500

Hey there, I moved your post into this thread, which is the official explainer thread for this puzzle. Take a look at the explainer post (it’s the 1st post in this thread), as well as the posts from other users. That should help you get unstuck.

Hope this helps!
Ben

Thank you!! I couldnt do it because I was trying to do it inside the block of let ratings [ ] instead of outside & of course couldnt put it there.

1 Like

I’m really proud to hear you were able to solve the problem on your own!

So I was doing the same thing as elsea. I got it to work but I dont really understand why. What does let=[ ] do? I guess I dont understand why we leave the brackets empty

I figured out how to code it but it wont let me pass ingot everything right and some like wtf I’m ready to move on

@John_Smith1 @Ryan_Sipple

Hey there,

In this puzzle, we know we’ll need to store all of the hotel ratings in an array. However, when we declare that array, we don’t yet know what the ratings are, so we don’t yet know what to put in the array.

That’s why we create an empty array, and then use a for loop to go through hotelList and use .push() to add each hotel’s rating to the array.

When the for loop finishes running, the array will contain all of the ratings.

Hope that makes sense!
Ben

Do I have to add anothything else I’m trying to get past this so I can get my second certificate I’m lost

You’re almost there! There are just a few issues on 2 lines.

First, you have the line return ratings in the middle of the function. The return keyword tells a function to immediately stop executing and return a value. Any code after it won’t run.

This means that right now, your function creates an empty array and then returns it, still empty.

To fix this, move the return statement down so that it is after the for loop (careful not to accidentally put it inside).

The second issue is how you’re using .push() inside the for loop.

Our for loop is looping through the hotelList array, which contains a bunch of objects representing hotels. It looks something like this:

[
    {
        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
    }
]

We want to look at each hotel in the array, and add its rating to the ratings array.

In the for loop, the looping variable element represents the current hotel as it loops through hotelList.

Therefore we want to add element.rating to the parentheses of .push(). Because we are pushing to the ratings array, we use ratings.push()

That will look like this:

ratings.push(element.rating)

Hope this helped! Good luck,
Ben