Browse Definitions :

Funny JavaScript for programmers who love literature

Learn about the book 'If Hemingway Wrote JavaScript' by Angus Croll and how JavaScript gives programmers the ability to create interactive code.

What would historically famous authors think of coding languages, specifically JavaScript? Would Jane Austen write code with her usual snark and sarcasm? Would Charles Dickens program with his signature drama and sentimentality? Would James Joyce code with a sense of adventure and grandiosity?

For new application developers, JavaScript is one of the most in-demand programming languages to know. With the ability to implement logic and interactivity, it's become the most popular programming language for front-end development of websites and applications. Unlike HTML, JavaScript isn't static and enables developers to create complex scripting for browser-based programming.

In If Hemingway Wrote JavaScript, author Angus Croll said JavaScript has much in common with natural language. "It is at its most expressive when combining simple idioms in original ways; its syntax, which is limited yet flexible, promotes innovation without compromising readability. And, like natural language, it's ready to write," he wrote in the book.

The following excerpt from If Hemingway Wrote JavaScript looks at how two influential authors might have applied their unique voices and creativity to writing JavaScript. Each chapter provides an exercise in how a specific author would approach a problem, including the Fibonacci sequence, factorials and prime numbers.



The Fibonacci sequence is the series of numbers whereby each new number is the sum of the previous two. By convention, the first two numbers of the series are 0 and 1. These are the first 15 Fibonacci numbers:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377

Book cover of If Hemingway Wrote JavaScript.Purchase your copy of If
Hemingway Wrote
by clicking
on the image above.

The sequence is named for Leonardo Pisano (also known as -- wait for it -- Fibonacci), but in a more just world, it would be named the Pingala sequence, after the Sanskrit grammarian who documented it a thousand years earlier.

As we progress through the series, the ratio between successive numbers tends toward a constant (roughly 1.61803) known as the golden ratio. Some mathematically inclined flora arrange their branches or petals according to the golden ratio -- though its prevalence in nature is sometimes overstated.

Ernest Hemingway (1899-1961)

Ernest Hemingway's work is characterized by direct, uncomplicated prose and a lack of artifice. In his fiction, he describes only the tangible truths: dialog, action, superficial traits. He does not attempt to explain emotion; he leaves it alone. This is not because Hemingway doesn't want his stories to convey feeling -- quite the opposite: his intent is to create a vacuum so that it might be filled by the reader's own experience. After all, emotion is more easily felt than described with words:

I have tried to eliminate everything unnecessary to conveying experience to the reader so that after he or she has read something it will become a part of his or her experience and seem actually to have happened.

Hemingway's prose is never showy, and his syntax is almost obsessively conventional. The short, unchallenging sentences and absence of difficult words add a childlike quality to his cadence. He assumes the role of naive observer, all the better to draw his readers into the emotional chaos beneath.

1 function fibonacci(size) {
3     var first = 0, second = 1, next, count = 2, result = [first, second];
5     if (size < 2)
6           return "the request was made but it was not good"
8     while (count++ < size) {
9           next = first + second;
10          first = second;
11          second = next;
12          result.push(next);
13    }
15    return result;
16 }

The Hemingway paradox is, to some extent, the JavaScript paradox. Just as Hemingway uses only the sparest prose to allow the intricacies of the human condition to surface, JavaScript's terse and direct syntax, when used well, can crystallize complex logic into something tangible and immediate.

Hemingway's Fibonacci solution is code reduced to its essentials, with no word or variable wasted. It's not fancy -- maybe it's even a little pedantic -- but that's the beauty of Hemingway's writing. There's no need for elaborate logic or showy variable names. Hemingway's Java Script is plain and clear, and it does only what is necessary -- and then it gets out of the way to allow the full glory of the Fibonacci sequence to shine through.

Hemingway didn't suffer fools gladly, so if you ask for a series with fewer than two numbers, he'll just ignore you or complain, "I'm tired and this question is idiotic."

William Shakespeare (1564-1616)

In stark contrast to Hemingway's hands-off approach, William Shakespeare probes the human psyche to the fullest. In wondrously expressive verse, he maps the dark crevices of his protagonists and lays bare their souls. Shakespeare's commentary is universal because he recognizes in his subjects those archetypal traits that transcend geography and time.

