Reputation: 73
I am writing a quiz sort of game and I want the background to change every time there is a new question. Could someone help me with either how to code this or ideally the code? (I haven't copied the array in as it links to a CSV.
Here is my current code:
for (var i = 1; i <= 100; i++) {
var number = Math.floor((Math.random() * 18) + 1);
var stringGuess = prompt(questions[number])
if (stringGuess == answers[number]) {
alert("YOU GOT IT RIGHT!! THE ANSWER IS " + answers[number])
} else if (stringGuess > answers[number]) {
alert("Too high. The answer is " + answers[number]);
} else {
alert("Too low. The answer is " + answers[number]);
}
}
Thank you!!!
Upvotes: 1
Views: 61
Reputation: 2422
For a random colour you could set a random RGB value using the function below. To set css background-color on an element you can use element.style.backgroundColour = colour;
function randomRGB(){
return `rgb(${Math.floor(Math.random()*256)},${Math.floor(Math.random()*256)},${Math.floor(Math.random()*256)})`;
}
function setColour(element, colour){
element.style.backgroundColour = colour;
}
Therefore, use this whenever you need to set a random background colour:
setColour(document.getElementById("my-background-element"), randomRGB());
remembering to change "my-background-element" to the id of the background element you have.
Upvotes: 1