Created
November 20, 2018 20:33
-
-
Save tuvo1106/2f6d5084e0ac2c505dbc6722c5446b6a to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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