Skip to content

Instantly share code, notes, and snippets.

@shanimal
Last active September 24, 2016 07:48
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 shanimal/fe6ba5e9fb9ef7d2358ec27c3a62cfae to your computer and use it in GitHub Desktop.
Save shanimal/fe6ba5e9fb9ef7d2358ec27c3a62cfae to your computer and use it in GitHub Desktop.
Pick any number. Add the digits. Repeat until there is only one digit in your number. Multiply that by 5. Multiply that by 2. Subtract 2. Your number is 8.
/**
* Pick any number.
* Add the digits.
* Repeat until there is only one digit in your number.
* Multiply that by 5.
* Multiply that by 2.
* Subtract 2.
* Take the last digit in this number
* Your number is 8.
*/
var str = 'Nice trick. If you subtract 2 from any number that was multiplied by ten, it will end with an eight...';
var list = [];
var start = 0;
function reduceToOneDigit(num){
list.push(num)
num = num.toString().split('').reduce(function (e,v,i) {
return e + parseInt(v);
}, 0);
return num.toString().length === 1 ? num : reduceToOneDigit(num);
}
for(var i = 1; i < 100; i++) {
list.length = 0;
start = start + i * Math.floor(Math.random() * 100);
str += "\n";
var result = reduceToOneDigit(start);
str += list.join(' is ') + ' is ' + result + " and (" + result + " x 10 - 2 = " + (result * 10 - 2) + ")";
}
console.log(str);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment