0

I have a quiz and want to randomize the data coming from my jSON file so that every time someone attempt the quiz sees different question order.

I came across this example but couldn't make it work out with the array comming from my jSON file. See the JS bellow:

.controller('QuizController', ['$scope', '$http', function($scope, $http, $state){
  $scope.score = 0;
  $scope.activeQuestion = -1;
  $scope.activeQuestionAnswered = 0;
  $scope.percentage= 0;

  $http.get('js/quiz_data.json').then(function(quizData){
    $scope.myQuestions = quizData.data;
    $scope.totalQuestions = $scope.myQuestions.length;
  });

  $scope.randomSort = function(myQuestion) {
    return Math.random();
  };

HTML:

ng-repeat="myQuestion in myQuestions|orderBy:randomSort">

It would be nice if the answers were in random order also... Thanks in advance!

Community
  • 1
  • 1
  • Can you supply an example of the JSON? – AnotherDeveloper Aug 27 '15 at 17:33
  • Yes, it's like that: { "question" : "Qual o número total de biomas no Brasil?", "answers" : [ {"id" : 0, "text" : "27"}, {"id" : 1, "text" : "5"}, {"id" : 2, "text" : "6"}, {"id" : 3, "text" : "26"} ], "correct" : 2 }, – Rafael Ortman Aug 27 '15 at 22:08
  • Is it feasible to return the results from the `GET` already randomly ordered? Like push that logic to the back end? – ryanyuyu Aug 28 '15 at 13:46

1 Answers1

1

This might be helpful How to randomize (shuffle) a JavaScript array?

You can use the same concept on a JSON Object

EDIT

 function createobj() {
        var obj = [
            {"Question":"This is the first question","Answer":"This is the first Answer"},
            {"Question":"This is the second question","Answer":"This is the second Answer"},
            {"Question":"This is the third question","Answer":"This is the third Answer"},
            {"Question":"This is the forth question","Answer":"This is the forth Answer"},
            {"Question":"This is the fifth question","Answer":"This is the fifth Answer"},
        ];
    //Assigning the obj to the new one
        obj = randomize(obj);
    }

    function randomize (obj) {
        var index;
        var temp;
        for (var i = obj.length - 1; i > 0; i--) {
            //get random number
            index = Math.floor((Math.random() * i));
            //swapping
            temp = obj[index];
            obj[index] = obj[i];
            obj[i] = temp;
        }
        //Prints the results into the console
        for (i = 0; i < obj.length; i++) {
            console.log(obj[i].Question + ": " + obj[i].Answer);
        }
//My edit to pass the obj back to the function
        return obj;

    }

Here is the function, it takes a simple JSON object i created and randomizes it. It will print the results into the Console. Hope this helps more.
Community
  • 1
  • 1
Jake Schuurmans
  • 169
  • 2
  • 3
  • 12
  • Hi thanks for your answer. I don't really know how to address my problem with your tip. Even with an example closer to the context I couldn't solve the issue. Check it out: http://jsfiddle.net/q6kv7/ – Rafael Ortman Aug 27 '15 at 22:07
  • Editing my answer, i made a simple randomizer for you. – Jake Schuurmans Aug 28 '15 at 13:39
  • Thanks, Jason! Excuse my lack of js knowledge but, on your answer, you passed the json directly on the function, how should I "link" the json to the variable? – Rafael Ortman Sep 08 '15 at 13:54
  • IDK who jason is, but what do you mean link? if you mean returning the randomized json obj back to the function, ill edit my answer to show you what i think you want. – Jake Schuurmans Sep 08 '15 at 13:58
  • Sorry, Jake! When I said "link" I meant my source JSON file: 'js/quiz_data.json'. Thank you!!! – Rafael Ortman Sep 08 '15 at 21:27
  • OH, you mean to OPEN the file. here are two other questions that have answered that, using and not using jquery http://stackoverflow.com/questions/9838812/how-can-i-open-a-json-file-in-javascript-without-jquery http://stackoverflow.com/questions/7346563/loading-local-json-file – Jake Schuurmans Sep 08 '15 at 21:58