Created
August 11, 2018 18:35
-
-
Save niradler/b8926fd15bce1ac5e98e1efde163d047 to your computer and use it in GitHub Desktop.
find the missing number 1- 1000
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
// find the missing number 1- 1000 | |
const min =0, max=1000; | |
let arr = new Array(max).fill(0).map((v,k)=> k); | |
const erase = Math.floor(Math.random() * (max - min + 1)) + min; | |
arr.splice(erase,1); | |
const findMissing = (arr) => { | |
const expected_sum = new Array(max).fill(0).map((v,k)=> k).reduce((sum,i) => sum + i); | |
const sum = arr.reduce((sum,i) => sum + i); | |
return expected_sum - sum; | |
} | |
const res = findMissing(arr); | |
console.log(res == erase,res, erase); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment