Skip to content

Instantly share code, notes, and snippets.

@zachgoll
Created December 24, 2020 23:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zachgoll/718af2e9dd2bcf40db15a37085a1e5de to your computer and use it in GitHub Desktop.
Save zachgoll/718af2e9dd2bcf40db15a37085a1e5de to your computer and use it in GitHub Desktop.
// This is a JavaScript comment. It doesn't affect the code at all
// Here, we are selecting the <body></body> HTML tag
const htmlBody = document.querySelector("body");
// This is a function
const randomClickFunction = function () {
// This is an array of color codes.
const colors = ["#002942", "#0CA7DB", "#F56C05", "#DB3E00", "purple"];
// This will calculate a random "index" that we can use to select a color in the array
const randomIndex = Math.floor(Math.random() * colors.length);
// We are selecting a single value from our "colors" array above
const randomColor = colors[randomIndex];
// We are setting the webpage background our random color
htmlBody.style.backgroundColor = randomColor;
// We are printing a message to the console
console.log("The user clicked and turned the background" + randomColor + "!");
};
// This sets an "event listener" which "listens" for click events on the webpage
htmlBody.onclick = randomClickFunction;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment