Find the OR and AND of Array elements using JavaScript
Given an array and the is task to find the OR and AND of the values of an Array using JavaScript.
Simple method: It uses a simple method to access the array elements by an index number and use the loop to find the OR and AND of values of an Array using JavaScript.
Example 1: This example uses a simple method to find the OR of Array elements using JavaScript.
html
< body style = "text-align:center;" > < h1 style = "color:green;" > GeeksForGeeks </ h1 > < h3 > How to Find the OR of Values of an Array in JavaScript? </ h3 > < h4 > ----Given Array----< br > [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] </ h4 > < button onclick = "myGeeks()" >Click</ button > < p id = "gfg" ></ p > < script > function OR(input) { if (toString.call(input) !== "[object Array]") return false; var total = Number(input[0]); for(var i=1;i< input.length ;i++) { if(isNaN(input[i])){ continue; } total |= Number(input[i]); } return total; } function myGeeks(item) { document.getElementById("gfg") .innerHTML = "----OR of Array----" + "<br>" + OR([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]); } </ script > </ body > |
Output:

OR and AND of Array elements
Example 2: This example uses a simple method to find the AND of Array elements using JavaScript.
html
< body style = "text-align:center;" > < h1 style = "color:green;" > GeeksForGeeks </ h1 > < h3 > How to Find the AND of Values of an Array in JavaScript? </ h3 > < h4 > ----Given Array---- < br > [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] </ h4 > < button onclick = "myGeeks()" >Click</ button > < p id = "gfg" ></ p > < script > function AND(input) { if (toString.call(input) !== "[object Array]") return false; var total = Number(input[0]); for(var i=1;i< input.length ;i++) { if(isNaN(input[i])){ continue; } total &= Number(input[i]); } return total; } function myGeeks(item) { document.getElementById("gfg") .innerHTML = "----AND of Array----" + "<br>" + AND([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]); } </ script > </ body > |
Output:

OR and AND of Array elements
Using reduce() method: The array reduce() method in JavaScript is used to reduce the array to a single value and executes a provided function for each value of the array (from left-to-right) and the return value of the function is stored in an accumulator. Syntax:
array.reduce( function( total, currentValue, currentIndex, arr ), initialValue )
Example 1: This example uses the array reduce() method to find the OR of values of an Array using JavaScript.
html
< body style = "text-align:center;" > < h1 style = "color:green;" > GeeksForGeeks </ h1 > < h3 > How to Find the OR of Values of an Array in JavaScript? </ h3 > < h4 > ----Given Array----< br > [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] </ h4 > < button onclick = "myGeeks()" > Click Here! </ button > < br >< br > OR: < span id = "GFG" ></ span > < script > var arr=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]; function ORofArray(OR, num) { return OR | num; } function myGeeks(item) { document.getElementById("GFG").innerHTML = arr.reduce(ORofArray); } </ script > </ body > |
Output:

OR and AND of Array elements
Example 2: This example uses the array reduce() method to find the AND of values of an Array using JavaScript.
html
< body style = "text-align:center;" > < h1 style = "color:green;" > GeeksForGeeks </ h1 > < h3 > How to Find the AND of Values of an Array in JavaScript? </ h3 > < h4 > ----Given Array----< br > [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] </ h4 > < button onclick = "myGeeks()" > Click Here! </ button > < br >< br > AND: < span id = "GFG" ></ span > < script > var arr=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]; function ANDofArray(AND, num) { return AND & num; } function myGeeks(item) { document.getElementById("GFG").innerHTML = arr.reduce(ANDofArray); } </ script > </ body > |
Output:

OR and AND of Array elements
Please Login to comment...