Add values to vector and return value in true condition

4

I have a question in a script where I am doing a Quiz, in my last Quiz my I put 3 answer options: A, B and C. Where if the answer is The sum 3 points for option X, 2 points for option Y and 1 point for Option Z. If the answer is B sum 2 points for option X, 3 points for option Y and 1 point for Option Z. If the answer is C adds 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 it to the following function:

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

If the number of points is up to 10, return Option X. If the number of points is from 11 to 24, return option Y. If the number of points is greater than 24, return Option Z.

Here's 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;
};
}]);
    
asked by anonymous 20.07.2016 / 05:39

1 answer

1
To resolve the problem of the value of each letter, you should change the arrays [3, 2, 1] , [2, 3, 1] [1,2,3] [4, 2, 0] , [2, 4, 0], and [0, 2, 4] (respectively and in the 3 questions). The score you test on the "getResult" function, 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 imagine it to be this!

    
13.09.2016 / 15:58