Fortune Cookies Code

JavaScript


  // This assumes the fortunes are in a JavaScript array variable 
  // called "fortunes". This code must be at the very bottom of
  // the HTML file.
    
  function update() {
    var which = Math.floor(Math.random()*fortunes.length);
    var randomFortune = fortunes[which];
    randomFortune = randomFortune.replace("~", "<br />~");
    document.getElementById('fortune').innerHTML = randomFortune;
  }
    
  document.getElementById('update').onclick = update;
    
  update();