Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var Question = function (question, answers, correctAnswer){
- this.question = question;
- this.answers = answers;
- this.correctAnswer = correctAnswer;
- };
- // Posible Quiz questions
- var q1 = new Question ("Is JS the coolest programing language?",["1: Yes", "2: No"], 1);
- var q2 = new Question ("Who was your online teacher?", ["1: John", "2: Mark", "3: Jonas", "4: Jane"], 3)
- var q3 = new Question ("Is coding awesome?", ["1: Fuck Yeah", "2: It's ok", "3: Not realy"], 1)
- var questions = [q1, q2, q3];
- function newQ () {
- var score = 0;
- while (input !== 0){
- // Select the question
- var n = Math.floor(Math.random()*questions.length);
- // Print the question
- console.log(questions[n].question + "\n" + questions[n].answers.join('\n'));
- // Get and validate the answer
- var input = parseInt(prompt("Please select correct answer (just type the number).\nEnter \"0\" to quit the game"));
- if (input === questions[n].correctAnswer){
- console.log("That's correct!")
- score = score + 1;
- console.log("-------------------\n Your score is: " + score + "\n-------------------");
- }
- else if (input === 0) {
- console.log("Quiz completed!")
- console.log("-------------------\n Your score is: " + score + "\n-------------------");
- }
- else {
- console.log("Wrong answer.");
- console.log("-------------------\n Your score is: " + score + "\n-------------------");
- };
- };
- return score;
- };
- newQ();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement