Untitled

mail@pastecode.io avatar
unknown
javascript
2 years ago
1.2 kB
2
Indexable
Never
var matchTable = {
  "A": "X",
  "B": "Y",
  "C": "Z"
}

var pointTable = {
  "X": 1,
  "Y": 2,
  "Z": 3
}

const defeatTable = {
  "A" : "Z",
  "B" : "X",
  "C" : "Y"
}

const reverseDefeatTable = {
  "A" : "Y",
  "B" : "Z",
  "C" : "X"
}

const DRAW_VALUE = 3;
const WIN_VALUE = 6;

function calculateJokempo(results){
  var sum = 0;
  results.forEach(element => {
    
    const player1 = element.split(" ")[0];
    var player2 = element.split(" ")[1];

    if(player2==="Y"){
      player2 = matchTable[player1];
    }else if (player2 === "X"){
      player2 = defeatTable[player1]
    }else{
      player2 = reverseDefeatTable[player1]
    }
    
    sum+= pointTable[player2];

    if(draw(player1, player2)){
      sum += DRAW_VALUE;      
    }else if(win(player1, player2)){
      sum += WIN_VALUE;
    }

  });

  console.log(sum);
}

const win = (player1, player2) => (player1 === "A" && player2 === "Y") 
|| (player1 === "B" && player2 === "Z")
|| (player1 === "C" && player2 === "X");

const draw = (player1, player2) => matchTable[player1] === player2

calculateJokempo(results);