0

I'm putting together a quiz with A/B questions. Every answer has 5 parameters that have to be updated as the user advances through the quiz to show a final results page.

It's very simple but I can't figure out why the parameters aren't updating. This is my first Javascrips project, can somebody point me in the right direction? Thank you!

//The five parameters to be updated

        let totalOrg = 2;
        let totalDel = 2;
        let totalMod = 0;
        let totalLux = 2;
        let totalSer = 2;

// Array with values to modify the parameters for A or B answers

    var valueDataA = [
        [1,2,5,1,3],
        [6,5,1,2,8]
    ];
    
    var valueDataB = [
        [-6,-3,-7,-3,-2],
        [-1,-7,-5,-2,-3]
    ];
    
//Function to add the values to the parameters

    function chooseOptionA() {
        totalOrg = totalOrg + valueDataA[currentQuestion][0];
        totalDel = totalDel + valueDataA[currentQuestion][1];
        totalMod = totalMod + valueDataA[currentQuestion][2];
        totalLux = totalLux + valueDataA[currentQuestion][3];
        totalSer = totalSer + valueDataA[currentQuestion][4];
        console.log(totalParameters);
    };
    
    function chooseOptionB() {
        totalOrg = totalOrg + valueDataB[currentQuestion][0];
        totalDel = totalDel + valueDataB[currentQuestion][1];
        totalMod = totalMod + valueDataB[currentQuestion][2];
        totalLux = totalLux + valueDataB[currentQuestion][3];
        totalSer = totalSer + valueDataB[currentQuestion][4];
        console.log(totalParameters);
    };
    
    let totalParameters = [totalOrg, totalDel, totalMod, totalLux, totalSer];
  • Explain what you want to get or your expected out put? and also share full details that when *chooseoptionA()* or *chooseoptionB()* will be called? and what you want to do in these functions. your question is not clear. – Waqas Altaf Jul 05 '22 at 07:28
  • Are you expecting the values inside `totalParameters` to change, because you assigned new values to `totalOrg`, `totalDel` etc.? Then you should go read https://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language – CBroe Jul 05 '22 at 07:44
  • I suppose you expect `totalParameters` to change when call one of the functions chooseOptionA / chooseOptionB ? This will not happen because the array `totalParameters` is not connected to the references of the variables (`totalOrg`, `totalDel`, ...). When you instantiate the array `let totalParameters = [totalOrg, ...]` the current values of the variables will be copied and that's it. If you now change a value of e.g. `totalOrg` the values in totalParameters array are not affected. – lwohlhart Jul 05 '22 at 07:44

2 Answers2

0

When you place totalOrg in the array totalParameters, you are not placing a pointer to the first variable, but the value, i.e. 2. So the final line of code is no different from:

let totalParameters = [2, 2, 0, 2, 2];

This will clarify why that array is not getting any changes when the functions are called. There are several ways to do this. I will propose one, where the first "parameter" values are stored in a single object, whose property names correspond to your current variable names. You must then adapt the rest of your code to reference those properties:

let totalParameters = {
    totalOrg: 2,
    totalDel: 2,
    totalMod: 0,
    totalLux: 2,
    totalSer: 2
};

var valueDataA = [
    [1,2,5,1,3],
    [6,5,1,2,8]
];

var valueDataB = [
    [-6,-3,-7,-3,-2],
    [-1,-7,-5,-2,-3]
];

function chooseOptionA() {
    totalParameters.totalOrg += valueDataA[currentQuestion][0];
    totalParameters.totalDel += valueDataA[currentQuestion][1];
    totalParameters.totalMod += valueDataA[currentQuestion][2];
    totalParameters.totalLux += valueDataA[currentQuestion][3];
    totalParameters.totalSer += valueDataA[currentQuestion][4];
    console.log(totalParameters);
};

function chooseOptionB() {
    totalParameters.totalOrg += valueDataB[currentQuestion][0];
    totalParameters.totalDel += valueDataB[currentQuestion][1];
    totalParameters.totalMod += valueDataB[currentQuestion][2];
    totalParameters.totalLux += valueDataB[currentQuestion][3];
    totalParameters.totalSer += valueDataB[currentQuestion][4];
    console.log(totalParameters);
};
trincot
  • 317,000
  • 35
  • 244
  • 286
0

Let me pitch you a different approach

//The five parameters to be updated

let totalOrg = 2;
let totalDel = 2;
let totalMod = 0;
let totalLux = 2;
let totalSer = 2;

// Array with values to modify the parameters for A or B answers

var valueDataA = [
    [1,2,5,1,3],
    [6,5,1,2,8]
];

var valueDataB = [
    [-6,-3,-7,-3,-2],
    [-1,-7,-5,-2,-3]
];


/**
 * updateParameters
 *
 * Function to update the values of the parameters
 * 
 * @param {Array<Number>} parameters
 * @param {Array<Number>} parameterUpdates
 * @returns {Array<Number>}
 */
function updateParameters(parameters, parameterUpdates) {
    for (let i = 0; i < parameters.length; i++) {
        parameters[i] += parameterUpdates[i];
    }
    return parameters;
}

let totalParameters = [totalOrg, totalDel, totalMod, totalLux, totalSer];

for (let currentQuestionIndex = 0; currentQuestionIndex < 2 /** questions.length */; currentQuestionIndex++) {
    let option = 'A'; /** actually getUserChoice(); */
    if (option == 'A') {
        totalParameters = updateParameters(totalParameters, valueDataA[currentQuestionIndex])
    } else if(option == 'B') {
        totalParameters = updateParameters(totalParameters, valueDataB[currentQuestionIndex])
    }
    console.log(totalParameters);
    // you can use destructuring assignment to get individual parameters written
    // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
    [totalOrg, totalDel, totalMod, totalLux, totalSer] = totalParameters;
}
lwohlhart
  • 1,829
  • 12
  • 20