Object lookup
over multiple if
conditions
// Guard Clauses
function logicalCalc(array, operation) {
let result = array[0];
for(let i = 1; i < array.length; i++) {
if(operation === 'AND') {
result = result && array[i];
}
if(operation === 'OR') {
result = result || array[i];
}
if(operation === 'XOR') {
result = result !== array[i];
}
}
return result;
}
logicalCalc([false, true, 3], 'AND');
// Object Key Lookup
const operations = {
AND: (a, b) => a && b,
OR: (a, b) => a || b,
XOR: (a, b) => a !== b,
};
const logicalCalc = (array, options) => array.reduce(operations[options]);
logicalCalc([1,2,3], 'AND');