Randomized questions based on category

by lia   Last Updated November 14, 2017 16:03 PM

I am developing a webpage for an online exam and I am currently running into a bug. I want to randomize some questions depending on the category. The order of categories are chronological while the questions per category should be randomized. My problem is, my questions don't get completed and I don't know why. If I put while condition, it just sometimes iterates so long that the page stops responding, and sometimes it works. I don't know why.

Here is my code (rand here is the object holder for questions which contain question_id, question_value, and category_id):

 function randomize(rand){
            var ran=[];//the while condition is while (ran.length<rand.length)
            for(var i=0;i<rand.length;i++){
                for(var j=0;j<category_length[i];j++){
                        var x = Math.floor(Math.random() * rand.length);//randomize number
                        var sukat=true;
                    if(categ==rand[x]["category_id"]){
                        for(var y = 0; y < ran.length; y++)//loop checking for duplicates
                            {
                                if(ran[y] == x)
                                {
                                    sukat = false;
                                }
                            }//cheking if duplicate
                            if(sukat)
                            {
                                ran.push(x);
                            }
                            else
                            {
                                y--;
                            }
                    }else{
                        j--;
                    }
                }
                    categ++;
                }


                    return ran;
        }

What am I doing wrong or what can I do to solve this?



Related Questions


calculate choice price

Updated April 21, 2015 04:01 AM

Insert new row into table with same function

Updated January 11, 2017 10:01 AM