Skip to content Skip to sidebar Skip to footer

Go On To The Next Element In A Javascript Quiz So Whenever It Finishes One It Goes On To The Next One

Whenever you finish answering a question it ends the program in the code below. Is there anyway to make it so that whenever you finish a question maybe number 2, it goes on to numb

Solution 1:

You seem to have already asked this question here. On the other hand, there's a problem with your code.

49.60 Unclosed string.

"question": ["When was the first apple computer made?],

I've written the code for you. I've used a simple JavaScript for loop, and here is the updated sc() function:

functionsc(){
  for (x = 1; x < 6; x++){ //this is the vital part
    quiz.forEach(q => q.choices.scramble());
    var ans = ""functionmyFunction(item, index) {
      ans += "\n[" + (index+1) + "]: " + item ; 
    }
    quiz[x].choices.forEach(myFunction);
    var y = prompt(quiz[x].question+"\nYour anwser is:"+ans);
    if (y == quiz[x].correct){
      alert("Correct!")
      clickCounter()
    }
    elseif(y=="Cancel"){
      alert("canceled")
      return; //This closes the box
    }
    else{
      alert("Wrong! Please Try Again.");
      repeat()
    }
    functionrepeat(){
      quiz.forEach(q => q.choices.scramble());
      var ans = ""functionmyFunction(item, index) {
        ans += "\n[" + (index+1) + "]: " + item ; 
      }
      quiz[x].choices.forEach(myFunction);
      var y = prompt(quiz[x].question+"\nYour anwser is:"+ans);
      if (y == quiz[x].correct){
        alert("Correct!,Good Job")
        clickCounter()
      } elseif(y=="Cancel"){
        alert("canceled")
      } else {
        alert("Sorry! \nThe right answer is "+quiz[x].correct);
      }
    }
  } //This is also vital
}

You can read more about for loops here: https://www.tutorialspoint.com/javascript/javascript_for_loop.htm

And here is an example of the code: http://codepen.io/JamesDouglas/pen/ybzByQ

P.S. It's quite annoying not being able to close the box, so i've added onto your code that closes it. It would also be helpful to notify users that typing "Cancel" will close the box, as I only found out after looking at the code.

Solution 2:

As you already know, your code is really beautiful :/ . Lets clean it up a bit:

var quiz = [{
    "question": ["When was the first apple computer made?"],
    "choices": ["1904","1976","1978","2004"],
    "correct": ["1976"]
}, {
    "question": "Who is the founder of Microsoft?",
    "choices": ["Bill Gates", "Steve Jobs", "Steve Wozniak" , "Martin Shaba"],
    "correct": "Bill Gates"
}, {
    "question": "What was your first dream?",
    "choices": ["8 bits", "64 bits", "1024 bits"],
    "correct": "8 bits"
}, {
   "question": "The C programming language was developed by?",
   "choices": ["Brendan Eich", "Dennis Ritchie", "Guido van Rossum"],
   "correct": "Dennis Ritchie"
}, {
  "question": "What does CC mean in emails?",
  "choices": ["Carbon Copy", "Creative Commons", "other"],
  "correct": "Carbon Copy"
}, {
    "question": "What is the full for of IP",
    "choices": ["Internet provider", "Intenet Port", "Other","Internet Protocol"],
    "correct": "Other"
}];
 quiz.forEach(question=>question.choices.scramble());

var score=0;

functionask(index){
      index= (index||0) % quiz.length; //let index be validvar question=quiz[index];
      //ask the questionvar answer=prompt(question.question+" ["+question.choices.join("] [")+"]");
      //exit on falseif(!answer) returnalert("Goodbye! Your score is "+score);
      //check if user was rightif(answer===question.correct){
        alert("Yes. Youre right!");
        score++;
     }else{
       alert("Nope it was "+question.correct);
    }
   //procceed to the next questionask(index+1);//next question
}

//lets startask(0);

You can play it at http://jsbin.com/wocuzajoma/edit?console

Post a Comment for "Go On To The Next Element In A Javascript Quiz So Whenever It Finishes One It Goes On To The Next One"