Skip to content

Instantly share code, notes, and snippets.

@tuvo1106
Created November 20, 2018 20:33
Show Gist options
  • Select an option

  • Save tuvo1106/2f6d5084e0ac2c505dbc6722c5446b6a to your computer and use it in GitHub Desktop.

Select an option

Save tuvo1106/2f6d5084e0ac2c505dbc6722c5446b6a to your computer and use it in GitHub Desktop.
const example = [1,2,5,7,9,11,13,17,19]
const example2 = [-2,5,8,14,1,5,-9,-10,2]
// imperative programming
avg = function(arr) {
let sum = 0
// iterate through array
for (let i = 0; i < arr.length; i++) {
// add each number to var sum
sum += (arr[i])
}
let length = arr.length
return sum/length
}
console.log(avg(example)) // 9.333
console.log(avg(example2)) // 1.555
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment