Challenge 11 Megathread

Welcome to challenge #11!

Use this thread for any and all questions relating to challenge 11

3 Likes

Nice challenge… Learning and learning all these days…

4 Likes

This one is difficult! I keep getting that my object properties aren’t being updated :confused:

1 Like

Can you say where you’re having trouble? Perhaps post a bit of your code?

So this challenge I’m having trouble with my if statements for undefined properties of an object.

if (typeof date.bookDate === "undefined")

This seems appropriate for the cases where it doesn’t exist yet either.

2 Likes

undefined is a reserved word. When you put inside quotes you transformed it into a simple string.

4 Likes

I’ve since figured it out! But I’m still confused as to why my code did not previously work.

SPOILER ALERT! Code below.

Originally, I had written:

for (let i = 0; i < platformList.length; i++) {
        if (platformList[i].bookDate === undefined) {
            // update properties
        }
    }

However it only worked after I changed it to:

for (let obj in platformList) {
        if (platformList[obj].bookDate === undefined) {
            // update properties
        }
    }

Can you give me an idea of why the first example is incorrect? I can’t see where the issue would be.

1 Like

I think it is because the for - in loop is the main for loop for objects especially when you are trying to access its keys

I think it may be something else you had in the body where you have the comment // update properties

Both your loops currently behave the same way actually:

Also just wanted to point out that in your for in loop obj actually refers to the current list index itself

2 Likes

I agree. The first code does work…

1 Like

Interesting. I don’t think I changed any of the // update properties code other than to change if it was platformList[i].bookDate or platformList[obj].bookDate. Thanks for all the replies! I’ll have another look later and see if I can figure out what I changed lol. :]

1 Like

Not having used it before, the for in loop is… forigen to me.
:rofl:

3 Likes

You need to go out from the loop once you found the empty platform.

2 Likes

A little shorthand you might like for testing if a property is blank
if (!platformList.bookdate)

Banged my head for a while before I realized I need to avoid over-writing existing booked dates ^_^;

2 Likes

Should put the index
if (!platformList.bookdate) ==> if (!platformList[i].bookdate)

Didn’t work in my code :frowning_face:

Yep, you’ll want to include the index.

Also make sure to capitalize the D in bookDate

Just a heads up to anyone trying to use forEach, there is no proper way to exit the loop

There is no ‘break’ in forEach, but you can exit it by throwing an exception. Not ideal

Yeah, that is what I found out

This is my code and I do not know why it is not working because when I do console.log mission date I get the exact date. Please what am I doing wrong?