Shakespeare's plays and sonnets make heavy use of iambic pentameter, which was the popular lyrical form of his time. A foot is a metrical unit consisting of a stressed syllable and one or more unstressed syllables, and an iamb is a two-syllable foot with the second syllable stressed (for example, "reVIEW" or "the CAT"). An iambic pentameter is 5 iambs in a row -- 10 syllables with stresses on the even-numbered syllables.

Here's a simple couplet in iambic pentameter taken from Shakespeare's "Sonnet 18." Stressed syllables are capitalized:


So LONG lives THIS, and THIS gives LIFE to THEE.

Shakespeare often adds dramatic emphasis by deviating from strict iambic pentameter -- he might add an extra syllable or use an alternate stress. In the famous opening line of Richard III, the stress of the first foot is reversed (a trochee), highlighting the urgency of "now."

NOW is the WINter OF our DISconTENT

1 function theSeriesOfFIBONACCI(theSize) {
3     //a CALCKULATION in two acts
4     //employ'ng the humourous logick of JAVA-SCRIPTE
6     //Dramatis Personae
7     var theResult; //an ARRAY to contain THE NUMBERS
8     var theCounter; //a NUMBER, serv'nt to the FOR LOOP
10    //ACT I: in which a ZERO is added for INITIATION
12    //[ENTER: theResult]
14    //Upon the noble list bestow a zero
15    var theResult = [0];
17    //ACT II: a LOOP in which the final TWO NUMBERS are QUEREED and SUMM'D
19    //[ENTER: theCounter]
21    //Commence at one and venture o'er the numbers
22    for (theCounter = 1; theCounter < theSize; theCounter++) {
23          //By divination set adjoining members
24          theResult[theCounter] = (theResult[theCounter-1] || 1) +
25                theResult[Math.max(0, theCounter-2)];
26    }
28    //'Tis done, and here's the answer
29    return theResult;
31    //[Exeunt]
32 }

Shakespeare's solution comes in the form of a two-act comedy that draws heavily on JavaScript's unusual mannerisms for levity. We're introduced to the cast of players before settling in for the main event. In keeping with the traditions of Elizabethan comedy, the unsettling opening act (in which an incomplete result is prematurely presented) is happily resolved by the final act, affording us much comfort and cheer.

The Bard gets a little wordy, but we wouldn't have it any other way. Several clever devices are employed -- for example, the use of Math.max ensures that theResult does not suffer the indignity of being addressed by a negative index.

Notice that although Shakespeare's comments are in iambic pentameter, he's using weak endings (that is, adding an extra unstressed syllable). Shakespeare frequently used weak endings to denote enquiry or uncertainty (the Elizabethan equivalent of upspeak). We can only assume he found JavaScript as vexing as the rest of us do.

Next Steps

Humorous AI is a riddle worth solving

Humor in marketing can engage, retain customers

Dig Deeper on Programming

  • Telnet

    Telnet is a network protocol used to virtually access a computer and provide a two-way, collaborative and text-based ...

  • big-endian and little-endian

    The term endianness describes the order in which computer memory stores a sequence of bytes.

  • Address Resolution Protocol (ARP)

    Address Resolution Protocol (ARP) is a protocol that maps dynamic IP addresses to permanent physical machine addresses in a local...

  • Mitre ATT&CK framework

    The Mitre ATT&CK (pronounced miter attack) framework is a free, globally accessible knowledge base that describes the latest ...

  • timing attack

    A timing attack is a type of side-channel attack that exploits the amount of time a computer process runs to gain knowledge about...

  • privileged identity management (PIM)

    Privileged identity management (PIM) is the monitoring and protection of superuser accounts that hold expanded access to an ...

  • employee resource group (ERG)

    An employee resource group is a workplace club or more formally realized affinity group organized around a shared interest or ...

  • employee training and development

    Employee training and development is a set of activities and programs designed to enhance the knowledge, skills and abilities of ...

  • employee sentiment analysis

    Employee sentiment analysis is the use of natural language processing and other AI techniques to automatically analyze employee ...

Customer Experience
  • customer profiling

    Customer profiling is the detailed and systematic process of constructing a clear portrait of a company's ideal customer by ...

  • customer insight (consumer insight)

    Customer insight, also known as consumer insight, is the understanding and interpretation of customer data, behaviors and ...

  • buyer persona

    A buyer persona is a composite representation of a specific type of customer in a market segment.