-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsquares.js
62 lines (52 loc) · 1.51 KB
/
squares.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
$(document).ready(function() {
// Create grid
createGrid(16, squareSize(16));
//Clear Grid on Button Press
$('#clear').click(function() {
//Ask user for how many squares and validate between 0 and 80
var numSquares = prompt("How many squares per side?");
if (isValid(numSquares)) {
clearGrid();
createGrid(numSquares, squareSize(numSquares));
} else {
}
});
});
function randColor() {
//Generate color in Decimal
var decColor = Math.floor(Math.random() * 16777216);
//Convert to Hex
var hexColor = '#' + decColor.toString(16);
console.log("Dec Color: " + decColor + " hex Color " + hexColor);
return hexColor;
}
function createGrid(sideSize, squareSize) {
for (i = 0; i < sideSize; i++) {
for (j = 0; j < sideSize; j++) {
$('.container').append('<div class="square" style="width:' + squareSize + 'px; height:' + squareSize + 'px;"></div>');
};
$('.container').append('<br>');
};
//Change color on hover event
$('.square').mouseover(function() {
$(this).css("background-color", randColor());
});
}
function clearGrid() {
$('.container').html('');
console.log("Grid cleared");
}
function isValid(numSquares) {
if (numSquares > 0) {
if (numSquares <= 64) {
return true;
}
}
alert("Try again, the amount of squares per side must be between 0 and 64");
return false;
}
function squareSize(numSquares) {
var size = Math.floor((960 - 3 * numSquares) / numSquares);
console.log("Calculated size: " + size);
return size;
}