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
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) {
2
3 var first = 0, second = 1, next, count = 2, result = [first, second];
4
5 if (size < 2)
6 return "the request was made but it was not good"
7
8 while (count++ < size) {
9 next = first + second;
10 first = second;
11 second = next;
12 result.push(next);
13 }
14
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 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) {
2
3 //a CALCKULATION in two acts
4 //employ'ng the humourous logick of JAVA-SCRIPTE
5
6 //Dramatis Personae
7 var theResult; //an ARRAY to contain THE NUMBERS
8 var theCounter; //a NUMBER, serv'nt to the FOR LOOP
9
10 //ACT I: in which a ZERO is added for INITIATION
11
12 //[ENTER: theResult]
13
14 //Upon the noble list bestow a zero
15 var theResult = [0];
16
17 //ACT II: a LOOP in which the final TWO NUMBERS are QUEREED and SUMM'D
18
19 //[ENTER: theCounter]
20
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 }
27
28 //'Tis done, and here's the answer
29 return theResult;
30
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.