mardi 8 août 2017

Using jQuery and Math.random() to select nested objects properties

I'm creating a random quote machine that will present a random quote from various philosophers.

I have an object literal with nested objects containing philosophers and their quotes. Using jQuery functions and Math.random(), how can I select a random quote from my object literal structure? Is there a better way to organize the data?

I've started with a jQuery closure that will display a designated quote that I'd like to modify using Math.random().

Looking for explanations to solutions as I'm a beginner. Thanks in advance.

Example object literal:

var quotes = 
{
  awatts: {
    name: "Alan Watts",
    quote: "The only way to make          sense out of change is to            plunge into it, move with it,        and join the dance."
  },
  etolle: {
    name: "Eckhart Tolle",
    quote: "Realize deeply that the       present moment is all you ever       have."
  },
  tmckenna: {
    name: "Terrence Mckenna",
    quote: "“The cost of sanity in        this society, is a certain          level of alienation” "
  }
};

Example jQuery functions with single quote selected:

    $(document).ready(function() {
        $('.mybutton').click(function() {
            $('#quote').html(quotes.awatts.quote);
        });
    });




Aucun commentaire:

Enregistrer un commentaire