CodeEval Solution in Javascript