-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patht1-12_t1-14.js
More file actions
73 lines (63 loc) · 2.05 KB
/
t1-12_t1-14.js
File metadata and controls
73 lines (63 loc) · 2.05 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
// fullstackopen @villeheilala
// Part 1 Assignments 1.12-1.14
import React from 'react';
import ReactDOM from 'react-dom';
const Button = ({ handleClick, text }) => (
<div>
<button onClick={handleClick}>
{text}
</button>
</div>
)
const Votes = ({ votes }) => (
<div>
<p>This quote has {votes} votes</p>
</div>
)
const Best = ({ best, votes }) => (
<div>
<h2>Best quote ever!</h2>
<p>{anecdotes[best]}</p>
<p>This quote has {votes} votes</p>
</div>
)
class App extends React.Component {
constructor(props) {
super(props)
this.state = {
selected: 0,
votes: [0, 0, 0, 0, 0, 0]
}
}
clickRandomizeButton = (select) => () => { this.setState({ selected: select}) }
clickVoteButton = ({ selected, votes }) => () => {
const votesCopy = [...votes]
votesCopy[selected] += 1
this.setState({ votes: votesCopy })
}
render() {
const best = this.state.votes.indexOf(Math.max(...this.state.votes))
return (
<div>
<h1>Quote of the day</h1>
{this.props.anecdotes[this.state.selected]}
<Button handleClick={this.clickRandomizeButton(Math.floor(Math.random() * 6))} text="Random quote"/>
<Button handleClick={this.clickVoteButton(this.state)} text="Vote" />
<Votes votes={this.state.votes[this.state.selected]} />
<Best best={best} votes={this.state.votes[best]} />
</div>
)
}
}
const anecdotes = [
'If it hurts, do it more often',
'Adding manpower to a late software project makes it later!',
'The first 90 percent of the code accounts for the first 90 percent of the development time...The remaining 10 percent of the code accounts for the other 90 percent of the development time.',
'Any fool can write code that a computer can understand. Good programmers write code that humans can understand.',
'Premature optimization is the root of all evil.',
'Debugging is twice as hard as writing the code in the first place. The refore, if you write the code as cleverly as possible, you are, by defini tion, not smart enough to debug it.'
]
ReactDOM.render(
<App anecdotes={anecdotes}/>,
document.getElementById('root')
)