There is a quiz. In it, the user guesses one of 3 options. Each option has its own weight:

  • Option 1: 20 points
  • Option 2: 30 points
  • Option 3: 50 points

If the player guesses the option, then he gets all the weight. If not, then only its part, the distribution of which is strictly regulated.

Ответ игрока || Правильный ответ || Очки 1 1 100% 2 1 75% 3 1 50% 1 2 75% 2 2 100% 3 2 75% 1 3 25% 2 3 50% 3 3 100% 

You can easily write something in the spirit:

 calcRateScore: function(fact, user) { var rateScore = 0; switch (fact) { case 1: switch (user) { case 1: rateScore = 20; break; case 2: rateScore = 20 * 0.75; break; case 3: rateScore = 20 * 0.5; break; } break; case 2: switch (user) { case 1: rateScore = 30 * 0.75; break; case 2: rateScore = 30; break; case 3: rateScore = 30 * 0.75; break; } break; case 3: switch (user) { case 1: rateScore = 50 * 0.25; break; case 2: rateScore = 50 * 0.5; break; case 3: rateScore = 50; break; } break; } return rateScore; } 

But it looks just awful. Tell me how to get rid of these switch / case (preferably from all)?

    1 answer 1

     var answers = [ null, { weight: 20, fractions: [0, 1, 0.75, 0.5] }, { weight: 30, fractions: [0, 0.75, 1, 0.75] }, { weight: 50, fractions: [0, 0.25, 0.5, 1] } ]; calcRateScore: function(fact, user) { var rateScore = 0; if (answers[fact] && answers[fact].fractions[user]) { rateScore = answers[fact].weight * answers[fact].fractions[user]; } return rateScore; }