Skip to content

Instantly share code, notes, and snippets.

@keune
Last active August 29, 2015 13:58
Show Gist options
  • Save keune/9994677 to your computer and use it in GitHub Desktop.
Save keune/9994677 to your computer and use it in GitHub Desktop.
function findDigit(given) {
// log all the numbers in specified scope that contain a given digit
for (var i = 100; i >= -100; i--) {
var absI = Math.abs(i);
var numberOfDigits = Math.floor(Math.log(absI) / Math.log(10)) + 1;
var found = false,
subtract = 0;
while (numberOfDigits > 0) {
var x = Math.pow(10, --numberOfDigits);
var currentDigit = Math.floor((absI - subtract) / x);
subtract += (currentDigit * x);
if (currentDigit == given) {
found = true;
break;
}
}
if (found) console.log(i);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment