Random Quote Machine (aasanchez)

Random Quote Machine (aasanchez)
0.0 0

#1

This is my Random Quote Machine, please feel free to comment, destroy, critic…

Thanks


#2

Couple of suggestions to simplify your code.

#1) The following code:

  $(document).ready(function () {
     randomQuote(random());
     $("#getMessage").on("click", function () {
         randomQuote(random());
     });
  });

could reduce to:

$(document).ready(function () {
    randomQuote();
    $("#getMessage").on("click", randomQuote);
});

and then in your randomQuote function you could do:

function randomQuote() {
    var index = random();  // this takes care of getting the random index
    var quote = json[index]['quote'];
    var author = json[index]['author'];
    .
    .
    .
}

If you read about destructuring assignment of an object, then you could simply these three lines above to:

var {quote, author} = json[random()];

#2) Since you are using jQuery alot in your project, you could change:

document.body.style.backgroundColor = '#' + (Math.random() * 0xFFFFFF << 0).toString(16);

to

$("body").css("background-color", '#' + (Math.random() * 0xFFFFFF << 0).toString(16));

#3) This is a very small suggestion, but because both of each quote object’s property names have no spaces or other non-word characters, you could change:

{
  "quote": "Life isn’t about getting and having, it’s about giving and being.",
  "author": "Kevin Kruse"
}

to

{
  quote: "Life isn’t about getting and having, it’s about giving and being.",
  author: "Kevin Kruse"
}

#3

thank you for your advices, I integrated into my code, thanks