Returning array values that are not odd in JavaScript

JavascriptWeb DevelopmentFront End Technology

Problem

We are required to write a JavaScript function that takes in an array of numbers.

Our function should construct and return a new array that contains all the numbers of the input array that are not odd.

Example

Following is the code −

 Live Demo

const arr = [5, 32, 67, 23, 55, 44, 23, 12];
const findNonOdd = (arr = []) => {
   const res = [];
   for(let i = 0; i < arr.length; i++){
      const el = arr[i];
      if(el % 2 !== 1){
         res.push(el);
         continue;
      };
   };
   return res;
};
console.log(findNonOdd(arr));

Output

[ 32, 44, 12 ]
raja
Published on 19-Apr-2021 11:40:57
Advertisements