Basic Algorithm Scripting - Boo who

Tell us what’s happening:
I don’t QUITE see where to go from here???
I know that its only comparing to boolArr[0] though
Your code so far

function booWho(bool) {
  let boolArr = [true , false]
  for (let i = 0; i < boolArr.length; i += 1){
    let truBool = boolArr[i];
    if(bool === truBool){
      return true
    }
    else if(bool!= truBool){
      return false
    }
  }

}

booWho(null);

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36

Challenge: Basic Algorithm Scripting - Boo who

Link to the challenge:

also i realize there are other ways to do this, but i WANTED to use a validating array type idea

You can use an array but you don’t need a for loop. Try using an if statement (with an else clause as needed).

do you mean like
if (bool=== boolArr[0] || bool === boolArr[1]) …
because i was hoping for the more… dynamic??? solution.

It is up to you if you wish to use a for loop to loop over two values.
Just don’t return early.

Or you have two values true and false. That screams a simple if statement to me.

ik i see that solution… and i appreciate the point your making… ig im trying to “prep for the bigger things”

like the kinda solution that is prepared to simple accomodate to accept a “third boolean” if the world ever presented it

That is fine. Make the if statement logic work. My hint is: use one if statement in your for loop.

Edit: never mind about the or operator

and when you said “don’t return early” i felt like that is what im doing but im struggling to see the rewrite in my head.

Put some console log statements in your else clause so you can see what I mean by returning early. Do a little experimenting. (Is that else clause really needed?)

all i’ve managed to successfully do was trim it down

function booWho(bool) {
  let boolArr = [true , false]
  for (let i = 0; i < boolArr.length; i += 1){
    let truBool = boolArr[i];
console.log("bool loop",  bool)
console.log("truBool loop", truBool)
    if(bool === truBool){
console.log("bool if",  bool)
console.log("truBool if", truBool)
      return true
    }
   return false 
   
  }

}

booWho(null);

i don’t quite understand; all its giving me is true and true … false @ boolArr[1] never gets touched
it never gets assigned as a value for truBool

also, only 1 test fails… when bool = false; it should return true; but it only checks it against the value at boolArr[0] = true; then it reverts to false instead of looping again to check boolArr[1]

i checked this by switching the positions of the booleans inside boolArr and seeing a very different test result

anyway i solved it the simple way

you are still returning too early in the for loop.

add a console.log just above the 2nd return and log the i value

It looks like you’re not understanding that return immediately halts execution of the function.

function example() {
  for (let i = 0; i < 10; i++) {
    return i;
    console.log(i);
  }
}

Can you tell me what gets logged to the console when running the function above? (this is a trick question)

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.