4

I have the following code:

$.getJSON('js/questions1.json').done(function(data){
        window.questionnaire = data;
        console.log(window.questionnaire);
        startGame();
    });

This brings a json from the server and logs it into a variable. Now after this, I want to select a random question located in the questions.json document:

function pickRandomQuestion(){
        window.selectedquestion = window.questionnaire[Math.floor(Math.random * window.questionnaire.length)];
        console.log(window.selectedquestion);
        console.log(window.questionnaire);
    }

However, when console.log() the selectedquestion variable, nothing comes back, it's undefined. Is there something wrong with my code? I've tripled checked it and I see nothing bad on it, but it might just be my head playing games with me.

Here's how the json looks:

"q1" : {
        "question" : "This country is one of the largest wine-producing countries of the world, where wine is grown in every region of the country. Which country is this?",
        "a"        : "France",
        "b"        : "Italy",
        "c"        : "Germany",
        "d"        : "Australia",
        "corrrect" : "b"
    },
    "q2" : {
        "question" : "What is the name for the type of art portrait that deliberately exaggerates a person?",
        "a"        : "Environmental",
        "b"        : "Cartooning",
        "c"        : "Caricature",
        "d"        : "Tribal",
        "corrrect" : "c"
    },
    "q3" : {
        "question" : "Who was the first president of the United States?",
        "a"        : "Abraham Lincoln",
        "b"        : "Ronald Reagan",
        "c"        : "George Washington",
        "d"        : "Barack Obama",
        "corrrect" : "c"
    }...
CodeTrooper
  • 1,890
  • 6
  • 32
  • 54

3 Answers3

7

That's because math.random is function not a property.

Change it to: Math.random()

and becuase window.questionnaire is an object you can't access it using indexes i.e(0,1,2)

you can do this:

function pickRandomQuestion(){
        var obj_keys = Object.keys(window.questionnaire);
        var ran_key = obj_keys[Math.floor(Math.random() *obj_keys.length)];
        window.selectedquestion = window.questionnaire[ran_key];
        console.log(window.selectedquestion);
        console.log(window.questionnaire);
}
Omar Elawady
  • 3,300
  • 1
  • 13
  • 17
1

You can sort the data randomly whenever you get it from the json:

data.sort(function() { return .5 - Math.random();});

$.getJSON('js/questions1.json').done(function(data){
    window.questionnaire = data;
    window.questionnaire.sort(function() { return .5 - Math.random();});
    console.log(window.questionnaire);
    startGame();
});

Then, in the pickRandomQuestion() you can just take the first element in window.questionnaire, knowing that it was randomly sorted.

Note: you could also always randomly sort the list inside the pickRandomQuestion() routine, but I imagine you might want some logic in there so that the same random question doesn't come up as often as it would randomly, or so at least pickRandomQuestion() won't return the same question as the current one.

Alan Files
  • 318
  • 1
  • 9
0

I think this should work

function pickRandomQuestion(){
    window.selectedquestion = window.questionnaire['q' + Math.floor(Math.random() * window.questionnaire.length)];
    console.log(window.selectedquestion);
    console.log(window.questionnaire);
}
pieperz
  • 62
  • 2
  • 8
  • 1
    It doesn't look like `window.questionnaire` will have a `length` property. You probably need to do something like `Object.keys(window.questionnaire).reduce(function (length, key) { return length + (window.questionnaire.hasOwnProperty(key) ? 1 : 0); }, 0)` – redbmk May 05 '15 at 19:55