Go On To The Next Element In A Javascript Quiz So Whenever It Finishes One It Goes On To The Next One
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:
function sc(){
for (x = 1; x < 6; x++){ //this is the vital part
quiz.forEach(q => q.choices.scramble());
var ans = ""
function myFunction(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()
}
else if(y=="Cancel"){
alert("canceled")
return; //This closes the box
}
else{
alert("Wrong! Please Try Again.");
repeat()
}
function repeat(){
quiz.forEach(q => q.choices.scramble());
var ans = ""
function myFunction(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()
} else if(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;
function ask(index){
index= (index||0) % quiz.length; //let index be valid
var question=quiz[index];
//ask the question
var answer=prompt(question.question+" ["+question.choices.join("] [")+"]");
//exit on false
if(!answer) return alert("Goodbye! Your score is "+score);
//check if user was right
if(answer===question.correct){
alert("Yes. Youre right!");
score++;
}else{
alert("Nope it was "+question.correct);
}
//procceed to the next question
ask(index+1);//next question
}
//lets start
ask(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"