Last active
June 15, 2017 09:25
-
-
Save merrickluo/0dd244fdb0991029a4798e4c080e82b4 to your computer and use it in GitHub Desktop.
numbers
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
const isAllzero = function (num) { | |
return (num.toString().replace(/0/g, '') === '1') | |
} | |
const nextNumber = function (current, limit) { | |
const longer = parseInt(`${current}0`) | |
if (longer <= limit) { | |
return longer | |
} | |
const bigger = current + 1 | |
if (isAllzero(bigger)) { | |
return undefined | |
} | |
if (bigger <= limit) { | |
return bigger | |
} | |
const shorter = Math.round(current / 10) | |
if (shorter !== 0 && (shorter + 1) <= limit) { | |
return shorter + 1 | |
} | |
return undefined | |
} | |
const output = [] | |
let num = 1 | |
while(num) { | |
output.push(num) | |
num = nextNumber(num, 23) | |
} | |
console.log(JSON.stringify(output)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment