Execute digits in even places in a JavaScript array?


In this article, the given task is to execute digits in even places in a JavaScript array with suitable examples.

To execute the digits in even places in a JavaScript array, we need to get the starting index of the array and we need to loop through it by checking whether the index is at odd place or even place.

Using the filter() Method: This method will create a new array filled with elements that pass a test provided by a specified function. This method doesn’t execute the function for empty elements and this method do not modify the original array.

Syntax

The syntax for the filter() method with an arrow function is −

filter((element) => { /* statement*/ } );
filter((element, index) => { /* statement */ } );
filter((element, index, array) => { /* statement */ } );
or
array.filter(((element, index) => { /* statement */ } );

Where,

  • element is the element being processed.

  • index is the index being processed.

  • array is name of the array.

Example 1

This is an example program to get the even places of an array in JavaScript using filter() method.

<!DOCTYPE html>
<html>
<head>
   <title>Execute digits in even places in a JavaScript array?</title>
</head>
<body>
   <p id="even" style="text-align : center"></p>
   <script>
      var arr = [1,2,3,4,5,6,7,8,9,10];
      let even_places = arr.filter((a, i) => i % 2 === 1);
      document.getElementById('even').innerHTML = 'The even elements in the array are : ' + even_places;
   </script>
</body>
</html>

On executing the above code, the following output is generated.

Example 2

This is an example program to get the even places of an array by checking whether the index is even or not. If the index is even, we push the element into a new array.

<!DOCTYPE html>
<html>
<head>
   <title>Execute digits in even places in a JavaScript array?</title>
</head>
<body>
   <p id="even" style="text-align : center"></p>
   <script>
      var arr = [10, 20, 30, 40, 50, 60, 70, 80];
      var new_arr = [];
      function get_evenPlaces(arr) {
         for (var i = 0; i < arr.length; i++) {
            if ((i + 1) % 2 === 0) { // Index place is even
            new_arr.push(arr[i]);
         }
      }
      return new_arr;
      }
      document.getElementById('even').innerHTML = 'The even elements in the array are : ' + get_evenPlaces(arr);
   </script>
</body>
</html>

On executing the above code, the following output is generated.

Example 3

This is an example program to get the even places of an array by strating from the second element and increment the index to next even position.

<!DOCTYPE html>
<html>
<head>
   <title>Execute digits in even places in a JavaScript array?</title>
</head>
<body>
   <p id="even" style="text-align : center"></p>
   <script>
      var arr = [10, 20, 30, 40, 50, 60, 70, 80];
      var new_arr = [];
      function get_evenPlaces(arr) {
         for (var i = 1; i < arr.length; i += 2) {
            // Start from the second element and increment the index to next even position
            new_arr.push(arr[i]);
         }
         return new_arr;
      }
      document.getElementById('even').innerHTML = 'The even elements in the array are : ' + get_evenPlaces(arr);
   </script>
</body>
</html>

On executing the above code, the following output is generated.

Updated on: 09-Dec-2022

88 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements