exercise 1.12

This commit is contained in:
Anish Lakhwara 2023-05-15 22:05:49 +10:00
parent 9b4da062a3
commit 48736417d6

View file

@ -1,70 +1,36 @@
import { useState } from 'react' import { useState } from 'react'
const StatisticLine = ({ value, text }) => {
// Whoops, I already made it a table
return (
<tr>
<td>{text}</td>
<td>{value}</td>
</tr>
)
}
const Stats = ({ positive, neutral, negative, total, votes }) => {
if (votes === 0) {
return (
<>
<p> No Votes Cast Yet </p>
</>
)
}
return (
<>
<h2> Results </h2>
<StatisticLine text="Positive" value={positive} />
<StatisticLine text="Neutral" value={neutral} />
<StatisticLine text="Negative" value={negative} />
<StatisticLine text="All" value={votes} />
<StatisticLine text="Average" value={total / votes} />
<StatisticLine text="Positive" value={positive / votes} />
</>
)
}
const Button = ({ handler, text }) => { const Button = ({ handler, text }) => {
return ( return (
<button onClick={handler}> <>
{text} <button onClick={handler}>{text}</button>
</button> </>
) )
} }
const App = () => { const App = () => {
const [good, setGood] = useState(0) const anecdotes = [
const [neutral, setNeutral] = useState(0) 'If it hurts, do it more often.',
const [bad, setBad] = useState(0) 'Adding manpower to a late software project makes it later!',
const total = good - bad 'The first 90 percent of the code accounts for the first 10 percent of the development time...The remaining 10 percent of the code accounts for the other 90 percent of the development time.',
const votes = good + neutral + bad '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. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.',
'Programming without an extremely heavy use of console.log is same as if a doctor would refuse to use x-rays or blood tests when diagnosing patients.',
'The only way to go fast, is to go well.'
]
const incPositive = () => { const [selected, setSelected] = useState(0)
setGood(good + 1)
}
const incNegative = () => { const randomText = () => {
setBad(bad + 1) const value = Math.floor(Math.random() * anecdotes.length)
} setSelected(value)
const incNeutral = () => {
setNeutral(neutral + 1)
} }
return ( return (
<div> <div>
<h2> Feedback </h2> <Button handler={randomText} text="Randomize" />
<Button handler={incPositive} text="positive" /> {anecdotes[selected]}
<Button handler={incNeutral} text="neutral" />
<Button handler={incNegative} text="negative" />
<Stats positive={good} negative={bad} neutral={neutral} total={total} votes={votes} />
</div> </div>
) )
} }