Last active
January 27, 2022 20:48
-
-
Save k0d3d/79acf90cde22e59686d8227289f4fb8c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const arrayOfEvenNumbers = (numbers) => { | |
const arrayForEventualResults = [] | |
for (let index = numbers.length - 1; index >= 0; index--) { | |
// check if this is an even index | |
if (index % 2 === 0) { | |
// check if the number at this index is evem | |
const numberAtCurrentIndex = numbers[index] | |
if (numberAtCurrentIndex % 2 === 0) { | |
// add the number to the `arrayForEventualResults` | |
arrayForEventualResults.push(numberAtCurrentIndex) | |
} | |
} | |
} | |
// after the above transversal, | |
// find the average /mean | |
console.log(arrayForEventualResults) | |
return arrayForEventualResults.reduce((a, b) => a + b, 0) / arrayForEventualResults.length | |
} | |
console.log('input 11, 21, 32, 34, 36, 37, 41, 42, 52 -> output average ->', arrayOfEvenNumbers([11, 21, 32, 34, 36, 37, 41, 42, 52])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment