Salinger, Nabokov and Tupac do JavaScript

So anyway, I asked J.D. Salinger, Geoffrey Chaucer, Tupac Shakir, Virginia Woolf and Vladimir Nabokov to solve the happy numbers problem in JavaScript. Amazingly they all responded. Here’s how they did…

The Problem

A happy number is one which, when repeatedly replaced by the sum of the squares of its digits, will tend towards 1. By way of reference here’s a bog-standard solution:

function isHappy(n) { // to avoid infinite looping, keep a history of numbers already visited var history = {'1': true}, nextNum; while( !history.hasOwnProperty(nextNum) ) { nextNum = 0; // prepare to calculate the next number in the series history[n] = true; // split the number into digits and square each one n.toString().split('').map(function(digit) { return digit * digit;...

Continue reading