我正在学习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
答案 0 :(得分:1)
这里的主要问题是您使用的是slice
而不是splice
。甚至可能是一个错字。在while循环中使用temp2
时,您可能没有增加整个索引。我感觉它总是下一个洞。
附注/建议:
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);
答案 1 :(得分:1)
我得到了答案,谢谢Danil,你的帮助很有用。 :)
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);