I am currently building a web app where I need to randomly switch the font and color of a title (inside the #content
div) and the background of the page every time a button is clicked.
I wrote the following code that works perfectly, but I'm wondering if it could be optimized (or at least written in a more elegant way).
// Switch font randomly
var font = ["lobster", "shadows", "oswald", "josefin", "gloria", "pacifico"];
var color = ["blue", "purple", "yellow", "red", "orange", "green"];
var background = ["bg-blue", "bg-purple", "bg-yellow", "bg-red", "bg-orange", "bg-green"];
$("#try-me").click(function() {
$("#content")
.hide()
.removeClass()
.addClass(font[Math.floor(Math.random()*font.length)]) // Font
.addClass(color[Math.floor(Math.random()*color.length)]) // Color
.fadeIn(600);
$("html")
.removeClass()
.addClass(background[Math.floor(Math.random()*background.length)]); // Background
});