From d5a7559ef0e5f7d86bdc181dc6971ee022b2f466 Mon Sep 17 00:00:00 2001 From: Urszuja Date: Mon, 6 Dec 2021 20:18:20 +0100 Subject: [PATCH 1/2] add Question class --- src/main.js | 14 ++++++++++++++ src/model/question.js | 26 ++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 src/model/question.js diff --git a/src/main.js b/src/main.js index 220b44a..925f2eb 100644 --- a/src/main.js +++ b/src/main.js @@ -1,5 +1,6 @@ import './style.css'; import Button from './components/Button.js'; +import Question from './model/question.js'; document.querySelector('#app').innerHTML = `

CC first project

@@ -8,6 +9,19 @@ document.querySelector('#app').innerHTML = ` const simpleCallback = () => { console.log(`Greetings from koala`); }; +// how to use Question class Question has {imageUrl, correct, incorrectAnswers, question} and .getAnswers method + +const firstQuestion = new Question( + 'url', + 'ula', + ['ala', 'ela', 'ola'], + "Mirror, mirror on the wall who's the fairest of them all?", +); + +console.log(firstQuestion.question); +//show answers +console.log('The correct answer is:'); +setTimeout(() => console.log(firstQuestion.correct), 3000); // how to use: Button(label, className, animate, 'eventListener', callback) const buttonQuiz = Button('start quiz', 'quiz', true, 'click', simpleCallback); diff --git a/src/model/question.js b/src/model/question.js new file mode 100644 index 0000000..45008ce --- /dev/null +++ b/src/model/question.js @@ -0,0 +1,26 @@ +export default class Question { + constructor(imageUrl, correct, incorrectAnswers, question) { + this.imageUrl = imageUrl; + this.correct = correct; + this.incorrectAnswers = incorrectAnswers; + this.question = question; + } + + /*getAnswers = () => { + return this.randomAnswers(); + }; + + randomAnswers() { + shuffleArray = (array) => { + for (var i = array.length - 1; i > 0; i--) { + var j = Math.floor(Math.random() * (i + 1)); + [array[i], array[j]] = [array[j], array[i]]; + } + }; + + const answers = [this.correct, ...this.incorrectAnswers]; + const mixedAnswers = shuffleArray(answers); + return mixedAnswers; + } + */ +} From 6a8a32c46df43381475fefec8dae65cf5dd42a9b Mon Sep 17 00:00:00 2001 From: Urszuja Date: Wed, 8 Dec 2021 22:15:44 +0100 Subject: [PATCH 2/2] add gestAnswers - function for answers shuffling --- src/main.js | 7 ++++--- src/model/question.js | 18 +++--------------- 2 files changed, 7 insertions(+), 18 deletions(-) diff --git a/src/main.js b/src/main.js index 925f2eb..42ed6ba 100644 --- a/src/main.js +++ b/src/main.js @@ -9,17 +9,18 @@ document.querySelector('#app').innerHTML = ` const simpleCallback = () => { console.log(`Greetings from koala`); }; + // how to use Question class Question has {imageUrl, correct, incorrectAnswers, question} and .getAnswers method const firstQuestion = new Question( 'url', - 'ula', - ['ala', 'ela', 'ola'], + 'Ala', + ['Ula', 'Ela', 'Ola'], "Mirror, mirror on the wall who's the fairest of them all?", ); console.log(firstQuestion.question); -//show answers +console.log(firstQuestion.getAnswers()); console.log('The correct answer is:'); setTimeout(() => console.log(firstQuestion.correct), 3000); diff --git a/src/model/question.js b/src/model/question.js index 45008ce..507a7c9 100644 --- a/src/model/question.js +++ b/src/model/question.js @@ -6,21 +6,9 @@ export default class Question { this.question = question; } - /*getAnswers = () => { - return this.randomAnswers(); - }; - - randomAnswers() { - shuffleArray = (array) => { - for (var i = array.length - 1; i > 0; i--) { - var j = Math.floor(Math.random() * (i + 1)); - [array[i], array[j]] = [array[j], array[i]]; - } - }; - + getAnswers() { const answers = [this.correct, ...this.incorrectAnswers]; - const mixedAnswers = shuffleArray(answers); - return mixedAnswers; + answers.sort(() => (Math.random() > 0.5 ? 1 : -1)); + return answers; } - */ }