Skip to content Skip to sidebar Skip to footer

Ranking Algorithm Base On Multiple Condition/factor

I'm writing a mini game but stuck in the end. It's a simple quiz where it rank user by the number of question correctly answered and the time taken to finish the set of questions.

Solution 1:

You can process several conditions in a single pass in the comparison function of Array.prototype.sort().

Example:

var score = [];

storeScore('user1', 7, 65); // 7 correct answers in 65 sec.storeScore('user2', 8, 70); // 8 correct answers in 70 sec.storeScore('user3', 6, 50); // 6 correct answers in 50 sec.storeScore('user4', 7, 50); // 7 correct answers in 50 sec.

score = score.sort(function(a, b) {
  return b.correct > a.correct || (b.correct == a.correct && b.time < a.time);
});

for(var id in score) {
  console.log(
    '#' + ((id | 0) + 1),
    score[id].userId,
    score[id].correct + ' correct answers in ' +
    score[id].time + ' seconds'
  );
}

functionstoreScore(userId, correct, time) {
  score.push({
    userId : userId,
    correct: correct,
    time   : time
  });
}

Output:

#1 user2 8 correct answers in 70 seconds#2 user4 7 correct answers in 50 seconds#3 user1 7 correct answers in 65 seconds#4 user3 6 correct answers in 50 seconds

Solution 2:

Play around with the relative weight of the two conditions.

What I mean is simple. Think of the following equation:

weight(user) = a * parameter_1 + b *  parameter_2

In your case the parameters are number_of_correct answers and time_spent, but this is true for any number of parameters (shortest time to answer a single question, number of retries, whatever).

There are no "correct" values to a and b, only what works for you. I would play around with some different values for them, see how your ranking list ends up and then decide on them.

This is a common question when ranking results of test with multiple data points per test - how do you decide which kind of data point is more important.

Note: For two parameters you don't really need two parameters, one is enough (same as saying a=1).

Solution 3:

You can use Promise.race(), Promise.all(), onsubmit event of <form> element

var forms = [].slice.call(document.querySelectorAll("[id^=form]")),
progress = newDate().getTime();

Promise.race(
  forms.map(function(form, index) {
    returnnewPromise(function(resolve) {
      form.addEventListener("submit", function(e) {
        resolve.call(form, [form.id, newDate().getTime() - progress]);
      });
    })
  })
)
.then(function(res) {
  console.log(res)
  alert(res[0] + " completed quiz fastest at " + res[1])
});

Promise.all(
  forms.map(function(form, index) {
    returnnewPromise(function(resolve) {
      form.addEventListener("submit", function(e) {
        resolve.call(form, [form.id, newDate().getTime() - progress]);
      });
    })
  })
)
.then(function(quiz) {
  if (quiz[0][1] < quiz[1][1]) {
    alert(quiz[0][0] + " wins quiz")
  } else {
    alert(quiz[1][0] + " wins quiz")
  }
})
<formid="form1"><label>user 1</label>
  5+5=10<inputname="correct1"type="checkbox"required>
  5+6=12<inputname="incorrect1"type="checkbox"required>
  5+2=7<inputname="correct2"type="checkbox"required>
  7+7=15<inputname="incorrect2"type="checkbox"required><inputtype="submit"></form><formid="form2"><label>user 2</label>
  5+5=10<inputname="correct1"type="checkbox"required>
  5+6=12<inputname="incorrect1"type="checkbox"required>
  5+2=7<inputname="correct2"type="checkbox"required>
  7+7=15<inputname="incorrect2"type="checkbox"required><inputtype="submit"></form>

Post a Comment for "Ranking Algorithm Base On Multiple Condition/factor"