-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
41 lines (36 loc) · 941 Bytes
/
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
32
33
34
35
36
37
38
39
40
41
const newColourBtnElement = document.getElementById("new-colour-button");
const currentColourElement = document.getElementById("current-colour");
const hexValues = [
"0",
"1",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"A",
"B",
"C",
"D",
"E",
"F",
];
function getRandomHexValue() {
const randomIndexPosition = Math.floor(Math.random() * hexValues.length);
const randomHexValue = hexValues[randomIndexPosition];
return randomHexValue;
}
function getRandomHexString(stringLength) {
let hexString = "";
for (let index = 0; index < stringLength; index++) {
hexString += getRandomHexValue();
}
return hexString;
}
newColourBtnElement.addEventListener("click", function () {
const randomHexString = "#" + getRandomHexString(6);
document.body.style.setProperty("background-color", randomHexString);
currentColourElement.textContent = randomHexString;
});