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.

## Fibonacci

THE ASSIGNMENT: WRITE A FUNCTION THAT RETURNS THE FIRST n NUMBERS OF THE FIBONACCI SEQUENCE.

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

Hemingway Wrote
JavaScript
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) {23     var first = 0, second = 1, next, count = 2, result = [first, second];45     if (size < 2)6           return "the request was made but it was not good"78     while (count++ < size) {9           next = first + second;10          first = second;11          second = next;12          result.push(next);13    }1415    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 as MEN can BREATHE or EYES can SEE,

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) {23     //a CALCKULATION in two acts4     //employ'ng the humourous logick of JAVA-SCRIPTE56     //Dramatis Personae7     var theResult; //an ARRAY to contain THE NUMBERS8     var theCounter; //a NUMBER, serv'nt to the FOR LOOP910    //ACT I: in which a ZERO is added for INITIATION1112    //[ENTER: theResult]1314    //Upon the noble list bestow a zero15    var theResult = [0];1617    //ACT II: a LOOP in which the final TWO NUMBERS are QUEREED and SUMM'D1819    //[ENTER: theCounter]2021    //Commence at one and venture o'er the numbers22    for (theCounter = 1; theCounter < theSize; theCounter++) {23          //By divination set adjoining members24          theResult[theCounter] = (theResult[theCounter-1] || 1) +25                theResult[Math.max(0, theCounter-2)];26    }2728    //'Tis done, and here's the answer29    return theResult;3031    //[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

• What is asynchronous?

In general, asynchronous -- from Greek asyn- ('not with/together') and chronos ('time') -- describes objects or events not ...

• What is a URL (Uniform Resource Locator)?

A URL (Uniform Resource Locator) is a unique identifier used to locate a resource on the internet.

• What is FTP?

File Transfer Protocol (FTP) is a network protocol for transmitting files between computers over TCP/IP connections.

• What is a software license?

A software license is a document that provides legally binding guidelines for the use and distribution of software.

• What is data storytelling?

Data storytelling is the process of translating complex data analyses into understandable terms to inform a business decision or ...

• What is demand shaping?

Demand shaping is an operational supply chain management (SCM) strategy where a company uses tactics such as price incentives, ...

Close