I had a good idea for my practice website. I would like to press a button which would generate a random number using math.random. Then, depending on the result( I think i would use if/else statements) I would like it to change the background color. something like if(number) < .33 make background red, else if(number) < .66, make background blue, else make background yellow. How would I go about this? Thanks!!
Pick a random number, then use an if/else if/else
statement to choose the color to change it to.
Do another Google search.
<html>
<body>
<button id="button" onclick="funcToRun();">Sample Website</button>
<script>
function funcToRun() {
var randomNumber = Math.random();
if (randomNumber <= 0.33) {
document.body.style.background = 'red';
}
else if (number <= 0.66) {
document.body.style.background = 'blue';
}
else {
document.body.style.background = 'yellow';
}
}
</script>
</body>
</html>
Again… simple VanillaJs, you should already know this if you finished the Js course…
1 Like
Ok, this is what I was thinking. Thanks for taking the time to help me. I going to try it now.
1 Like
Btw @ithortureu this is not jQuery, this is pure JS (Vanilla JS)