The function you gave does not satisfy the challenge. Right shifting will not do what is asked for. For example, your notNotNot(6,true)
is false
, not true
when put through your function.
Your question is about bitwise operation on a boolean though. Since operators like >>
and <<
work on integers, Javascript first converts the boolean value to an integer. So true
becomes 1 and false
becomes 0. To see this you can shift by zero:
console.log("true is",true >> 0)
console.log("false is", false >> 0)
So bitwise operation on booleans is really just bitwise operation on either 0 or 1.
Using !!
is a handy way to convert anything into a boolean. It takes anything that would be considered equivalent to false (such as 0, null
, undefined
or “”) and gives back false
. Similarly anything that is truthy (like 14, “hello”, [4], {a:1}) and give back true
. !!
works because the first exclamation mark gives the ‘not’ of the expression which is always true
or false
, then the second exclamation mark gives the opposite of that (false
or true
).
Getting back to the challenge, it wants to apply the not-operator ‘a’ times and compare to the ‘b’ value. So something like this would work:
function notNotNot(a, b) { return !!(a%2 - b); }
console.log("notNotNot(1, true)",notNotNot(1, true));
console.log("notNotNot(2, false)",notNotNot(2, false));
console.log("notNotNot(6, true)",notNotNot(6, true));