forked from jatloe/RandomWebsite1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
squares.html
146 lines (136 loc) · 4.08 KB
/
squares.html
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<!DOCTYPE html>
<html>
<head>
<link rel="icon" href="jatloesmile-hd.png" />
<title>Squares Game</title>
</head>
<body>
<div id="test"></div>
</body>
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
<script
src="https://unpkg.com/react@18/umd/react.production.min.js"
crossorigin
></script>
<script
src="https://unpkg.com/react-dom@18/umd/react-dom.production.min.js"
crossorigin
></script>
<script type="text/babel">
const e = React.createElement;
function MyButton(props) {
return <button onClick={props.onClick}>{props.value}</button>;
}
class Board extends React.Component {
constructor(props) {
super(props);
this.state = {
squares: Array(49).fill(0),
message: "Perfect squares: none",
};
}
checkSquares() {
let foundSquares = [];
let allSquares = [];
let mySquares = this.state.squares;
for (let lol = 10; lol <= 31; lol++) {
allSquares.push(lol * lol);
}
for (let i = 0; i <= 4; i++) {
for (let j = 0; j <= 6; j++) {
let cat_awa =
mySquares[7 * i + j] * 100 +
mySquares[7 * (i + 1) + j] * 10 +
mySquares[7 * (i + 2) + j];
if (
allSquares.includes(cat_awa) &&
!foundSquares.includes(cat_awa)
) {
foundSquares.push(cat_awa);
}
}
}
for (let i = 0; i <= 6; i++) {
for (let j = 0; j <= 4; j++) {
let cat_awa =
mySquares[7 * i + j] * 100 +
mySquares[7 * i + j + 1] * 10 +
mySquares[7 * i + j + 2];
if (
allSquares.includes(cat_awa) &&
!foundSquares.includes(cat_awa)
) {
foundSquares.push(cat_awa);
}
}
}
foundSquares.sort();
let newMessage = "Perfect squares: ";
for (let foundSquare of foundSquares) {
newMessage += foundSquare + ", ";
}
newMessage = newMessage.slice(0, -2);
if (newMessage == "Perfect squares")
newMessage = "Perfect squares: none";
if (
newMessage ==
"Perfect squares: 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900, 961"
)
newMessage += ". You won!";
this.setState({ message: newMessage });
}
handleClick(i) {
let x = this.state.squares.slice();
x[i] = (x[i] + 1) % 10;
this.setState({ squares: x });
setTimeout(() => {
this.checkSquares();
}, 50);
}
renderButton(i) {
render(
<MyButton
value={this.state.squares[i]}
onClick={() => this.handleClick(i)}
/>
);
}
renderMessage() {
return <p>{this.state.message}</p>;
}
renderButtonRow(i) {
let thing = [];
for (let j = i * 7; j < i * 7 + 7; j++) {
thing.push(
<MyButton
value={this.state.squares[j]}
onClick={() => this.handleClick(j)}
key={j}
/>
);
}
return <div>{thing}</div>;
}
render() {
return (
<div>
<p>V1.2</p>
<p>Form all 3 digit squares in the grid!</p> <br />
{this.renderMessage()}
<br />
{this.renderButtonRow(0)}
{this.renderButtonRow(1)}
{this.renderButtonRow(2)}
{this.renderButtonRow(3)}
{this.renderButtonRow(4)}
{this.renderButtonRow(5)}
{this.renderButtonRow(6)}
</div>
);
}
}
const domContainer = document.querySelector("#test");
const root = ReactDOM.createRoot(domContainer);
root.render(e(Board));
</script>
</html>