From 6b113f5213788850ed2022daa0fc604c7d53a977 Mon Sep 17 00:00:00 2001 From: Weltinio <68684063+Weltinio@users.noreply.github.com> Date: Mon, 14 Dec 2020 20:50:57 +0800 Subject: [PATCH] tinio/fixed bugs for randomization --- main.js | 48 +++++++++++++++++++++++++++++------------------- main1.js | 47 ----------------------------------------------- 2 files changed, 29 insertions(+), 66 deletions(-) delete mode 100644 main1.js diff --git a/main.js b/main.js index 24adada..32d3f02 100644 --- a/main.js +++ b/main.js @@ -3,30 +3,34 @@ let Question = function (question, answer, correctAnswer) { this.answer = answer; this.correctAnswer = correctAnswer; this.askQuestion = function () { - var randomNumber = Math.floor(Math.random() * 3) - randomNumber - questionsArray[randomNumber] + // questionsArray[randomNumber] console.log(this.question) console.table(this.answer) } - this.checkAnswer = function (answer) { - if (answer == this.correctAnswer) { + this.checkAnswer = function () { + var questionnaire = prompt('Write your answer here') + if (questionnaire == this.correctAnswer) { console.log('Correct Answer'); gameScore++; + console.log(gameScore) + start(); + + } else if (questionnaire == 'exit'){ + alert('game aborted'); } else { - console.log('Incorrect Answer'); + console.log('incorrect answer') + start(); } } }; -let isGameRunning = true; -let questionNumber = 0; +// let isGameRunning = true; +// let questionNumber = 0; var question1 = new Question("How many members are there in TWICE?", ["7", "8", "9"], 2); var question2 = new Question("Who is the main dancer in TWICE?", ["Momo", "Dahyun", "Nayeon"], 0); var question3 = new Question("What year did TWICE debut?", ["2014", "2015", "2016"], 1); let questionsArray = [question1, question2, question3]; -var answersArray = [["7", "8", "9"], ["Momo", "Dahyun", "Nayeon"], ["2014", "2015", "2016"]]; var gameScore = 0; @@ -39,17 +43,23 @@ var gameScore = 0; -(function start() { - while (isGameRunning) { - const currentQuestion = questionsArray[questionNumber]; - let tempAnswer = prompt(currentQuestion.askQuestion()); +(start = () => { + // while (isGameRunning) { + var randomNumber = Math.floor(Math.random() * 3) + + questionsArray[randomNumber].askQuestion(); + questionsArray[randomNumber].checkAnswer(); - currentQuestion.checkAnswer(tempAnswer); - console.log('%c Current Score:', 'color: blue; font-size: 14px', gameScore) + // let tempAnswer = prompt(currentQuestion.askQuestion()); - if (tempAnswer == "exit"){ - isGameRunning = false; - } + // currentQuestion.checkAnswer(tempAnswer); + // console.log('%c Current Score:', 'color: blue; font-size: 14px', gameScore) + + // if (tempAnswer == "exit"){ + // // isGameRunning = false; + // alert('Game Aborted!') + // } - } + // } })(); + diff --git a/main1.js b/main1.js deleted file mode 100644 index 577e4c4..0000000 --- a/main1.js +++ /dev/null @@ -1,47 +0,0 @@ -let Question = function (question, answer, correctAnswer) { - this.question = question; - this.answer = answer; - this.correctAnswer = correctAnswer; - this.askQuestion = function () { - var randomNumber = Math.floor(Math.random() * 3) - randomNumber; - questionsArray[randomNumber] - console.log(this.question) - console.log(this.answer) - } - this.checkAnswer = function (answer) { - if (answer == this.correctAnswer) { - console.log('Correct Answer'); - gameScore++ - } else { - console.log('Incorrect Answer'); - } - } -}; - - -let isGameRunning = true; -let questionNumber = 0; -var question1 = new Question("How many members are there in TWICE?", ["7", "8", "9"], '8'); -var question2 = new Question("Who is the main dancer in TWICE?", ["Momo", "Dahyun", "Nayeon"], 'Momo'); -var question3 = new Question("What year did TWICE debut?", ["2014", "2015", "2016"], '2015'); -let questionsArray = [question1, question2, question3]; -var gameScore = 0; - -// BUG 1: change correct answer to string instead of index number -// BUG 2: remove console.log which reveals random number in line 7 - -// BUG 3: introduce randomNumber into question selection -// BUG 4: change console.log display such that options are more readable - - - -(function start() { - while (isGameRunning) { - const currentQuestion = questionsArray[questionNumber]; - let tempAnswer = prompt(currentQuestion.askQuestion()); - currentQuestion.checkAnswer(tempAnswer); - console.log('%c Current Score:', 'color: blue; font-size: 14px', gameScore) - } - -})(); \ No newline at end of file