-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patht1-6_t1-11.js
More file actions
94 lines (86 loc) · 2.08 KB
/
t1-6_t1-11.js
File metadata and controls
94 lines (86 loc) · 2.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
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
// fullstackopen @villeheilala
// Part 1 Assignments 1.6-1.11
import React from 'react';
import ReactDOM from 'react-dom';
const Title = () => <h1>Anna palautetta</h1>
const Button = ({ handleClick, text }) => (
<button onClick={handleClick}>
{text}
</button>
)
const Statistics = ({ hyva, neutraali, huono, keskiarvo, positiivisia }) => {
if (hyva + neutraali + huono > 0) { return (
<div>
<table>
<tbody>
<tr>
<td>Hyvä</td>
<td>{hyva}</td>
</tr>
<tr>
<td>Neutraali</td>
<td>{neutraali}</td>
</tr>
<tr>
<td>Huono</td>
<td>{huono}</td>
</tr>
<tr>
<td>Keskiarvo</td>
<td>{keskiarvo}</td>
</tr>
<tr>
<td>Positiivisia</td>
<td>{positiivisia} %</td>
</tr>
</tbody>
</table>
</div>
)} return (
<div>
<p>Yhtään palautetta ei ole annettu.</p>
</div>
)
}
class App extends React.Component {
constructor(props) {
super(props)
this.state = {
hyva: 0,
neutraali: 0,
huono: 0,
keskiarvo: () => {
const mean = (this.state.hyva + (-1 * this.state.huono)) /
(this.state.hyva + this.state.neutraali + this.state.huono)
if (mean) return Number.parseFloat(mean).toFixed(2)
return 0
},
positiivisia: () => {
const positive = this.state.hyva / (this.state.hyva +
this.state.huono + this.state.neutraali) * 100
if (positive) return Number.parseFloat(positive).toFixed(2)
return 0
}
}
}
clickButton = (value) => () => this.setState({ [value]: this.state[value] + 1 })
render() {
return (
<div>
<Title />
<Button handleClick={this.clickButton("hyva")} text="Hyvä" />
<Button handleClick={this.clickButton("neutraali")} text="Neutraali" />
<Button handleClick={this.clickButton("huono")} text="Huono" />
<h2>Statistiikka</h2>
<Statistics
hyva={this.state.hyva}
neutraali={this.state.neutraali}
huono={this.state.huono}
keskiarvo={this.state.keskiarvo()}
positiivisia={this.state.positiivisia()}
/>
</div>
)
}
}
ReactDOM.render(<App />, document.getElementById('root'));