Select a random object from JSON

I have the following code:

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

      

This outputs the json from the server and writes it to a variable. Now after that I want to select a random question located in the questions.json doc:

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

      

However, when the console.log()

variable is selected, nothing is returned, it is undefined. Is there something wrong with my code? I've tested it three times and I don't see anything wrong with that, but it could only be my head playing with me.

This is what the json looks like:

"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"
    }...

      

+3


source to share


3 answers


This is because math.random

a function is not a property.

Change it to: Math.random()

and becuase window.questionnaire

is an object that you cannot access using indexes ie (0,1,2)



You can do it:

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);
}

      

+5


source


You can sort the data randomly when you get it from 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, pickRandomQuestion()

you can just take the first element in window.questionnaire

, knowing that it was sorted randomly.

Note: you can always sort the random list within the procedure pickRandomQuestion()

, but I guess you might need some logic in there so that the same random question does not come up as often as it would accidentally, or so at least that pickRandomQuestion()

one won't return the same question as the current one.

+1


source


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);
}

      

0


source







All Articles