-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
132 lines (118 loc) · 2.65 KB
/
index.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
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
import React, { Component } from 'react'
import ReactDOM from 'react-dom'
let questions = [
{
question: 'What is 2 + 2',
answers: ['4', '22', '1', '8'],
solution: 0
},
{
question: 'What is 2 + 5',
answers: ['4', '7', '1', '8'],
solution: 1
},
{
question: 'What is 2 + 20',
answers: ['4', '22', '1', '8'],
solution: 1
},
{
question: 'What is 12 + 2',
answers: ['4', '22', '14', '8'],
solution: 2
},
{
question: 'What is 2 - 2',
answers: ['4', '22', '1', '0'],
solution: 3
},
{
question: 'What is 2 / 2',
answers: ['4', '22', '1', '8'],
solution: 2
}
];
class Game extends React.Component{
constructor(props) {
super(props)
this.state = {question: questions[0], correct: 0, incorrect: 0}
this.handleClick = this.handleClick.bind(this)
}
handleClick(index) {
this.setState((prevState, props) => {
let i = Math.floor((Math.random() * questions.length))
if (index === this.state.question.solution) {
return {
question: questions[i],
correct: prevState.correct + 1,
incorrect: prevState.incorrect
}
} else {
return {
question: questions[i],
correct: prevState.correct,
incorrect: prevState.incorrect + 1
}
}
})
}
render() {
var h1Style = {
color: 'red',
textAlign: 'center'
};
var style = {
display: 'flex'
};
return (
<div style={style}>
<div>
<h1 style={h1Style}>{this.state.question.question}</h1>
<Board answers={this.state.question.answers} handleClick={this.handleClick}/>
</div>
<Solution correct={this.state.correct} incorrect={this.state.incorrect}/>
</div>
)
}
}
function Board(props) {
var answers = []
for (let i = 0; i < 4; i++) {
answers.push(<Answer index={i} key={i} text={props.answers[i]} handleClick={props.handleClick}/>)
}
return (
<div>
{answers}
</div>
)
}
function Answer(props) {
var style = {
display: 'flex',
justifyContent: 'center',
width: '300px',
height: '50px',
color: 'blue'
};
return (
<button style={style} onClick={() => props.handleClick(props.index)}>
{props.text}
</button>
)
}
function Solution(props) {
var style = {
'paddingLeft': '50px',
'display': 'flex',
'flex-direction': 'column',
'justify-content': 'center'
};
return (
<div style={style}>
<h3>Correct: {props.correct}</h3>
<h3>Incorrect: {props.incorrect}</h3>
</div>
)
}
const root = document.querySelector('#app')
ReactDOM.render(<Game />, root)