Skip to content

Instantly share code, notes, and snippets.

@klauszhang
Last active November 13, 2017 06:06
Show Gist options
  • Save klauszhang/532d0872007f1dcf9499431ec2998424 to your computer and use it in GitHub Desktop.
Save klauszhang/532d0872007f1dcf9499431ec2998424 to your computer and use it in GitHub Desktop.
a trail for code
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