Created
December 24, 2020 23:47
-
-
Save zachgoll/718af2e9dd2bcf40db15a37085a1e5de to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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