completed exercise 1.7

This commit is contained in:
counterweight 2025-05-19 23:37:27 +02:00
parent 14fbf0aa7c
commit f9bfc61e6c
Signed by: counterweight
GPG key ID: 883EDBAA726BD96C
2 changed files with 28 additions and 6 deletions

View file

@ -8,7 +8,7 @@ Exercises:
* [X] 1.4
* [X] 1.5
* [X] 1.6
* [ ] 1.7
* [X] 1.7
* [ ] 1.8
* [ ] 1.9
* [ ] 1.10

View file

@ -8,10 +8,10 @@ const Button = (props) => {
return <button onClick={props.onClickCallback}>{props.buttonText} </button>;
};
const Counter = (props) => {
const StatisticDisplay = (props) => {
return (
<p>
{props.counterName} {props.counterValue}
{props.statisticName} {props.statisticValue}
</p>
);
};
@ -22,6 +22,16 @@ const App = () => {
const [neutral, setNeutral] = useState(0);
const [bad, setBad] = useState(0);
const computeTotalCount = () => {
return good + neutral + bad;
};
const computeAverage = () => {
return (good - bad) / computeTotalCount();
};
const computePercentagePositive = () => {
return good / computeTotalCount();
};
return (
<div>
<Header headerText="give feedback" />
@ -44,9 +54,21 @@ const App = () => {
buttonText="bad"
/>
<Header headerText="statistics" />
<Counter counterName="good" counterValue={good} />
<Counter counterName="neutral" counterValue={neutral} />
<Counter counterName="bad" counterValue={bad} />
<StatisticDisplay statisticName="good" statisticValue={good} />
<StatisticDisplay statisticName="neutral" statisticValue={neutral} />
<StatisticDisplay statisticName="bad" statisticValue={bad} />
<StatisticDisplay
statisticName="count"
statisticValue={computeTotalCount()}
/>
<StatisticDisplay
statisticName="average"
statisticValue={computeAverage()}
/>
<StatisticDisplay
statisticName="positive"
statisticValue={computePercentagePositive() * 100 + "%"}
/>
</div>
);
// Buttons