Skip to content

Commit fcdd2fa

Browse files
author
Mateusz Nowak
committed
#8 Refactor QuestionGenerator following functional programming principles
1 parent efc6386 commit fcdd2fa

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

src/app/QuestionGenrator.js

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,29 @@ export class QuestionGenerator {
1212
async generateQuestion() {
1313
const questionsIdArray = this.generateRandomIdArray();
1414
const rightAnswerId = this.randomRightAnswer(questionsIdArray);
15-
const result = {
16-
image: '',
17-
answers: [],
18-
rightAnswer: '',
19-
};
2015

21-
await Promise.all(
22-
questionsIdArray.map((id) =>
23-
this.fetchData(this.mode, id).then((data) => {
24-
result.answers.push(data.name);
25-
if (rightAnswerId == id) {
26-
result.image = btoa(
27-
`static/assets/img/modes/${this.mode}/${id}.jpg`,
28-
);
29-
result.rightAnswer = data.name;
30-
}
31-
}),
32-
),
16+
const questions = await Promise.all(
17+
questionsIdArray.map(this.getQuestion()),
18+
);
19+
const answers = questions.map((question) => question.name);
20+
const rightAnswer = questions.find(
21+
(question) => rightAnswerId === question.id,
22+
).name;
23+
const questionImage = btoa(
24+
`static/assets/img/modes/${this.mode}/${rightAnswerId}.jpg`,
3325
);
26+
return {
27+
image: questionImage,
28+
answers,
29+
rightAnswer,
30+
};
31+
}
3432

35-
return result;
33+
getQuestion() {
34+
return (questionId) =>
35+
this.fetchData(this.mode, questionId).then((questionResponse) => ({
36+
id: questionId,
37+
...questionResponse,
38+
}));
3639
}
3740
}

0 commit comments

Comments
 (0)