Last active
August 29, 2015 13:58
-
-
Save keune/9994677 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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