0

is there anyway to make..

var init = { 
 'questions': [ 
   {
       'question': 'jQuery is a...',
       'answers': ['JavaScript library','Ruby Gem','PHP Framework','None of the above'],
          'correctAnswer': 1
   },
   {
       'question': 'X comes after?',
       'answers': ['P','W','Y','v'],
          'correctAnswer': 2
   },
     {
       'question': 'Meh stands for..',
       'answers': ['Eh','/Sigh','What?','Whatever'],
          'correctAnswer': 4
   }
 ] };

Right now system shows me the question in the way it's written. So is there a way that i could make these questions random shown? Thank you.

2 Answers2

4

You seem to want this :

var questions = init.questions;
var question = questions[Math.floor(Math.random()*questions.length)]

This gives you a random element of the questions array.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • Umh, i'm a noobie at this, so how should i implement this code you gaved into the code who handels var init, and displays the code? pastebin.com/2se0vwDC – user2468381 Jun 09 '13 at 13:55
  • It totally depends on how is your current code. If your code takes a question index, then it's `Math.floor(Math.random()*questions.length)`. – Denys Séguret Jun 09 '13 at 13:56
  • pastebin.com/2se0vwDC Well here's the code, it's messy as hell but maybe you can figure it out? I'm really stuck on this. – user2468381 Jun 09 '13 at 13:58
  • There's a for `loop` starting at line 34. At the start of the loop add `var index = Math.floor(Math.random()*questions.length)` and the replace all `questions[questionsIteratorIndex]` with `questions[index]`. – Denys Séguret Jun 09 '13 at 14:01
1

There are two possible angles to this question; the original question isn't clear which one is meant (or possibly both), so I'll cover them both.

  1. Randomising the order that the questions are shown in.

  2. Randomising the order of the answers within any given question.

For the first of those points -- random order for the questions, given that all the questions are in the array already, we just need to shuffle the array. This is a common enough task, and there are a number of code examples around demonstrating how to do it. I suggest reading some of the answers to this question: How can I shuffle an array?

The answers on that question include code snippets that will do exactly what you need.

The second part is basically the same problem, but is marginally more complex due to the need to keep track which answer is the correct one.

The basic shuffle would be the same, but you'd need to have something that was shuffled with each answer indicating which one is right, rather than a single separate value pointing to an array key.

So, for example, your question data might look like this:

{
   'question': 'X comes after?',
   'answers': [
       {'answer':'P','correct':false},
       {'answer':'W','correct':true},
       {'answer':'Y','correct':false},
       {'answer':'v','correct':false}
   ]
},

Now it's easy to use the same shuffle technique on the answers as on the questions, without losing track of which answer is correct.

Hope that helps.

Community
  • 1
  • 1
Spudley
  • 166,037
  • 39
  • 233
  • 307