-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
41 lines (40 loc) · 1.22 KB
/
app.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
window.onload = () => {
main();
};
// main function
function main() {
let increment = 0;
let bgColorChanged = document.getElementById("color-changed");
let btn = document.getElementById("colorChangerBtn");
btn.addEventListener("click", function () {
let bgColor = generateRGBcolor();
bgColorChanged.style.backgroundColor = bgColor;
increment++;
fullHistory(bgColor, increment);
});
}
// Generating RGB Random Color
function generateRGBcolor() {
let r = Math.floor(Math.random() * 255) + 1;
let g = Math.floor(Math.random() * 255) + 1;
let b = Math.floor(Math.random() * 255) + 1;
return `rgb(${r}, ${g}, ${b})`;
}
// History
function fullHistory(bgColor, increment) {
let ul = document.getElementById("history");
let li = document.createElement("li");
li.classList.add("list-group-item");
li.style.backgroundColor = bgColor;
li.innerText = increment + ") " + bgColor;
ul.insertBefore(li, ul.children[0]);
console.log(increment);
}
// Reset
let reset = document.getElementById("reset");
reset.addEventListener("click", function () {
let bgColorChanged = document.getElementById("color-changed");
bgColorChanged.style = "";
let ul = document.getElementById("history");
ul.innerHTML = "";
});