-1

我正在学习 JS,我正在制作一个应用程序来查找高尔夫比赛中每个洞的获胜者。杆数较少的球员是该洞的获胜者,但有时不止一名球员可以以相同的杆数将洞打平,因此,要找到该洞的获胜者,我需要评估并列球员的杆数下一个洞并打破平局,如果平局持续,我需要评估下一个洞等等。

函数 foo 应该返回一个数组,其中包含该洞的获胜者的索引、位置或名称(如果无法打破平局)。

这是我的代码:

var players = [
    "Jug 1",
    "Jug 2",
    "Jug 3",
    "Jug 4"
];

var arrHoyos = [
    [4, 3, 3, 4],
    [4, 5, 4, 5],
    [4, 4, 5, 6]
];

function foo(array) {
    var arrWinners = [];
    //arrHoyos
    for (var i = 0, len = array.length; i < len; i++) {
        var arr = array[i];
        var arr2 = array[(i + 1) % array.length];
        var pos = 0;
        var min = 0;
        var tie = false;
        var temp = [];
        var temp2 = [];
        min = Math.min.apply(null, arr);
        //arr
        for (var j = 0, len2 = arr.length; j < len2; j++) {
            if (min == arr[j]) {
                temp.push(arr.indexOf(min, j));
            }   
        }
        if (temp.length > 1) {
            tie = true;
            var counter = 0;
            while (tie == true) {
                for (var count = 0; count < temp.length; count++) {
                    pos = temp[count];
                    temp2[count] = arr2[pos];
                }
                min = Math.min.apply(null, temp2);
                for (var count2 = 0; count2 < temp.length; count2++) {
                    if (temp2[count2] != min) {
                        temp.slice(count2, 1);
                    }
                }
                if (temp.length == 1 || counter == array.length) {
                    tie = false;
                }
                counter++;
            }
        }
        arrWinners[i] = players[temp[0]];
        console.log(temp, temp2);
        console.log(arrWinners[i]);
    }
}

foo(arrHoyos);

Chrome 控制台结果:

[1, 2] [5, 4]
Jug 2
[0, 2] [4, 5]
Jug 1
[0, 1] [4, 3]
Jug 1
4

2 回答 2

1

我得到了答案,谢谢丹尼尔,你的帮助很有用。:)

var players = [
    "Jug 1",
    "Jug 2",
    "Jug 3",
    "Jug 4"
];

var arrFieldStrokes = [
    [3, 3, 3, 3],
    [5, 5, 5, 5],
    [5, 6, 5, 5],
    [5, 7, 6, 5],
    [4, 5, 5, 4],
    [4, 5, 6, 8],
    [4, 6, 6, 5],
    [4, 5, 9, 5],
    [4, 4, 4, 4]
];

function breakTie(hole, arrTiedPlayers, arrField) {
    for (var i = 0; i < arrField.length; i++) {
        var min = 0, count = 0; arrNextScore = [];
        for (var j = 0; j < arrTiedPlayers.length; j++) {
            arrNextScore[j] = arrField[(hole + i + 1) % arrField.length][arrTiedPlayers[j]];
        }
        min = Math.min.apply(null, arrNextScore);
        count = arrNextScore.length;
        while (count--) {
            if (arrNextScore[count] != min) {
                arrTiedPlayers.splice(count, 1);
            }
        }
        if (arrTiedPlayers.length == 1) {
            return players[arrTiedPlayers[0]];
        }
    }
    var tiePlayers = [];
    for (var c = 0; c < arrTiedPlayers.length; c++) {
        tiePlayers[c] = players[arrTiedPlayers[c]];
    }
    return tiePlayers;
}

function winners(arrField) {
    var arrWinners = [];
    for (var i = 0; i < arrField.length; i++) {
        var min = 0, arrTiedPlayers = [];
        min = Math.min.apply(null, arrField[i]);
        for (var j = 0; j < arrField[i].length; j++) {
            if (arrField[i][j] == min) {
                arrTiedPlayers.push(arrField[i].indexOf(min, j));
            }
        }
        if (arrTiedPlayers.length == 1) {
            arrWinners[i] = players[arrTiedPlayers[0]];
            console.log("Hoyo-" + (i + 1) + " Winner: " + arrWinners[i]);
        }
        else {
            arrWinners[i] = breakTie(i, arrTiedPlayers, arrField);
            console.log("Hoyo-" + (i + 1) + " Winner: " + arrWinners[i]);
        }
    }
}

winners(arrFieldStrokes);
于 2015-08-21T01:17:32.757 回答
1

这里的主要问题是您使用slice的是splice. 甚至可能是一个错字。temp2在 while 循环内使用时,您可能不会增加整个索引。我有一种感觉,它永远是下一个洞。

旁注/建议:

  1. 学习使用调试器。
  2. 为变量使用有意义的名称。temp, temp2,arr和其他都不好。

编辑:包括正确splice使用的实际代码

var players = [
    "Jug 1",
    "Jug 2",
    "Jug 3",
    "Jug 4"
];

var arrHoyos = [
    [4, 3, 3, 4],
    [4, 5, 4, 5],
    [4, 4, 5, 6]
];

function foo(array) {
    var arrWinners = [];
    //arrHoyos
    for (var i = 0, len = array.length; i < len; i++) {
        var arr = array[i];
        var arr2 = array[(i + 1) % array.length];
        var pos = 0;
        var min = 0;
        var tie = false;
        var temp = [];
        var temp2 = [];
        min = Math.min.apply(null, arr);
        //arr
        for (var j = 0, len2 = arr.length; j < len2; j++) {
            if (min == arr[j]) {
                temp.push(arr.indexOf(min, j));
            }   
        }
        if (temp.length > 1) {
            tie = true;
            var counter = 0;
            while (tie == true) {
                for (var count = 0; count < temp.length; count++) {
                    pos = temp[count];
                    temp2[count] = arr2[pos];
                }

                min = Math.min.apply(null, temp2);
                for (var count2 = 0; count2 < temp.length; count2++) {
                    if (temp2[count2] != min) {
                        temp.splice(count2, 1);
                    }
                }
                if (temp.length == 1 || counter == array.length) {
                    tie = false;
                }
                counter++;
            }
        }
        arrWinners[i] = players[temp[0]];
        console.log(temp, temp2);
        console.log(arrWinners[i]);
    }
}

foo(arrHoyos);
于 2015-08-19T20:37:51.640 回答