Sorpresa:
JavaScript non funziona solo sui browser!

Francesco Prelz

INFN, sezione di Milano

Sommario

Un po' di storia

Esempi di CGI

Ruetteln

Download

If Hemingway wrote Javascript... (1)

Non dimentichiamo come sempre l'importanza di un buono stile di programmazione, terso, leggibile e commentato.
Dan Brown Dan Brown
«My mind tells me I will never understand JavaScript. And my heart tells me I am not meant to».
/*
 FACT: Some time in 1557, Michelangelo Moribundi, the renowned, bald-headed alchemist, fashioned
 a secret code out of bits of asparagus and placed it in a long-forgotten vault ...
*/
function theDaFibonacciCode(numeratiFettucini) {
 // Wide awake, the bleary-eyed Langdon watched as two tall, lissome number ones, with
 // big feet and a type of hat, sidled up to the rounded zero ...
 var ilInumerati = [0,1,1];
 // while theIntegerThatIncrementsOneByOne morphed eerily into a ... three.
 theIntegerThatIncrementsOneByOne = 3,
 // Now the silent ratio that could not be uttered had come to make it right.
 TheBotticelliVector = 1.61803;
 
 while (theIntegerThatIncrementsOneByOne < numeratiFettucini) {
  // Somehow another number one appeared and theIntegerThatIncrementsOneByOne snatched at it gracefully.
  theIntegerThatIncrementsOneByOne = theIntegerThatIncrementsOneByOne + 1;

 // The renowned, rounded 16-bit unsigned integer tentatively succumbed to the strange force of
 // the vector before pushing itself bodily into the hands of the weakly typed array.
 ilInumerati.push(
 Math.round(ilInumerati[theIntegerThatIncrementsOneByOne - 2] * TheBotticelliVector)
   );
  }
}
         

If Hemingway wrote Javascript... (2)

William Shakespeare William Shakespeare
«So foul and fair a language I have not seen».
 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;
  //[Exeunt]
 }
         

If Hemingway wrote Javascript... (3)

Ernest Hemingway Ernest Hemingway
«All my life I've looked at JavaScript as though I were seeing it for the first time».
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;
}
         
A voi la scelta... (Altri autori nel libro originale).