Challenge 20 Megathread

Three things that I can see:

  1. if(mission.length === checks.dataEntries) mission.length is an integer, which is good. But checks.dataEnteries is the entire object, which is bad. You need to access one of the values in dataEnteries instead.

  2. mission.length is actually the wrong integer, because mission is the property key as a string, so you’re coming up with how many characters are in the string. You need to access the value of the property instead.

  3. Your for loop and if statement seem to be structured such that if any of the tests pass, you return true and the function stops executing. You won’t return false if only one of the tests fail.

2 Likes

“The return statement ends function execution and specifies a value to be returned to the function caller.”

That’s the first line from the MDN docs which, according to this challenge, is incorrect?

Why is forEach() an exception?

It is correct, but you might be misunderstanding it. You have a function nested within your function. The syntax forEach(el => { is beginning a new function. Your return false is exiting that new function but the rest of the parent function will continue executing until you get to return true at the end.

It isn’t really that forEach() is an exception, it’s that => creates a new function, and that’s the one being exited.

2 Likes

Are “methods” different from “functions” or something? Because I’m still not getting it.

Methods are basically functions that exist within an object. Functionally, they’re the same idea.

Another way of stating your problem is that your parent fuction doesn’t know what’s happening with your forEach loop function. It doesn’t know the result, so it just keeps executing until return true.

Your problem could be illustrated this way without using an arrow function:

function nestedFunction() {
    //Do some tests, pretend they fail and return false.
    return false;
}

function parentFunction() {
    nestedFunction();
    //What happened in nestedFunction()? I don't know, I didn't save the return value anywhere. Let's just return true.
    return true;
}

parentFunction() will always return true because even though it is executing nestedFunction, the return false is only exiting the nested function and it’s return value isn’t being evaluated anywhere inside the parent function.

2 Likes

Adding to the great explanation by Myles, if you’re still not sure about to fix the problem, I would recommend switch to a for...of loop instead.

4 Likes

Or for...in is what I used

for...of returns an error missionData is not iterable :man_shrugging:

Yep! You can use for…of with object.keys though!

2 Likes

Ah, interesting. I didn’t know that. Thanks!

Thanks @MylesM and @LHL-Team for the help. Still doesn’t make sense to me so I just made a variable to return at the end. Set it to true by default and will be set to false if any conditions are met. Working now.

I’ll look into for...of and for...in after I go for a run.

Hi I’m wondering if I do this:
let type = Object.keys(missionData) // to get an array: type = [“astro”, “bio”, “physics”]
type[0] is “astro”
would it be possible to use type[0], missionData.type[0].length ← this doesn’t work
so the outcome can be equivalent to missionData.astro.length ?
Thank you :slight_smile:

You might try missionData[type[0]].length

1 Like
if(speed <= checks.maxSpeed || speed >= checks.minSpeed){
      for (const mission in missionData){
        for (const value of missionData[mission]){
        if(value.length === checks.dataEntries){
          console.log(value.length);
          return true;
        }
        }
      }
    } 
      return false;

Now I can access the values in it but something is still wrong with the second If value. Please I don’t know what is wrong. I don’t know what I am missing tbvh

Try console.log(checks.dataEntries) as well to see what the if statement is actually comparing against

The code is not working inside the second IF statement. Everything in there including the console.log is not working.

The thing is when I console. log outside of the if statement the value .length gives me numbers and checks.dataentries gives me an object. when I tried to loop through the data entries it is not working either. I don’t know

You don’t really need to loop through checks.dataEntries, you just need to compare to a property of checks.dataEntries instead of comparing to the entire object

if(missionData[mission].length === checks.dataEntries.astro){
          console.log(checks.dataEntries.astro);
          return true;

This does not work.

You have an additional problem with your logic. Your code will return true if any comparison in the loop passes, even if another one failed. You need to return true only if all comparisons passed

if(speed <= checks.maxSpeed || speed >= checks.minSpeed){
      for (const mission in missionData){
          console.log(checks.dataEntries.astro)
        if(missionData[mission].length === checks.dataEntries.astro){
          console.log(checks.dataEntries.astro);
          return true;
          
        }
      }
    } 
      return false;

I don’t know y’all everything looks right to me. Any explanation would be greatly appreciated.


its only passing one test and not return the proper result. any idea where i went wrong