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

时间:2019-03-28 19:20:11

标签: javascript

我正在创建一个具有两个分数阵列的游戏。随机分数最高的阵列为获胜者。

一个for循环用于循环遍历两个数组以找到最高的随机分数。将显示一条消息,说明score1score2是赢家。

我不断收到一条错误消息:

  

“未捕获的ReferenceError:未定义highScore”

如何在嵌套for循环中找到最高的随机分数并显示获胜者?

谢谢您的时间。下面是代码:

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"));

2 个答案:

答案 0 :(得分:3)

这里的关键问题是需要重组代码以实现所需的功能。一种方法是从var highScore;外部化变量var winner;function highestScore() { .. },以便在调用函数后可以访问和记录数据。

此外,您不需要使用任何循环来迭代得分即可达到您的要求。相反,您可以只从每个数组中选择随机值,然后比较这些值以确定赢家。

另外请注意,原始代码中有一些小错误,例如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");

希望有帮助!

答案 1 :(得分:1)

在您的代码中

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

可以写为

var result = console.log("Highest score: " + highScore + "." + " " + winner + " is the winner");
highestScore(result);
找不到highScore中的

console.log(...),因为它们仅在函数highestScore中定义,导致出现错误消息。

这样写

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();

此外,您还通过了console.log(...)作为函数的参数,这是不正确的。在函数声明中,您确实定义了没有任何参数的函数。