Skip to content

Commit 44e89df

Browse files
committed
chore: better props declaration
1 parent 890b556 commit 44e89df

File tree

2 files changed

+23
-22
lines changed

2 files changed

+23
-22
lines changed

src/pages/GameOverScreen.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@ import Button from '../components/UI/Button';
33
import classes from './GameOverScreen.module.css';
44

55
const GameOverScreen = (props) => {
6-
const { correctAnswers, incorrectAnswers } = props;
6+
const { trueAnswers, falseAnswers } = props;
77

88
return (
99
<div className={classes.container}>
1010
<h1>
11-
Result: {correctAnswers.length} of {props.questions.length} (
12-
{(correctAnswers.length / props.questions.length) * 100}%)
11+
Result: {trueAnswers.length} of {props.questions.length} (
12+
{(trueAnswers.length / props.questions.length) * 100}%)
1313
</h1>
1414

15-
{incorrectAnswers.length !== 0 && (
15+
{falseAnswers.length !== 0 && (
1616
<div className={classes['answers-container']}>
17-
<span className={classes['answers-title']}>Incorrect answers:</span>
18-
{incorrectAnswers.map((answer) => (
17+
<span className={classes['answers-title']}>False answers:</span>
18+
{falseAnswers.map((answer) => (
1919
<div
2020
key={answer.question.id}
2121
className={classes['question-container']}
@@ -34,10 +34,10 @@ const GameOverScreen = (props) => {
3434
</div>
3535
)}
3636

37-
{correctAnswers.length !== 0 && (
37+
{trueAnswers.length !== 0 && (
3838
<div className={classes['answers-container']}>
39-
<span className={classes['answers-title']}>Correct answers:</span>
40-
{correctAnswers.map((answer) => (
39+
<span className={classes['answers-title']}>True answers:</span>
40+
{trueAnswers.map((answer) => (
4141
<div
4242
key={answer.question.id}
4343
className={classes['question-container']}

src/pages/QuizScreen.js

+14-13
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ import { getObjKey } from '../utils/get-obj-keys';
44
import GameOverScreen from './GameOverScreen';
55
import classes from './QuizScreen.module.css';
66

7-
let correctAnswers = [];
8-
let incorrectAnswers = [];
7+
let trueAnswers = [];
8+
let falseAnswers = [];
99

1010
const QuizScreen = (props) => {
1111
const [currentQuestion, setCurrentQuestion] = useState(0);
1212
const [chosenAnswers, setChosenAnswers] = useState([]);
1313
const [gameIsOver, setGameIsOver] = useState(false);
14+
const { questions } = props;
1415

15-
let answersArray = props.questions[currentQuestion].answers;
16+
let answersArray = questions[currentQuestion].answers;
1617
let answersValues = Object.values(answersArray).filter((v) => v !== null);
1718

1819
function answersHandler(selectedAnswer) {
@@ -29,7 +30,7 @@ const QuizScreen = (props) => {
2930
}
3031

3132
const nextHandler = () => {
32-
let correctAnswersList = props.questions[currentQuestion].correct_answers;
33+
let correctAnswersList = questions[currentQuestion].correct_answers;
3334
const userAnswer = chosenAnswers.join(', ');
3435
let correct_answer = '';
3536

@@ -40,21 +41,21 @@ const QuizScreen = (props) => {
4041
}
4142

4243
if (userAnswer === correct_answer) {
43-
correctAnswers.push({
44-
question: props.questions[currentQuestion],
44+
trueAnswers.push({
45+
question: questions[currentQuestion],
4546
correctAnswer: correct_answer,
4647
userAnswer: userAnswer,
4748
questionNumber: currentQuestion + 1,
4849
});
4950
} else {
50-
incorrectAnswers.push({
51-
question: props.questions[currentQuestion],
51+
falseAnswers.push({
52+
question: questions[currentQuestion],
5253
correctAnswer: correct_answer,
5354
userAnswer: userAnswer,
5455
questionNumber: currentQuestion + 1,
5556
});
5657
}
57-
if (currentQuestion < props.questions.length - 1) {
58+
if (currentQuestion < questions.length - 1) {
5859
setCurrentQuestion((curr) => curr + 1);
5960
} else {
6061
setGameIsOver(true);
@@ -65,9 +66,9 @@ const QuizScreen = (props) => {
6566
if (gameIsOver) {
6667
return (
6768
<GameOverScreen
68-
correctAnswers={correctAnswers}
69-
incorrectAnswers={incorrectAnswers}
70-
questions={props.questions}
69+
trueAnswers={trueAnswers}
70+
falseAnswers={falseAnswers}
71+
questions={questions}
7172
/>
7273
);
7374
}
@@ -77,7 +78,7 @@ const QuizScreen = (props) => {
7778
{
7879
<h2 className={classes.title}>
7980
{currentQuestion + 1 + '. '}
80-
{props.questions[currentQuestion].question}
81+
{questions[currentQuestion].question}
8182
</h2>
8283
}
8384
<div className={classes['answers-container']}>

0 commit comments

Comments
 (0)