简体   繁体   English

JavaScript:如何从两个数组中找到最高的随机值得分

[英]JavaScript: How to find the highest random value score from two arrays

I am creating a game that has two arrays of scores. 我正在创建一个具有两个分数阵列的游戏。 The array with the highest random score is the winner. 随机分数最高的阵列为获胜者。

A for loop is being used to loop through both arrays to find the highest random score. 使用for循环遍历两个数组以找到最高的随机分数。 A message will display saying if score1 or score2 is the winner. 将显示一条消息,说明score1score2是获胜者。

I keep getting an error message that say: 我不断收到一条错误消息,内容为:

"Uncaught ReferenceError: highScore is not defined" “未捕获的ReferenceError:未定义highScore”

How can I find the highest random score in a nested for loop and display the winner?. 如何在嵌套for循环中找到最高的随机分数并显示获胜者?

Thank you for your time. 感谢您的时间。 Below is the code: 下面是代码:

 function highestScore() { var score1 = [260, 57, 83, 53, 11, 33, 56, 77, 64, 98, 45, 94, 33, 45, 29, 40, 28, 57 ]; var score2 = [160, 157, 183, 153, 198, 111, 133]; var highScore = 0; // hold highest score var winner; var randScore1 = Math.floor(Math.random() * score1.length); var randScore2 = Math.floor(Math.random() * score2.length); var get_randScore1 = score1[randScore1]; var get_randScore2 = score2[randScore2]; score1 = get_randScore1 score2 = get_randScore2 for (var i = 0; i < score1.length; i++) { for (var j = 0; j < score2.length; j++) { if (score[i] > highScore) { highScore = score[i]; winner = "Score1"; } else if (score2[i] > highScore) { highScore = score2[i]; winner = "Score2"; } } } return highScore; } highestScore(console.log("Highest score: " + highScore + "." + " " + winner + " is the winner")); 

A key issue here is the need to restructure your code to achieve what you require. 这里的关键问题是需要重组代码以实现所需的功能。 One approach would be to externalise the variables var highScore; 一种方法是将变量var highScore;外部var highScore; and var winner; var winner; from function highestScore() { .. } so that the data can be accessed and logged after the function has been called. function highestScore() { .. }以便在调用该函数之后可以访问和记录数据。

Additionally, you shouldn't need to iterate the scores using any kind of loop to achieve what you require. 此外,您不需要使用任何循环来迭代得分即可达到您的要求。 You can instead just select random values from each of the arrays and compare those to determine a winner. 您可以从每个数组中选择随机值,然后比较这些值以确定赢家。

Also as a note, there are a few minor errors in your original code such as score1 = get_randScore1 , and use of the undefined array score , however with the revisions made in the snippet below, these can be omitted from the solution: 另外请注意,您的原始代码中有一些小错误,例如score1 = get_randScore1 ,并且使用了未定义的数组score ,但是使用以下代码段中的修订,可以从解决方案中忽略这些错误:

 /* Move these varialbes outside of highestScore() */ var highScore = 0; var winner; function highestScore() { var score1 = [260, 57, 83, 53, 11, 33, 56, 77, 64, 98, 45, 94, 33, 45, 29, 40, 28, 57 ]; var score2 = [160, 157, 183, 153, 198, 111, 133]; var randScore1 = Math.floor(Math.random() * score1.length); var randScore2 = Math.floor(Math.random() * score2.length); var get_randScore1 = score1[randScore1]; var get_randScore2 = score2[randScore2]; /* Remove this: score1 = get_randScore1 score2 = get_randScore2 */ /* This achieves what the description in your question requires */ if (get_randScore1 > get_randScore2) { highScore = get_randScore1; winner = "Score1"; } else if (get_randScore2 > get_randScore1) { highScore = get_randScore2; winner = "Score2"; } else { winner = "No winning score"; } /* This doesn't seem to achieve what the description of your question requires for (var i = 0; i < score1.length; i++) { for (var j = 0; j < score2.length; j++) { if (score1[i] > highScore) { highScore = score1[i]; winner = "Score1"; } else if (score2[j] > highScore) { highScore = score2[j]; winner = "Score2"; } } } */ /* Not needed: return highScore; */ } /* Call function */ highestScore(); /* Log results */ console.log("Highest score: " + highScore + "." + " " + winner + " is the winner"); 

Hope that helps! 希望有帮助!

In your code, 在您的代码中

highestScore(console.log("Highest score: " + highScore + "." + " " + winner + " is the winner"))

can be written as 可以写成

var result = console.log("Highest score: " + highScore + "." + " " + winner + " is the winner");
highestScore(result);

and highScore within the console.log(...) can't be found, because it is only defined within the function highestScore , resulting in your error message. console.log(...)中找不到highScorehighScore ,因为它仅在函数highestScore定义,从而导致您出现错误消息。

Write it like this 这样写

function highestScore() {
  var score1 = [260, 57, 83, 53, 11, 33,
    56, 77, 64, 98, 45, 94,
    33, 45, 29, 40, 28, 57
  ];

  var score2 = [160, 157, 183, 153, 198, 111, 133];

  var highScore = 0; // hold highest score
  var winner;

  var randScore1 = Math.floor(Math.random() * score1.length);
  var randScore2 = Math.floor(Math.random() * score2.length);


  var get_randScore1 = score1[randScore1];
  var get_randScore2 = score2[randScore2];
  score1 = get_randScore1
  score2 = get_randScore2

  for (var i = 0; i < score1.length; i++) {
    for (var j = 0; j < score2.length; j++) {
      if (score[i] > highScore) {
        highScore = score[i];
        winner = "Score1";
      } else if (score2[i] > highScore) {
        highScore = score2[i];
        winner = "Score2";
      }
    }
  }
  console.log("Highest score: " + highScore + "." + " " + winner + " is the winner")
  return highScore;
}

highestScore();

Also, you passed the console.log(...) as parameter of the function, which is not right. 另外,您将console.log(...)作为函数的参数传递,这是不正确的。 In your function declaration you did define the function without any parameters. 在函数声明中,您确实定义了没有任何参数的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM