Last active
November 13, 2017 06:06
-
-
Save klauszhang/532d0872007f1dcf9499431ec2998424 to your computer and use it in GitHub Desktop.
a trail for code
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
function resolve(howMany) { | |
const arr = new Set(); | |
// generate stuff | |
function getNumber() { | |
return Math.floor(Math.random() * 10000); | |
} | |
function getFourDigitNum() { | |
let pin; | |
do { | |
pin = getNumber(); | |
} while (pin <= 1000); | |
return pin; | |
} | |
while (arr.size <= howMany) { | |
let pin; | |
let stringArr; | |
let numArr; | |
let isValid; | |
do { | |
pin = getFourDigitNum(); | |
const tempArr = pin.toString().split(''); | |
stringArr = new Set(tempArr); | |
numArr = tempArr.map(parseInt); | |
isValid = | |
numArr[0] - numArr[1] !== 1 && | |
numArr[1] - numArr[2] !== 1 && | |
numArr[2] - numArr[3] !== 1; | |
} while (stringArr.size !== 4 && isValid); | |
arr.add(pin); | |
} | |
return arr; | |
} | |
console.log(Array.from(resolve(1000)).join(',')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment