From 53abf6660818e78495886655cfd6231a5a6b0380 Mon Sep 17 00:00:00 2001 From: Pablo Martin Date: Tue, 20 May 2025 15:02:17 +0200 Subject: [PATCH] completed exercise 1.13 --- parts/1/anecdotes/src/App.jsx | 39 +++++++++++++++++++--------------- parts/1/anecdotes/src/main.jsx | 11 +++++++++- parts/1/notes.md | 4 ++-- 3 files changed, 34 insertions(+), 20 deletions(-) diff --git a/parts/1/anecdotes/src/App.jsx b/parts/1/anecdotes/src/App.jsx index 91ce188..f4892ea 100644 --- a/parts/1/anecdotes/src/App.jsx +++ b/parts/1/anecdotes/src/App.jsx @@ -1,8 +1,12 @@ import { useState } from 'react' const AnecdoteDisplay = (props) => { + + const inflectedVotesString = (props.anecdoteVotes === 1) ? 'vote' : 'votes' + return
- {props.anecdotes[props.anecdoteIndex]} +

{props.anecdotes[props.anecdoteIndex]}

+

This quote has {props.anecdoteVotes} {inflectedVotesString}.

} @@ -10,25 +14,26 @@ const Button = (props) => { return ; }; -const App = () => { - 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. 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 [selected, setSelected] = useState(Math.floor(Math.random() * anecdotes.length)); - const selectRandomAnecdote = () => { - setSelected(Math.floor(Math.random() * anecdotes.length)) +const App = (props) => { + const [selected, setSelected] = useState(Math.floor(Math.random() * props.anecdotes.length)); + const [votes, setVotes] = useState( + Array(props.anecdotes.length).fill(0) + ) + + const voteQuote = () => { + const newVotes = [...votes] + newVotes[selected]++ + setVotes(newVotes); } + + const selectRandomAnecdote = () => { + setSelected(Math.floor(Math.random() * props.anecdotes.length)) + } + return ( <> - + +