Skip to content

Instantly share code, notes, and snippets.

@merrickluo
Last active June 15, 2017 09:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save merrickluo/0dd244fdb0991029a4798e4c080e82b4 to your computer and use it in GitHub Desktop.
Save merrickluo/0dd244fdb0991029a4798e4c080e82b4 to your computer and use it in GitHub Desktop.
numbers
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