Shuffle problem


#1

Hi, I’m having a problem with my code, can you please help me, I’m making a quiz application , my problem is everytime I proceed into the next question it keeps repeating the same question , I used "fisher-yates-shuffle to randomized the questions in my app.I want that all the questions I’ve pass through will no longer repeat.Can you please help me thanks.

Heres my code:

.factory('wordFactory', function() {

    var questions = [
        {
            question: "It  cannot be changed while program is running.",
            wordClue: "STATNCO",
            answer: "constant"
           
        },
        {
            question: "Might change while program is running.",
            wordClue: "EBVAIRAL",
            answer: "variable"
          
        },
        {
            question: "Assignment made when declaring variable",
            wordClue: "TZNEIIIIAL",
            answer: "initialize"
            
        },
        {
            question: "Controls data values used to set variable.Also called accessor.",
            wordClue: "TSE THMEOD",
            answer: "set method"
            
        },
        {
            question: "Numeric variable concatenated to String using plus sign.",
            wordClue: "ACNATEONTEC",
            answer: "concatenate"
            
        }
];

//This is the fisher-yates-shuffle algorithm
    var shuffleArray = function(array) {
      var m = array.length, t, i;
    
      // While there remain elements to shuffle
      while (m) {
        // Pick a remaining element…
        i = Math.floor(Math.random() * m--);
    
        // And swap it with the current element.
        t = array[m];
        array[m] = array[i];
        array[i] = t;
      }
    
      return array;
    }
    
    
        return {
    
            getQuestion: function(id) {
               
                if(id < questions.length  ) {
                    return shuffleArray(questions)[id]; //I've insert the shuffleArray(questions) here
    
                } else {
                    return false;
                }
            }
    
    
        };

#2

eehhhmm

do not know anything about fisher-yates… but i would copy the array of questions --> select one (slice it out of the array)
–> user ansers --> select text one (slice it out) …

So you can modify your shuffle function to only make i = Math.floor(Math.random() * tnpArray.length - 1);
Problem is you not slicing your used item out… so it is in the array all the time… so it could be shown again and again…


#3

what is the tnpArray ?


#4

tmpArray is a copy of your questions.

Because you have to manipulate them, but you need all questions again if you rerun your question-functionality.