Monday, July 12, 2021

Junior Developer Central JS Practice Exercise - 14

 
Question: 

Write a Javascript program to get the largest even number from an array of integers. 




Answer: 

function largestEven (arr) {
const newArr01 = arr; 
const newArr02 = newArr01.filter(i => i % 2 === 0); 
newArr02.sort((a,b) => b - a); 
console.log (newArr02[0]); 
}




> we declare a function 'largestEven' 
> it has one parameter 'arr' which is an array of numbers
> we create a variable 'newArr01' and initialize it with value of the 'arr'
> we create another variable 'newArr02' 
> we initialize it with a value we get by using the filter method & modulo of 2, on 'newArr01'
> we then sort the 'newArr02' variable in descending order
> we console.log the first index value of the 'newArr02' to get the largest even number

No comments:

Post a Comment