Ian.M
February 25, 2023, 9:33pm
1
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:
Ian.M
February 25, 2023, 9:41pm
2
also i realize there are other ways to do this, but i WANTED to use a validating array type idea
hbar1st
February 25, 2023, 9:43pm
3
You can use an array but you don’t need a for loop. Try using an if statement (with an else clause as needed).
Ian.M
February 25, 2023, 9:45pm
4
do you mean like
if (bool=== boolArr[0] || bool === boolArr[1]) …
because i was hoping for the more… dynamic??? solution.
hbar1st
February 25, 2023, 9:46pm
5
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.
Ian.M
February 25, 2023, 9:47pm
6
ik i see that solution… and i appreciate the point your making… ig im trying to “prep for the bigger things”
Ian.M
February 25, 2023, 9:48pm
7
like the kinda solution that is prepared to simple accomodate to accept a “third boolean” if the world ever presented it
hbar1st
February 25, 2023, 9:49pm
8
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
Ian.M
February 25, 2023, 9:50pm
9
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.
hbar1st
February 25, 2023, 9:52pm
10
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?)
Ian.M
February 25, 2023, 10:03pm
11
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
Ian.M
February 25, 2023, 10:09pm
12
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
Ian.M
February 25, 2023, 10:21pm
13
anyway i solved it the simple way
hbar1st
February 25, 2023, 10:57pm
14
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)