1
<script type="text/javascript">

var questions = [
    ['http://i.imgur.com/k0fBtYP.png','0'],
    ['http://i.imgur.com/1PwDTVY.png','1'],
    ['http://i.imgur.com/QKE9UFA.png','2'],
    ['http://i.imgur.com/XEGhtgB.png','3'],
    ['http://i.imgur.com/QhnCkAp.png','4'],
    ['http://i.imgur.com/JoL8tco.png','5'],
    ['http://i.imgur.com/tcZNls4.png','6'],
    ['http://i.imgur.com/V9DQI0p.png','7'],
    ['http://i.imgur.com/39ePipM.png','8'],
    ['http://i.imgur.com/16yFeMy.png','9'],
    ['http://i.imgur.com/UUo2yNc.png','10'],
    ['http://i.imgur.com/5sza6Wm.png','11'],
    ['http://i.imgur.com/ygPZBdY.png','12'],
    ['http://i.imgur.com/SwJYBRR.png','13'],
    ['http://i.imgur.com/wNdpJBX.png','14'],
    ['http://i.imgur.com/wUS7pDs.png','15'],
    ['http://i.imgur.com/OEI6ZYX.png','16']
];
var qNo = 0;
var correct = 0;
var cnt = 0;

function NextQuestion(response) {
  if ((qNo < questions.length) && (response == questions[qNo][1])) { correct++; }
  document.getElementById('score').innerHTML 
    = 'Your score is '+correct+'/'+(cnt+1);
  qNo++;
  if (qNo < questions.length) { document.getElementById('Pic').src = questions[qNo][0]; cnt++; }
                     else { alert('Quiz is done'); }
}
onload = function() {
 document.getElementById('Pic').src = questions[0][0];
}

</script>

Okay so basically I have 17 questions and 17 answers that are listed using the numbers 0-16, what I want to do is randomize the order in which the pictures are shown so that you cant just find a pattern and not answer it right, also and I cant figure this out, I want to make it so that after each question is answered a green Correct or a Red incorrect shows up depending on If you got the previous question right can anyone help?

Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
  • 2
    Make use of [Math.random](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random). – Nonemoticoner Sep 27 '15 at 22:45
  • Typically you'd randomly splice one member from the array until there are none left. Or, if you don't want modify the original array, you can build a randomised index of the members of *question* and splice members from it. Highlighting answers for right or wrong can be done by adding or removing a class value. – RobG Sep 27 '15 at 22:46
  • So how would I implement that into my code is what I'm getting at. – Jacob Stewart Sep 27 '15 at 22:49
  • You write the code, we'll fix your problems is what we're getting at. ;-) – RobG Sep 27 '15 at 22:51

2 Answers2

1

sorry, presently i have to rush so i won't be able to give you a code but I can give you the steps you need to follow.

  1. Shuffle the array How to randomize (shuffle) a JavaScript array
  2. Write a simple loop that cycles through the array
Community
  • 1
  • 1
sudo_dudo
  • 573
  • 1
  • 5
  • 18
0

Take a look at this. Once you get a value between 0 and 16, store it as i, then display the question that corresponds with i. After you get the answer, remove that question and answer from the array by questions.splice(i, i+1); and answers.splice(i, i+1); Now repeat this for each question and eventually, the questions and answers array will reach a length of 0. This means that the user answered all of the questions.

Community
  • 1
  • 1
DarkHorse
  • 963
  • 1
  • 10
  • 28