-
Notifications
You must be signed in to change notification settings - Fork 105
/
Boolean logic from scratch.js
37 lines (34 loc) · 1.04 KB
/
Boolean logic from scratch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
Description:
Task
You need to implement two functions, xor and or, that replicate the behaviour of their respective operators:
xor = Takes 2 values and returns true if, and only if, one of them is truthy.
or = Takes 2 values and returns true if either one of them is truthy.
When doing so, you cannot use the or operator:
||
.
Input
Not all input will be booleans - there will be truthy and falsey values [the latter including also empty strings and empty arrays]
There will always be 2 values provided
Examples
xor(true, true) should return false
xor(false, true) should return true
or(true, false) should return true
or(false, false) should return false
*/
const or = (a, b) => {
a=Boolean(a);
b=Boolean(b);
if (a===false&&b==false) return false
if (a===true&&b==false) return true
if (a===false&&b==true) return true
if (a===true&&b==true) return true
};
const xor = (a, b) => {
a=Boolean(a);
b=Boolean(b);
if (a===false&&b==false) return false
if (a===true&&b==false) return true
if (a===false&&b==true) return true
if (a===true&&b==true) return false
};