87650

Go on to the next element in a javascript quiz so whenever it finishes one it goes on to the next on

Question:

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 number 3 and when it goes to the last number it finishes. Thanks in advance. I am 11 and started programming around 9 months ago

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Are you smarter than a 5th Grader?!!</title> <link rel="stylesheet" href="style.css"> </head> <body onload="starter()" bgcolor="lightblue">

<marquee><font color="red">Make Trivia Great Again!</font></marquee>

<h2><em><center>Are You Smater Than a 5th Grader?</center></em></h2> <button onclick="sc()">Start</button><br> <p id="demo">

<div id="result"></div> <button onclick="reset()">Reset Score</button> <script> function starter(){ setTimeout("clickCounter()",100) setTimeout("minusCounter()",101)} function reset(){ setTimeout("clickCounter()",100) localStorage.clickcount=-1 } function clickCounter() { if(typeof(Storage) !== "undefined") { if (localStorage.clickcount) { localStorage.clickcount = Number(localStorage.clickcount)+1; } else { localStorage.clickcount = 0; } document.getElementById("result").innerHTML = "Score:"+ localStorage.clickcount } else { } } function minusCounter(){ if(typeof(Storage) !== "undefined") { if (localStorage.clickcount) { localStorage.clickcount = Number(localStorage.clickcount)-1; } else { localStorage.clickcount = 0; } document.getElementById("result").innerHTML = "Score:"+ localStorage.clickcount } else { } } if (!("scramble" in Array.prototype)) { Object.defineProperty(Array.prototype, "scramble", { enumerable: false, value: function() { var o, i, ln = this.length; while (ln--) { i = Math.random() * (ln + 1) | 0; o = this[ln]; this[ln] = this[i]; this[i] = o; } return this; } }); } 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" }] function stop(){ alert("stopped") } function sc(){ quiz.forEach(q => q.choices.scramble()); var x = prompt("Select Start question number #:"); if (x >= 6) { alert("please pick a valid question") sc() } else if (x <= 5 && x > 0) { } else if (x == 0) { // x = 0 is assignment, not comparison alert("please pick a valid question") sc() return; // You have to 'return' here // otherwise the code following the else would continue to execute after this inner-sc() returns } else { alert("Please pick a valid question"), sc() } 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")} 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() //x+1 } else if(y=="Cancel"){alert("canceled")} else{ alert("Sorry! \nThe right answer is "+quiz[x].correct); } } } </script> </body> </html>

Answer1:

You seem to have already asked this question <a href="https://stackoverflow.com/questions/44059711/i-want-it-to-go-on-to-the-next-element-in-the-array-in-a-javascript-quiz?rq=1" rel="nofollow">here</a>. On the other hand, there's a problem with your code.

<blockquote>

49.60 Unclosed string.

"question": ["When was the first apple computer made?], </blockquote>

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: <a href="https://www.tutorialspoint.com/javascript/javascript_for_loop.htm" rel="nofollow">https://www.tutorialspoint.com/javascript/javascript_for_loop.htm</a>

And here is an example of the code: <a href="http://codepen.io/AquatuzMagnus/pen/ybzByQ" rel="nofollow">http://codepen.io/AquatuzMagnus/pen/ybzByQ</a>

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.

Answer2:

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 <a href="http://jsbin.com/wocuzajoma/edit?console" rel="nofollow">http://jsbin.com/wocuzajoma/edit?console</a>

Recommend

  • How to count number of Button clicks in Android [closed]
  • Angularjs binding value from async service
  • How to save and retrieve ListItems in Android [duplicate]
  • Updating multiple instances of App Widget in Android
  • jQuery next and prev button
  • NSPopover in NSCollectionView (or not)
  • Button Click Counter in PHP
  • 'Set' in Components of Cake PHP
  • Does Angular assign itself to `window.angular` globally, when loaded as CommonJS module?
  • How to send data along with file in http POST (angularjs + expressjs)?
  • Cannot read property 'addEvent' of undefined
  • Saving Files in silverlight and asynchronous call backs
  • Write “NOT IN” in Doctrine Query Language
  • Will these ActiveXObject and XMLHttpRequest checks apply for any other browser than IE6?
  • Data Type of Columns in a List - R
  • Undefined reference to my own library
  • How to autoplay video in angularjs?
  • How to get or calculate size of Azure File/Share or Service
  • javascript array numerical key resulting in excess “undefined”
  • Where to save the local DB created for iphone app?
  • Can one add a complex type item to ListModel?
  • Execute JavaScript code once a browser window/tab is opened for the first time
  • JavaScript Interface to a Browser not working in 4.2.2
  • How to write string.Contains(someText) in expression Tree
  • Android getAssets in Common class gives “The method getAsssets() is undefined for the type Context”
  • get passwords from chrome
  • Laravel lmutator $this->attributes return 'Undefined index: id'
  • DependencyObject.AssociatedObject is always null
  • pygame.init() shows as undefined variable after installing Pygame
  • Does the Azure table storage API cache results?
  • when does setTimeout start executing in a inline
  • Thread synchronization with syncwarp
  • How do I register classes by both interface and namespace with Windsor?
  • get iframe content as string
  • Angular2 - Template reference inside NgSwitch
  • Getting error 'Cannot read property 'document' of undefined' while importing exp
  • Angular2 Response for preflight is invalid (redirect) from some GET requests
  • Accessing IRQ description array within a module and displaying action names
  • Javascript Callbacks with Object constructor
  • Where to put my custom functions in Wordpress?