Challenge 11 Megathread

It looks like there may be two issues:

  1. your code is adding missionDate to undefined instead of setting the bookDate. The issue with this is undefined + "someText" = "undefinedsomeText"

  2. your code will replace every single bookDate that is undefined. If you take a look back at the sample output you’ll notice that only the first undefined bookDate should be replaced with the missionDate

Hope that helps

1 Like

Thank you, I think I get it

1 Like

Don’t do += you don’t want to add a number to the total value you want to assign the value of .bookDate to whatever missionDate is, so you just need =
also the return statement should be in the if statement because once you find the first undefined value you set it and leave. They ask you to just do the first one.

2 Likes

I have a little question here. Why it doesn’t work if I move the return platformList like this:

> for (let i = 0; i < platformList.length; i++) {
        if (platformList[i].bookDate === undefined) {
            platformList[i].bookDate = missionDate;
        }
    }
  return platformList;

Because that would be an else statement. So, what you are saying is if bookDate is undefined then bookDate is equals to mission date ELSE return the array.

2 Likes

Got it, thank you for explaining this! :wink:

1 Like

When you return outside of the loop the loop will continue and the next undefined bookDate will be set to missionDate. But the challenge only wants the first undefined bookDate to be updated.

If you return platformList in the loop right after you update the bookDate the loop won’t continue and only the first undefined bookDate is updated.

3 Likes

Because you’re going through the entire list and changing every .bookDate that has an undefined value. This current task only requires you to do the first one so by putting they return statement within the ig statement the moment you find and set the first undefined value it returns and exits the function.

Hey twcheung36, great question! I know some people have replied, so feel free to ignore this if you already understand the concept :slightly_smiling_face:

It is because you’re now changing the scope of what you are trying to return. If you move return platformList then you are waiting for the loop to apply that condition to every instance in its cycle and then return the value. In this case it is checking every instance of bookDate to see if it is undefined. If it is undefined, then set the bookDate to missionDate. By having the return statement after one if statement is met, you stop the loop and return platformList without updating every instance of undefined

Please feel free to ask if you need any more clarification.

Best of luck and happy coding,
Luke

3 Likes

For anyone who is stuck or needs a hint, the array helper function called ‘find’ does exactly what this questions asks for. It basically will return the first element that meets the condition that you specify, if no elements meet the condition then it will just return null.

3 Likes

It will work if you put " break;" inside if statement.

2 Likes

Thanks a bunch @Rofiat. I really struggle with which brackets to use when and where. Your explanation really helped.

1 Like

For anyone looking for an alternative to a traditional for loop, take a look at findIndex, which in most cases will be faster (at scale that will matter).

4 Likes

Agreed. Remember to break from the loop once you found the first empty plattform.

2 Likes

using findIndex it will only iterate until it finds the first index of what you are looking for and then return that to you. Thus bringing down your time complexity and not iterating through the entire array see the link that @MichaelBourne posted for more detail on syntax and functionality.

I really like the broader context you provide - like what kinds of things matter in “real-life programming”, such as speed at larger scales.

But can you please explain how this runs faster (at scale)? I assume the program still loops through each iteration of the array in order, checking for the first time the test is passed (such as bookDate being undefined). It then returns the array index number, which might be faster than returning the desired element - I guess that probably depends on how the data is stored.

But even if returning the index number is faster, the code then has to access that data again to pull the desired data (bookDate), so it can be updated.

I can certainly see array.prototype.findIndex() being faster than running every iteration of a loop (especially over a long array), but for something like this, we aren’t finishing the loop either way - just jumping out after finding the first “right” datum.

I’m not contesting your assertion, just asking for a deeper explanation, so I understand better. Would you please explain how/when it is faster (better) to use findIndex?

1 Like

My understanding is that the the built in javascript methods operate at a lower level, or native code rather than the for loop runs at what ever speed the language interpreter reads at. so you are limited by that. Poke around …here… for some deep reading into ECMAScript which is the real name of JavaScript.

1 Like

I’m still unclear as to why this code doesn’t pass both tests. The return is nested within the if statement so shouldn’t it update the first available bookDate and immediately break out of the loop?

Thank you! Your explanation makes sense to me, and I really appreciate it. I feel seriously out of my depth looking at the github you linked, but you basic answer is really helpful, and maybe I can work up to interpreting the documentation.

I got an issue which I feel like there was something I did wrong in my code.
From the instructions, what I understand is that if there is an opening for a platform, book the date for that platform and then stops (essentially returns the array with the platform bookDate updated)

So the first test it runs, Platform A and Platform C has a date booked, thus needing to “book” a date on the available Platform B

Second test that runs, only Platform A has a date booked, so Platform B and C are free, but only need to book one Platform for the mission date and leave Platform C unbooked.

I ran a for … in loop, then did an if statement to find the undefined value, then assigned the missionDate to said index value.
And then I return the array.

When I test my code and it passed all conditions but, it also booked the missionDate for Platform C when it ran the second test.

Here’s my output:

Running test: The bookFreePlatform function should return an array

Index, 0

Platform already booked

Index, 1

Platform is open for booking, null

What is the date to be booked, 2021-12-12

What is the data for of, [{“name”:“Platform A”,“bookDate”:“2021-12-11”},{“name”:“Platform B”,“bookDate”:“2021-12-12”},{“name”:“Platform C”,“bookDate”:“2022-01-12”}]

Running test: The bookFreePlatform function should book one platform

Index, 0

Platform already booked

Index, 1

Platform is open for booking, null

What is the date to be booked, 2021-12-12

What is the data for of, [{“name”:“Platform A”,“bookDate”:“2021-12-11”},{“name”:“Platform B”,“bookDate”:“2021-12-12”},{“name”:“Platform C”}]

Index, 0

Platform already booked

Index, 1

Platform already booked

Index, 2

Platform is open for booking, null

What is the date to be booked, 2021-12-12

What is the data for of, [{“name”:“Platform A”,“bookDate”:“2021-12-11”},{“name”:“Platform B”,“bookDate”:“2022-12-12”},{“name”:“Platform C”,“bookDate”:“2021-12-12”}]