- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 −
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
Advertisements