Created
December 25, 2018 13:08
-
-
Save stelf/05c5ef4c75fc76b6db5b3a302565c349 to your computer and use it in GitHub Desktop.
100 random.org nums
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 http = require('https') | |
| const querystring = require('querystring') | |
| const adr = 'https://www.random.org/integers/?'; | |
| const qry = querystring.stringify({ | |
| num: 100, | |
| min: 1, | |
| max: 100, | |
| col: 1, | |
| base: 10, | |
| format: 'plain', | |
| rnd: 'new' | |
| }) | |
| function loadNums() { | |
| let nums = [] | |
| return new Promise(( resolve, reject) => { | |
| let req = http.get(adr + qry, res => { | |
| res.on('data', d => { | |
| nums.push( ... | |
| d.toString(). | |
| split(/\n/). | |
| map( n => parseInt(n)). | |
| filter(n => n) | |
| ) | |
| }) | |
| res.on('end' , () => { | |
| console.log(nums.length + ' numbers read') | |
| resolve(nums) | |
| }) | |
| }) | |
| } | |
| ) | |
| } | |
| loadNums().then( nums => { | |
| let sum = nums.reduce( (acc, n) => n + acc) | |
| console.log('the sum of numbers is ' + sum) | |
| console.log('average of numbers is ' + sum/nums.length) | |
| }).catch( e => console.error(e)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment