在数组中添加值,然后返回true选项

时间:2016-07-20 22:35:13

标签: javascript jquery arrays

我对一个我正在做测验的脚本有疑问。在我的最后一个测验中,我提出了3个答案选择:A,B和C.如果答案是“A”,则“X”选项的3个点,“Y”选项的2个点和“Z”选项的1个点。如果答案是“B”,则选项“X”加2分,“Y”选项加3分,“Z”选项加1分。如果答案为“C”,则选项“X”为1分,“Y”选项为2分,“Z”选项为3分。

该函数检查选项A,B和C的点,并返回具有最高点数的选项。

但我想改变:

如果答案是“A”积累4分。如果答案是“B”积累2分。如果答案是“C”则积累0分。

如果点总数等于或小于10,则返回“X”选项。如果总点数大于10且小于24则返回“Y”选项。如果总点数大于24,则返回“Z”选项。

代码:

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;
};
}]);

1 个答案:

答案 0 :(得分:0)

var result = getResult();

function getResult(){
    var totalScore = 0;
    for (var i = 0; i < self.userAnswers.length; i++) {
      totalScore += self.userAnswers[i].values[0]; // assuming "values" is an array of equal values: [4,4,4],[2,2,2] or [0,0,0]
    }
    if(totalScore <= 10){
        return self.jobs[0]
    } else if (totalScore < 24) {
        return self.jobs[1]
    } else {
        return self.jobs[2]
    }
}