Home  >  Article  >  Web Front-end  >  Find OR and AND of array elements using JavaScript

Find OR and AND of array elements using JavaScript

WBOY
WBOYforward
2023-08-25 17:25:13809browse

使用 JavaScript 查找数组元素的 OR 和 AND

In JavaScript, we can use the "&&" operator to perform an AND operation between two elements, and the "||" operator to perform an OR operation between two elements. .

Here we will learn to perform OR and AND operations between each element of an array.

Use for loop

We can use the for loop to iterate the array elements and perform OR operation on the array elements and the result elements.

grammar

Users can use the for loop to perform OR or AND operations on each element according to the following syntax.

for () {
   result = result || array[i];
}

In the above syntax, "result" is the result value of the OR operation of the first i-1 elements.

Example 1

In the example below, we create an array of numbers. After that, we call the PerformOR() function by passing the array and its length as parameters.

We define the "result" variable and initialize it to zero. After that, we use a for loop to iterate over the array elements. In the for loop, we perform an OR operation on the array elements and the value of the "result" variable.

In the output, the user can observe the result value of the OR operation on all array elements.

<html>
<body>
   <div id = "output"> </div>
   <script>
      let output = document.getElementById('output');
      function performOR(array, n) {
         let result = 0;
         for (let i = 0; i < n; i++) {
            result = result || array[i];
         }
         return result;
      }
      let numbers = [1, 2, 3, 4, 5, 6, 7, 8];
      output.innerHTML += "Array: "+ JSON.stringify(numbers);
      output.innerHTML += "<br>"
      output.innerHTML += "Result of OR Operation of all elements of above array: " + performOR(numbers, numbers.length);
   </script>
</body>
</html>

Example 2

In the example below, we perform an AND operation on each array element. First, we initialize the result variable with the first array element. After that, we use a for loop to iterate from the second array element and AND the array element with the value of the result variable.

<html>
<body>
   <h3> Using the <i> for loop </i> to perform AND operation for all array elements in JavaScript </h3>
   <div id = "output"> </div>
   <script>
      let output = document.getElementById('output');
      function performAND(array, n) {
         let result = array[0];
         for (let i = 1; i < n; i++) {
            result = result && array[i];
         }
         return result;
      }
      let num = [10, 30, 20, 40, 50];
      output.innerHTML += "Array: "+ JSON.stringify(num );
      output.innerHTML += "<br>"
      output.innerHTML += "Result of AND operation of all elements of the above array:  " + performAND(num, num.length);
   </script>
</body>
</html>

Using While Loop

While loops are also useful for iterating over arrays of numbers like a for loop. When iterating over array elements, we can perform AND or OR operations on individual array elements with the result variable.

grammar

Users can use the while loop to find the OR and AND of array elements according to the following syntax.

while (n--) {
   result = result && array[n];
}

In the above syntax, the result variable contains the result value of the OR and AND operations of the array elements.

Example 3

In the following example, we use a while loop and the "&&" operator to find the AND of all array elements. We initialize the result variable with the last element of the array. After that, we decrement the value of n by 1 on each iteration and AND the array elements with the value of the result variable.

<html>
<body>
   <h3> Using the <i> while loop </i> to perform AND operation for all array elements in JavaScript </h3>
   <div id = "output"> </div>
   <script>
      let output = document.getElementById('output');
      function performAND(array, n) {
         let result = array[n];
         while (n--) {
            result = result && array[n];
         }
         return result;
      }
      let array = [2, 3];
      output.innerHTML += "The AND operation of all elements of " + array + " is  " + performAND(array, array.length - 1);
   </script>
</body>
</html>

Use Array.reduce() method

array.reduce() Reduces an array to a single result value. Here we can reduce the array so that we can get the AND or OR of all the array elements.

grammar

Users can use the array.reduce() method according to the following syntax to find the AND or OR of all array elements.

array.reduce((res, current) => res || current, 0);

Example 4

In the following example, the performOR() function returns the OR of all array elements. In the function, we have used the reduce() method to reduce the array and OR all the elements.

In the output, the user can observe the OR result values ​​of all elements.



   

Using the array.reduce() method to perform OR operation for all array elements in JavaScript

<script> let output = document.getElementById('output'); function performOR(array) { return array.reduce((res, current) =&gt; res || current, 0); } let num = [10, 30, 20, 40, 50]; output.innerHTML += "The AND operation of all elements of " + JSON.stringify(num) + " is " + performOR(num); </script>

Example 5

In this example, we use the array.reduce() method to perform an AND operation on all array elements. We pass the callback function as the first parameter of the array.reduce() method and the first array element as the second parameter, representing the initial value of the "res" variable. The 'res' variable contains the final value of the AND operation of the array elements.

<html>
<body>
   <h3> Using the <i> array.reduce() </i> method to perform AND operation for all array elements in JavaScript </h3>
   <div id = "output"> </div>
   <script>
      let output = document.getElementById('output');
      function performAND(array) {
         return array.reduce((res, current) => res && current, array[0]);
      }
      let num = [4, 5];
      output.innerHTML += "The AND operation of all elements of " + JSON.stringify(num) + " is  " + performAND(num);
   </script>
</body>
</html>

Conclusion

The user learned to find the AND or OR of array elements. In the first method we used for loop and in the second method we used while loop. In the third approach, we used the array.reduce() method, which is more readable code.

The above is the detailed content of Find OR and AND of array elements using JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete