completed exercise 1.13

This commit is contained in:
Pablo Martin 2025-05-20 15:02:17 +02:00
parent 0841e1a749
commit 53abf66608
3 changed files with 34 additions and 20 deletions

View file

@ -1,8 +1,12 @@
import { useState } from 'react' import { useState } from 'react'
const AnecdoteDisplay = (props) => { const AnecdoteDisplay = (props) => {
const inflectedVotesString = (props.anecdoteVotes === 1) ? 'vote' : 'votes'
return <div> return <div>
{props.anecdotes[props.anecdoteIndex]} <p>{props.anecdotes[props.anecdoteIndex]}</p>
<p>This quote has <em>{props.anecdoteVotes}</em> {inflectedVotesString}.</p>
</div> </div>
} }
@ -10,25 +14,26 @@ const Button = (props) => {
return <button onClick={props.onClickCallback}>{props.buttonText} </button>; return <button onClick={props.onClickCallback}>{props.buttonText} </button>;
}; };
const App = () => { const App = (props) => {
const anecdotes = [ const [selected, setSelected] = useState(Math.floor(Math.random() * props.anecdotes.length));
'If it hurts, do it more often.', const [votes, setVotes] = useState(
'Adding manpower to a late software project makes it later!', Array(props.anecdotes.length).fill(0)
'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.', const voteQuote = () => {
'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.', const newVotes = [...votes]
'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.', newVotes[selected]++
'The only way to go fast, is to go well.' setVotes(newVotes);
]
const [selected, setSelected] = useState(Math.floor(Math.random() * anecdotes.length));
const selectRandomAnecdote = () => {
setSelected(Math.floor(Math.random() * anecdotes.length))
} }
const selectRandomAnecdote = () => {
setSelected(Math.floor(Math.random() * props.anecdotes.length))
}
return ( return (
<> <>
<AnecdoteDisplay anecdotes={anecdotes} anecdoteIndex={selected}/> <AnecdoteDisplay anecdotes={props.anecdotes} anecdoteIndex={selected} anecdoteVotes={votes[selected]}/>
<Button buttonText={'Vote this'} onClickCallback={voteQuote}/>
<Button buttonText={'Next'} onClickCallback={selectRandomAnecdote}/> <Button buttonText={'Next'} onClickCallback={selectRandomAnecdote}/>
</> </>
) )

View file

@ -3,5 +3,14 @@ import ReactDOM from 'react-dom/client'
import App from './App.jsx' import App from './App.jsx'
ReactDOM.createRoot(document.getElementById('root')).render( ReactDOM.createRoot(document.getElementById('root')).render(
<App /> <App 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. 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.'
]}/>
) )

View file

@ -14,7 +14,7 @@ Exercises:
* [X] 1.10 * [X] 1.10
* [X] 1.11 * [X] 1.11
* [X] 1.12 * [X] 1.12
* [ ] 1.13 * [X] 1.13
* [ ] 1.14 * [ ] 1.14
My own final exercise: My own final exercise:
@ -22,5 +22,5 @@ My own final exercise:
- Assume loans are always one year long and paid monthly. - Assume loans are always one year long and paid monthly.
- Assume french amortization. - Assume french amortization.
- The app refreshes everything automatically every time any input is edited. - The app refreshes everything automatically every time any input is edited.
- The state of the application should be stored locally. The user must see the same thing if they close and visit the page again.
- Make it so that the user can compare multiple options side by side. - Make it so that the user can compare multiple options side by side.
- The state of the application should be stored locally. The user must see the same thing if they close and visit the page again.