Sum values in vector and return the value in true condition

Asked

Viewed 111 times

4

I have a doubt in a script where I am doing a quiz, in my last quiz I put 3 response options: A, B and C. Where if the answer is A sum 3 points for option X, 2 points for option Y and 1 point for Option Z. If the answer is B adds 2 points for option X, 3 points for option Y and 1 point for Option Z. If the answer is C sum 1 point for option X, 2 points for option Y and 3 points for Option Z.

Then the function checks which Option has more points and returns it.

But I want to modify to the following function:

If answer is A sum 4 points. If answer is B sum 2 points. If answer is C sum 0 points.

If the number of points is up to 10 returns Option X. If the number of points is 11 to 24 returns Option Y. If the number of points is greater than 24 returns Option Z.

Follows part of the code:

self.jobs = [
    {
        title: 'Opção X',
        description: 'Descrição Opção X'
    },
    {
        title: 'Opção Y',
        description: 'Descrição Opção Y'
    },
    {
        title: 'Opção Z',
        description: 'Descrição Opção Z'
    }
    ];

self.job = 9999;

self.questions = [
    {
        text: 'Pergunta 1',
        answered: false,
        skipped: false,
        answers: [
            {
                text: 'A',
                selected: false,
                values: [3, 2, 1]
            },
            {
                text: 'B',
                selected: false,
                values: [2, 3, 1]
            },
            {
                text: 'C',
                selected: false,
                values: [1, 2, 3]
            }
        ]
    },
    {
        text: 'Pergunta 2',
        answered: false,
        skipped: false,
        answers: [
            {
                text: 'A',
                selected: false,
                values: [3, 2, 1]
            },
            {
                text: 'B',
                selected: false,
                values: [2, 3, 1]
            },
            {
                text: 'C',
                selected: false,
                values: [1, 2, 3]
            }
        ]
    },
    {
        text: 'Pergunta 3',
        answered: false,
        skipped: false,
        answers: [
            {
                text: 'A',
                selected: false,
                values: [3, 2, 1]
            },
            {
                text: 'B',
                selected: false,
                values: [2, 3, 1]
            },
            {
                text: 'C',
                selected: false,
                values: [1, 2, 3]
            }
        ]
    }
];

self.index = 0;

self.userAnswers = [];

self.error = false;

self.answerQuestion = function(elem) {
    self.userAnswers[self.index] = elem.answer;
    self.index = elem.$parent.$index + 1;

    var question = elem.$parent.question;
    question.answered = true;

    angular.forEach(question.answers, function(answer) {
        answer.selected = false;
    });

    elem.answer.selected = true;
};

self.changeQuestion = function(elem) {
    var question = self.questions[self.index];
    if(question && !question.answered)
        question.skipped = true;

    self.index = elem.$index;
};

self.goToResult = function() {
    var question = self.questions[self.index];
    if(question && !question.answered)
        question.skipped = true;

    self.index = self.questions.length;
}

self.viewResult = function() {
    var hasError = false;

    angular.forEach(self.questions, function(question) {
        if(!question.answered) {
            hasError = true;
            question.skipped = true;
        }
    });

    if(hasError) {
        self.error = true;

        $timeout(function() {
            self.error = false;
        }, 2000);

        return false;
    }

    self.showQuiz = false;

    var result = getResult();
    self.job = result;
};

getResult = function() {
    var results = [0, 0, 0];
    var maior = -9999;
    var iMaior = 0;

    angular.forEach(self.userAnswers, function(answer) {
        for(var i=0; i < self.jobs.length; i++) {
            results[i] += answer.values[i];
        }
    });

    for(var i=0; i < results.length; i++) {
        var result = results[i];
        if(result > maior) {
            maior = result;
            iMaior = i;
        }
    }

    return iMaior;
};
}]);
  • Anyone can help?

  • You managed to solve?

1 answer

1


To solve the problem of the value of each letter, one must change the arrays [3, 2, 1], [2, 3, 1] and [1, 2, 3] for [4, 2, 0], [2, 4, 0] and [0, 2, 4] (respectively and in the 3 questions). The score you test in the function "getResult", modifying the end of it:

getResult = function() {
    var results = [0, 0, 0];
    var soma = 0;

    angular.forEach(self.userAnswers, function(answer) {
        for(var i=0; i < self.jobs.length; i++) {
            results[i] += answer.values[i];
        }
    });

    for(var i=0; i < results.length; i++) {
        soma += results[i];
    }

    if (soma > 24)
        return 2; // Z
    else if (soma >= 11 && soma <= 24)
        return 1; // Y
    else
        return 0; // X
};

I suppose that’s it!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.