Skip to content

Instantly share code, notes, and snippets.

@k0d3d
Last active January 27, 2022 20:48
Show Gist options
  • Save k0d3d/79acf90cde22e59686d8227289f4fb8c to your computer and use it in GitHub Desktop.
Save k0d3d/79acf90cde22e59686d8227289f4fb8c to your computer and use it in GitHub Desktop.
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