-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
39 lines (23 loc) · 1.16 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
var randomHexValue;
function numberToHex(c) {
var hex = c.toString(16);
return hex.length == 1 ? "0" + hex : hex;
}
const randomRgbNumber = () => { return Math.floor((Math.random() * 255) + 1) };
const randomHex = () => { return "#" + numberToHex(randomRgbNumber()) + numberToHex(randomRgbNumber()) + numberToHex(randomRgbNumber()); }
function changeColour() {
let randomButton = "button" + Math.floor((Math.random() * 3) + 1);
const arrayOfButtons = document.getElementsByTagName("button");
randomHexValue = randomHex();
document.getElementsByClassName("square")[0].setAttribute("style", "background-color:" + randomHexValue + ";");
Array.from(arrayOfButtons).forEach(element => {
element.id === randomButton ? document.getElementById(randomButton).textContent = randomHexValue : document.getElementById(element.id).innerText = randomHex();
});
}
function checkAnswer(button) {
button.textContent === randomHexValue ? document.getElementsByTagName("p")[0].textContent = "Correct!" : document.getElementsByTagName("p")[0].textContent = "Incorrect!";
changeColour();
}
window.onload = (event) => {
changeColour();
};