Created
January 30, 2016 11:41
-
-
Save rbrtsmith/15774371c47e537cc0c7 to your computer and use it in GitHub Desktop.
This file contains 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
//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