Fortune.js

Posted on 04 October 2019 in dev

Fortune aims to be a minimalist fortune script. Quotes / fortunes with equations in MathJax are supported.

TL;DR: Go play with the code on github.

How it works

Generate a quote

The fortune function gets a random fortune from the quotes array and writes it in the HTML document.

function fortune(){
  /* calculate a random index */
  index = Math.floor(Math.random() * quotes.length);
  /* display the quotation */
  document.write("<p>" + quotes[index] + "</p>");
};

To do so in the html just add:

<script type="text/javascript" src="js/fortune.js" type="text/javascript"></script>
<script type="text/javascript">fortune();</script>

Add a new quote or modify a quote

Open the fortune.js file and modify the quotes array.

Add MathJax support

Add the following code to support MathJax quotes:

<script>
    MathJax = {
        tex: {
            inlineMath: [
                ['$', '$'],
                ['\\(', '\\)']
            ]
        }
    };
</script>
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>

Example

To see what fortune.js can do for you, check the index.html file in the example folder.

Acknowledgments

js