Skip to main content

What if Great Writers Were Great JavaScript Coders?

Recommended Videos

What if the Bard of Avon was the Bard of Python? What if Hemingway wrote The Old Man and The C?  These are the questions that run through the head of coding guru and Twitter programmer, Angus Croll, as he downs his fourth cup of coffee infront of the glow of a monitor at four in the morning. Croll imagined how five prominent writers would write a program using the JavaScript language and returning a Fibonacci series. If you’re a lover of literature, if you’re a seasoned veteran of coding, or even if you have no idea what I’m talking about, seeing how Hemingway would’ve written JavaScript is pretty amusing. Check it out below.

First thing’s first: What the hell is JavaScript?

Second thing’s second: What the hell is a Fibonacci series?

Third thing’s third: Would that which we call a rose by any other name really smell as sweet?

I can help you with two of these questions! JavaScript is a prototype-based scripting language. It is much simpler than most other scripting languages, making it easier to use for automating tasks on a computer. For example, you could use JavaScript to make a computer automatically work out that “2+3=5” then “3+5=8” then “5+8=13” then “8+13=21” and so on and forth. Starting to see a pattern? Leonardo Fibonacci of Pisa sure did back in 1202. In his book, Liber Abaci, he outlined a sequence of numbers in which you get any number by adding up the two numbers before it. This sequence became known as the “Fibonacci sequence,” or a “Fibonacci series.” Here’s a Fibonacci series starting with 0 and 1:

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

The Fibonacci sequence even explains such natural phenomena as how branches grow on trees and how snail shells spiral. Pretty impressive for a guy without a TI-85, huh?

Angus Croll is a programmer and lifelong lover of literature. He loves programming in JavaScript because of the language’s depth relative to its simplicity. Much like great literature, JavaScript has a simple exterior, masking endless possibilities and interpretations; you can read and understand the words, but you have to dive deeper to really get it.

So just how would famous writers use JavaScript to tackle the Fibonacci sequence? Let’s take a look at how Croll thinks it would play out:

Ernest Hemingway

function fibonacci(size) {

var first = 0, second = 1, next, count = 2, result = [first, second];

if(size < 2)
return "the request was made but it was not good"

while(count++ < size) {
next = first + second;
first = second;
second = next;
result.push(next);
}
return result;
}

Hemingway was a man of few words. His language was simple, yet elegant. Notice how this code uses the “var” keyword in the second line to define the function’s variables. “0” is the first number in the sequence, so Hemingway names it “first.” “1” is the second number in the sequence, so Hemingway names it “second.” This function is extremely simple. Instead of saying Farewell to Arms, you can say farewell to confusing code! I’m so sorry about that.

William Shakespeare

function theSeriesOfFIBONACCI(theSize) {

//a CALCKULATION in two acts.
//employ'ng the humourous logick of JAVA-SCRIPTE

//Dramatis Personae
var theResult; //an ARRAY to contain THE NUMBERS
var theCounter; //a NUMBER, serv'nt to the FOR LOOP

//ACT I: in which a ZERO is added for INITIATION

//[ENTER: theResult]

//Upon the noble list bestow a zero
var theResult = [0];

//ACT II: a LOOP in which the final TWO NUMBERS are QUEREED and SUMM'D

//[ENTER: theCounter]

//Commence at one and venture o'er the numbers
for (theCounter = 1; theCounter < theSize; theCounter++) {
//By divination set adjoining members
theResult[theCounter] = (theResult[theCounter-1]||1) + theResult[Math.max(0, theCounter-2)];
}

//'Tis done, and here's the answer.
return theResult;

 

//[Exuent]
}

Although brevity is the soul of wit, everyone knows that Shakespeare was a bit lacking in the brevity department. His code is actually structured like a play with two acts and a Dramatis Personae. Shakespeare also makes extensive use of the “//” comment to introduce his players and scenes. You can use this code to show how cultured and knowledgable you are about Shakespeare. People will love it. I swear!

Charles Dickens

function mrFibbowicksNumbers(enormity) {
var assortment = [0,1,1], tally = 3, artfulRatio = 1.61803;

while(tally++ < enormity) {
//here is an exceedingly clever device
assortment.push(Math.round(assortment[tally-2] * artfulRatio));
}

//should there be an overabundance of elements, a remedy need be applied
return assortment.slice(0, enormity);
}

And here we are at Dickens. No one can say that Dickens lacks attention to detail. If you were wondering about the color of the inside of the constable’s right breast pocket, he’ll give you its material, its country of origin, and a list of the last six wearers of the jacket. It almost seems like he spent so much time focusing on the extraneous details, that the reader would miss something going on in the scene. According to Croll, who isn’t exactly a fan of Dickens, he misses the whole point of the Fibonacci sequence by using multiplication as opposed to addition. God bless us, everyone! At least everyone who has to use this code.

You can check out the rest of the code snippets here. Happy coding!

(via Neatorama)

relevant to your interests

Have a tip we should know? tips@themarysue.com

Author

Filed Under:

Follow The Mary Sue:

Exit mobile version