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

  • SD-WAN security

    SD-WAN security refers to the practices, protocols and technologies protecting data and resources transmitted across ...

  • net neutrality

    Net neutrality is the concept of an open, equal internet for everyone, regardless of content consumed or the device, application ...

  • network scanning

    Network scanning is a procedure for identifying active devices on a network by employing a feature or features in the network ...

  • virtual firewall

    A virtual firewall is a firewall device or service that provides network traffic filtering and monitoring for virtual machines (...

  • cloud penetration testing

    Cloud penetration testing is a tactic an organization uses to assess its cloud security effectiveness by attempting to evade its ...

  • cloud workload protection platform (CWPP)

    A cloud workload protection platform (CWPP) is a security tool designed to protect workloads that run on premises, in the cloud ...

  • Regulation SCI (Regulation Systems Compliance and Integrity)

    Regulation SCI (Regulation Systems Compliance and Integrity) is a set of rules adopted by the U.S. Securities and Exchange ...

  • strategic management

    Strategic management is the ongoing planning, monitoring, analysis and assessment of all necessities an organization needs to ...

  • IT budget

    IT budget is the amount of money spent on an organization's information technology systems and services. It includes compensation...

  • ADP Mobile Solutions

    ADP Mobile Solutions is a self-service mobile app that enables employees to access work records such as pay, schedules, timecards...

  • director of employee engagement

    Director of employee engagement is one of the job titles for a human resources (HR) manager who is responsible for an ...

  • digital HR

    Digital HR is the digital transformation of HR services and processes through the use of social, mobile, analytics and cloud (...

Customer Experience
  • chatbot

    A chatbot is a software or computer program that simulates human conversation or "chatter" through text or voice interactions.

  • martech (marketing technology)

    Martech (marketing technology) refers to the integration of software tools, platforms, and applications designed to streamline ...

  • transactional marketing

    Transactional marketing is a business strategy that focuses on single, point-of-sale transactions.