比较对象 属性 键与对象 属性 数组中的值,Return "Total Points"

Compare Object Property Keys with Object Property Values in Array, Return "Total Points"

Link 到 Codewars kata。基本上,据我了解:

我需要遍历每个个体 属性 以将 x 值与 y 值进行比较。但是我正在尝试的方法不起作用:

function points(games) {

  let points = 0;

  for (let key in games) {
    let item = games[key];
    let x = item[0];
    let y = item[1];
    //compare x values against y values in items
    if (x > y) {
      points += 3;
    }
    if (x < y) {
      points += 0;
    }
    if (x === y) {
      points += 1;
    }
  }
  return points;
}

console.log(points(["1:0", "2:0", "3:0", "4:0", "2:1", "3:1", "4:1", "3:2", "4:2", "4:3"]));

这将返回 0。

如何遍历数组中的每个 属性 以在 每个 属性 中进行比较?

EDIT - 在将字符串拆分为数组进行比较后,我仍然不明白如何将数组中的 x 值与 y 值进行比较:

function points(games) {
  let points = 0;

  for (let i = 0; i < games.length; i++) {
    let properties = games[i].split();

    if (properties[0] > properties[1]) {
      points += 3;
    }
    if (properties[0] < properties[1]) {
      points += 0;
    }
    if (properties[0] === properties[1]) {
      points += 1;
    }
  }
  return points;
}

阅读有关 reduce and map 的更多信息

function points(games) {

  return games.map(game => {
    let points = 0;
    let item = game.split(':');
    let x = item[0];
    let y = item[1];
    //compare x values against y values in items
    if (x > y) {
      points += 3;
    }
    if (x < y) {
      points += 0;
    }
    if (x === y) {
      points += 1;
    }
    return points;
  }).reduce((sum, curr) => (sum += curr),0);
}

console.log(points(["1:0", "2:0", "3:0", "4:0", "2:1", "3:1", "4:1", "3:2", "4:2", "4:3"]));