Monday, July 12, 2021

Junior Developer Central JS Practice Exercise - 11

 
Question: 

Write a Javascript program to find the number of even digits in an array of integers. 




Answer: 

function evenArr (arr) {

let regArr = arr; 
let evenArrL = regArr.filter(i => i % 2 === 0).length; 
console.log(evenArrL); 

}




> we declare a function 'evenArr' 
> it has one parameter 'arr' 
> we create a variable 'regArr'
> it is initialized with the parameter 'arr' which is an array of integers
> we create another variable 'evenArrL'
> we initialize it with the value we get when we use the filter method on 'regArr' 
> we want to filter all the even numbers of the 'regArr' 
> to do that we use the modulo / remainder operator 
> however we only need to know the number of even digits 
> so we add the 'length' method to get the total number of even digits in the array
> finally we console.log our answer

No comments:

Post a Comment