0

I'm currently working with an MVC JS framework and I want to be able to get a list of objects that I can take a random entry out of on a loop. So far I've managed to create a function that finds a random ID and pulls out that object so that part is not a problem. It's what is going into the array of objects:

QuestionsSetup: function(gameType) {

        // Setup Resources
        var c = this.View.children;
        var player1qs = [];
        var leftQ = 0;
        var rightQ = 0;
        var maxQValue = 50;
        var minQValue = 1;

        // Fill array with questions 
        for (var i = 0; i < 5; i++) {

            // Build a random question with numbers between 1 and 50

            // Build Question Text to output to user

            // Generate correct answers based on generated question

            // Generate unsorted, incorrect answers and add them to an array

            //Place Questions into object
            questions.qId = i;
            questions.leftQ = leftQ;
            questions.rightQ = rightQ;
            questions.correctAnswer = correctAnswer;
            questions.allAnswers = sortedAnswers;
            questions.questionText = questionText;

            //Add to array of questions
            player1qs.push(questions);
        }
    }

This does add them to an array but when adding a new object it also changes the values of the existing objects in the array so they all come out the same no matter which one I pull out later. The questions object is declared in it's own file in a models folder. Is there any way, at the start of each loop, to tell the application I want new empty questions object as opposed to referencing the existing ones? I know that you can in similar back end languguages so I refuse to beleive that something so simple doesn't exist in JavaScript too?

4
  • 3
    There's way too much code, try to come with a minimal example please Commented Jan 25, 2017 at 10:40
  • 1
    Simplified it to the relevant parts Commented Jan 25, 2017 at 10:42
  • Its passing by reference, so updating the original data is updating the original referenced data. You should use a new questions object each time? Commented Jan 25, 2017 at 10:44
  • where is you 'questions' defined? is it in global space? you overwrite the properties with every iteration and push to array always the same object. Commented Jan 25, 2017 at 10:45

3 Answers 3

1

Declaring a variable for each array item is definitely missing.

QuestionsSetup: function(gameType) {

    // Setup Resources
    var c = this.View.children;
    var player1qs = [];
    var leftQ = 0;
    var rightQ = 0;
    var maxQValue = 50;
    var minQValue = 1;

    // Fill array with questions 
    for (var i = 0; i < 5; i++) {

        var tempQuestion = {
           qId: i,
           leftQ: leftQ,
           rightQ: rightQ,
           correctAnswer: correctAnswer,
           allAnswers: sortedAnswers,
           questionText: questionText
        }


        // ...


        //Add to array of questions
        player1qs.push(tempQuestion);
    }
}

Using a separate closure inside a loop also might be a good idea.

Sign up to request clarification or add additional context in comments.

Comments

0

do this:

for (var i = 0; i < 5; i++) {
   let questions = {};
   // the rest....

you need to define the object first.

Comments

0

Maybe you should just initialize the questions object before initializing its properties, so the code should look like this:

       //Place Questions into object
        questions = {};

        questions.qId = i;
        questions.leftQ = leftQ;
        questions.rightQ = rightQ;
        questions.correctAnswer = correctAnswer;
        questions.allAnswers = sortedAnswers;
        questions.questionText = questionText;

        //Add to array of questions
        player1qs.push(questions);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.