forked from gSchool/checkerboard-exercise
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
124 lines (93 loc) · 3.88 KB
/
Copy pathscript.js
File metadata and controls
124 lines (93 loc) · 3.88 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Your JS goes here
//Part 1: Red & Black Checkered Board
//user must specify color in quotes, e.g. 'blue','red'
function makeCheckerBoard(color1,color2) {
function makeCheckerBoardRow (rowNumber) {
for (let i=0; i<9; i++) {
let checkerSquare = document.createElement('div');
document.body.appendChild(checkerSquare);
checkerSquare.style.width='11.1%';
checkerSquare.style.paddingBottom='11.1%';
checkerSquare.style.float='left';
if ((i+rowNumber)%2 === 0) {
checkerSquare.style.backgroundColor=color1;
}
else {
checkerSquare.style.backgroundColor=color2;
}
}
};
for (let i=0; i<9; i++) {
makeCheckerBoardRow(i);
};
};
//uncomment immediately below to invoke function
//makeCheckerBoard('red','black');
//Part 2: Random Color Checkered Board
//funtion to gernerate a rgb color code
function generatedRandomColor () {
let numberOne = Math.floor(Math.random() * 255)
let numberTwo = Math.floor(Math.random() * 255)
let numberThree = Math.floor(Math.random() * 255)
let numberFour = .3;
return (`rgb(${numberOne},${numberTwo},${numberThree},${numberFour})`);
};
//generate a checkerboard made of two random colors
//uncomment immediately below to invoke function
//makeCheckerBoard(generatedRandomColor(),generatedRandomColor());
//generate a board of random colored squares
function makeRandomBoardOfColoredSquares() {
function makeCheckerBoardRow () {
for (let i=0; i<9; i++) {
let checkerSquare = document.createElement('div');
document.body.appendChild(checkerSquare);
checkerSquare.style.width='11.1%';
checkerSquare.style.paddingBottom='11.1%';
checkerSquare.style.float='left';
checkerSquare.style.backgroundColor=generatedRandomColor();
}
};
for (let i=0; i<9; i++) {
makeCheckerBoardRow(i);
};
};
//uncomment immediately below to invoke funtion
//makeRandomBoardOfColoredSquares();
//generate a checkerboard and make it gradient from top to bottom
function generateGradientCheckerBoard() {
let color1 = generatedRandomColor();
let color2 = generatedRandomColor();
console.log(color1);
console.log(color2);
makeCheckerBoard(color1, color2);
let backgroundContainer = document.getElementsByTagName("body")[0];
backgroundContainer.setAttribute("style", "background: linear-gradient(#e66465, #9198e5); z-index: 2;");
document.body.appendChild(backgroundContainer);
// gradientLayer.setAttribute("style", "background: linear-gradient(red,blue);z-index:1;");
};
generateGradientCheckerBoard();
//note: to create a radient effect on each square could use css's repeating-linear-gradient:.
//generate a checkerboard and make the squares flash a different color
function generateFlashingCheckerBoard () {
makeRandomBoardOfColoredSquares();
setInterval( function replaceAllCheckers () {
function replaceCheckersRow () {
for (let i=0; i<81; i++) {
let checkerSquare = document.createElement('div');
let checkerSquareBeingReplaced = document.getElementsByTagName('body')[0];
checkerSquareBeingReplaced.replaceChild(checkerSquare, checkerSquareBeingReplaced.childNodes[i]);
checkerSquare.style.width='11.1%';
checkerSquare.style.paddingBottom='11.1%';
checkerSquare.style.float='left';
checkerSquare.style.backgroundColor=generatedRandomColor();
}
};
replaceCheckersRow();
for (i=81; i<85; i++) {
let removeExcess = document.getElementsByTagName('div')[i];
removeExcess.parentNode.removeChild(removeExcess);
}
}, 2000);
}
//uncomment below to invoke function
//generateFlashingCheckerBoard()