Boolean Gates in JavaScript



Problem

We are required to write a JavaScript function that takes in an array of Boolean values and a logical operator.

Our function should return a Boolean result based on sequentially applying the operator to the values in the array.

Example

Following is the code −

 Live Demo

const array = [true, true, false]; const op = 'AND'; function logicalCalc(array, op){    var result = array[0];    for(var i = 1; i < array.length; i++){       if(op == "AND"){          result = result && array[i];       }       if(op == "OR"){          result = result || array[i];       }       if(op == "XOR"){          result = result != array[i];       }    }    return result; } console.log(logicalCalc(array, op));

Output

false
Updated on: 2021-04-17T12:27:09+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements