Thursday, May 6, 2021

Javascript Practice 39: JS Hero - NOR

 
Question: 

Write a function nor that takes two Boolean values. If both values are false, the result should be true. In the other cases the return should be false.

I.e.: The call nor(false, false) should return true. The calls nor(true, false), nor(false, true) and nor(true, true) should return false.




Answer: 

function nor (boo1, boo2) {

let or = boo1 || boo2; 

return !or; 

}; 

> in the last post, I coded a NAND Gate (not and, !and) 
> in this post, I will code a NOR Gate (not or, !or)
> we declare a variable or
> we initialize it with a value of both our Boolean parameters
> like this boo1 || boo2
> so if boo1 or boo2, any one of them is true then variable 'or' will store the value true and in all other cases it will store the value of false
>> nor(true, false) = true
>> nor(false, true) = true
>> nor(true, true) = true
>> nor(false, false) = false
> but we need nor(false, false) to return true
> so instead of 'or' if we return '!or' (not or) then we shall get the value false

No comments:

Post a Comment