在JavaScript变量中寻找最高价值 [英] Finding highest value(s) amongst JavaScript variables
问题描述
我正在开发一个基于JS的游戏,在其中跟踪多个用户的分数.在游戏结束时,我需要宣布一个获胜者.可能有联系.
I'm working on a JS-based game where I'm tracking the scores of several users. At the end of the game, I need to declare a winner. There may be ties.
因此,有两个例子:
player1 = 3
player2 = 5
player3 = 4
(玩家2是赢家)
player1 = 4
player2 = 2
player3 = 4
(玩家1和3并列)
只有3个玩家,我可能只用一堆嵌套的if/els互相比较每个值来做到这一点,但是我想知道是否有一种更优雅的方法来实现此目的...尤其是在以下情况下举例来说,您可能有10位玩家,如果比较/不比较将很快变得笨拙.
With only 3 players, I could probably just do this with a bunch of nested if/elses comparing each value to each other, but was wondering if there is a more elegant way to go about this...especially in situations where you may have 10 players as an example, where if/else comparisons would get unwieldy pretty fast.
一种选择似乎是将值粘贴在数组中,然后对其进行排序,但这并不能给您变量名-只是值.然后,我可以做一个多维数组,但是那时事情似乎又变得混乱了.
One option seems to be to stick the values in an array, then sort it, but that doesn't give you the variable name--just the values. I could then do a multi-dimensional array, but then things seem to be getting messy again at that point.
推荐答案
没有简单的方法来遍历变量名称,例如 player1
, player2
等.而不使用 eval()
或不要求这些变量是全局变量,这通常是一种不好的方法.
There is no easy way to iterate through variable names like player1
, player2
, etc... without using eval()
or requiring those variables to be global variables which is generally a bad way to go.
如果它们是全局变量,则可以使用此技巧,但是我建议在下面的第二种方法中将玩家信息存储为对象数组.这是使用全局变量的黑客(不建议使用,但是可以直接回答您的问题):
If they were global variables, you could use this hack, but I would suggest my second method below which stores player info as an array of objects. Here's a hack using global variables (not recommended, but a more direct answer to your question):
// these must be global variables
var score1 = 100;
var score2= 99;
var score3= 80;
function findHighScore() {
var highestScore = score1;
var highestScorePlayerNum = 1;
var tempScore;
for var (i = 2; i <= 3; i++) {
// this works only because the score variables are global variables which
// can be accessed as properties off the window object
tempScore = window["score" + i];
if (tempScore > highestScore) {
highestScore = tempScore;
highestScorePlayerNum = i;
}
}
return {score: highestScore, playerNum: highestScorePlayerNum};
}
更好的方法是不将每个玩家的得分保持在自己的javascript变量中,而是将其放入这样的对象数组中:
A much better way would involve not keeping each player's score in its own javascript variable, but rather putting it into an array of objects like this:
var playerData = [
{name: "John", highScore: 145, lastPlayed: 23089234},
[name: "Bill", highScore: 99, lastPlayed: 59383945}
];
然后,您可以遍历玩家数据,找到最高分并获得该玩家的名字.
Then, you can loop through the player data, find the highest score and have the name of that player too.
function findHighScore() {
var highScoreSoFar = 0;
var result;
for (var i = 0; i < playerData.length; i++) {
if (playerData[i].highScore > highScoreSoFar) {
result = playerDatra[i];
highScoreSoFar = playerData[i].highScore;
}
}
return result;
}
var highPlayer = findHighScore();
alert("Highest scoring player is " + highPlayer.name + " with a score of " + highPlayer.highScore);
如果您想返回所有得分最高的玩家,则可以返回得分最高的玩家:
If you want to return all players tied for the highest score, you can return an array of players that had the highest score:
function findHighScore() {
var highScoreSoFar = 0;
var result = [];
for (var i = 0; i < playerData.length; i++) {
if (playerData[i].highScore > highScoreSoFar) {
// new high score so start a new array with this player in it
result = [playerData[i]];
highScoreSoFar = playerData[i].highScore;
} else if (playerData[i].highScore === highScoreSoFar) {
// more than one player with this highScore so add this one to the array
result.push(playerData[i]);
}
}
return result;
}
这篇关于在JavaScript变量中寻找最高价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!