Skip to content

Instantly share code, notes, and snippets.

@rbrtsmith
Created January 30, 2016 11:41
Show Gist options
  • Save rbrtsmith/15774371c47e537cc0c7 to your computer and use it in GitHub Desktop.
Save rbrtsmith/15774371c47e537cc0c7 to your computer and use it in GitHub Desktop.
//Sample code to read in test cases:
var fs = require("fs");
fs.readFileSync(process.argv[2]).toString().split('\n').forEach(function (line) {
if (line !== "") {
//do something here
//console.log(answer_line);
var makeArray = function(number) {
return number
.toString()
.split('')
.map(function(val) {
return parseInt(val, 10);
});
};
var squareDigits = function(array) {
return array.reduce(function(acc, val) {
return acc + (val * val);
}, 0);
};
var happyNumber = function(number) {
while (number !== 1) {
number = squareDigits(makeArray(number));
}
return number === 1 ? 1 : 0;
};
console.log(happyNumber(line));
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment