-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
34 lines (28 loc) · 889 Bytes
/
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
/**
* Project Requirements:
* - Change the background color by generating random hex color by clicking a button
* - Also display the hex code to a disabled input field
*/
// Steps
// Step 1 - create onload handler
window.onload = () => {
main();
};
function main() {
const root = document.getElementById('root');
const btn = document.getElementById('change-btn');
const output = document.getElementById('output');
btn.addEventListener('click', function () {
const bgColor = generateHexColor();
root.style.backgroundColor = bgColor;
output.value = bgColor;
});
}
function generateHexColor() {
// #000000 #ffffff
// 255, 255, 255 -> #FFFFFF
const red = Math.floor(Math.random() * 255);
const green = Math.floor(Math.random() * 255);
const blue = Math.floor(Math.random() * 255);
return `#${red.toString(16)}${green.toString(16)}${blue.toString(16)}`;
}