-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
32 lines (26 loc) · 1.08 KB
/
script.js
File metadata and controls
32 lines (26 loc) · 1.08 KB
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
/* QUESTION:
Given 5 boxes, Assign a random color and a random background to each box using DOM concepts
*/
// SOLUTION:
// Select all boxes div by .querySelectorAll(".box").
let boxes = document.body.querySelectorAll(".box");
// Selects container div by .querySelector(".container").
let container = document.body.querySelector(".container");
// Arrow function that generates a random number.
let r_n = () => {
// num variable generate numbers between 0 to 255
let num = Math.floor(Math.random() * 256);
// return num variable.
return num;
}
// Change the background color of container div.
container.style.backgroundColor = `rgb(${r_n()}, ${r_n()}, ${r_n()})`;
// for loop for selecting each box div.
for (let i = 0; i < boxes.length; i++) {
// Change the border of box div.
boxes[i].style.border = `5px solid rgb(${r_n()}, ${r_n()}, ${r_n()})`;
// Change the background color of box div.
boxes[i].style.backgroundColor = `rgb(${r_n()}, ${r_n()}, ${r_n()})`;
// Change the color of box div.
boxes[i].style.color = `rgb(${r_n()}, ${r_n()}, ${r_n()})`;
}