Skip to content

Instantly share code, notes, and snippets.

@echoeyecodes
Created November 13, 2019 07:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save echoeyecodes/0f6ed8d78219584a3c46b52f1843b679 to your computer and use it in GitHub Desktop.
Save echoeyecodes/0f6ed8d78219584a3c46b52f1843b679 to your computer and use it in GitHub Desktop.
function logicGate(type, inputs){
let value
switch (type.toUpperCase()){
case 'AND':
//Output is true if all inputs are true, otherwise it's false
value =inputs.find(item => item === 0)
console.log(value === 0 ? false : true)
return
case 'OR':
//Output is true if at least one of the input is true
value = inputs.find(item => item === 1)
console.log(value === 1 ? true : false)
return
case 'NOT':
//Has only one input and the output is the negation of the input
value = inputs[0]
console.log(value === 1 ? false : true)
return
case 'NAND':
//Output is false if and only if all inputs are true
value = inputs.every(item => item === 1)
console.log(!value)
return
case 'NOR':
//Output is the negation of the OR value
value = inputs.find(item => item === 1)
console.log(value === 1 ? false : true)
return
case 'XOR':
//Output is true if and only if exactly one input is true. Otherwise, it's false
value = inputs.filter(item => item ===1)
console.log(value.length > 1 || value.length === 0 ? false : true)
return
case 'XNOR':
// Output is true if and only if all the inputs are of the same value
function checkTrueOrFalse(factor = 0, count = 1){
value = inputs.every(item => item === factor)
value || count === 2 ? console.log(value) : checkTrueOrFalse(1, 2)
}
checkTrueOrFalse()
return
default:
console.error('Invalid gate. Use any of "AND, OR, NOT, NAND, NOR, XOR, XNOR" ')
}
}
logicGate('XOR', [0,0,0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment