Salinger, Nabokov and Tupac do JavaScript

(New Book “If Hemingway wrote JavaScript” out in September 2014.
Pre-order your copy here:

So anyway, I asked J.D. Salinger, Geoffrey Chaucer, Tupac Shakur, 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...

Continue reading