繁体   English   中英

为什么我不能在 for 循环中声明 ```var highScore = 0;```

[英]Why i can't declare ```var highScore = 0;``` inside for loop

嗨,当我尝试声明var highScore = 0; 在 Chrome 控制台for loop中,它说Uncaught SyntaxError: Unexpected token 'var'所以我可以为此做些什么是我的代码:

var scores = [60, 50, 60, 58, 54, 54,
              58, 50, 52, 54, 48, 69,
              34, 55, 51, 52, 44, 51,
              69, 64, 66, 55, 52, 61,
              46, 31, 57, 52, 44, 18,
              41, 53, 55, 61, 51, 44];

var totalTest = scores.length;

function bubbleScore() {
  for (var i = 0; var highScore = 0; i < scores.length; i++) {
  console.log("Bubble solution #" + i + " score: " + scores[i]);
  if (scores[i] > highScore) {
    highScore = scores[i];
  }
  }
  console.log("Bubbles tests: " + scores.length);
  return console.log("Highest bubble score: " + highScore);
}
bubbleScore();

正如@Nick 之前所说的for循环有三部分,如for (initialization; condition; post-expression)所以你在 for 循环中尝试的东西(声明两个变量) for非法的 由于您想在for循环中迭代(而不是在循环的第一个定义它)您的highScore变量,因此最好这样做:

var highScore = 0;

for (var i = 0; i < scores.length; i++) {
  console.log("Bubble solution #" + i + " score: " + scores[i]);
  if (scores[i] > highScore) {
    highScore = scores[i];
  }
}

但如果你坚持这样的事情,你可以这样做:

for (var i = 0, highScore = 0; i < scores.length; i++) {
  console.log("Bubble solution #" + i + " score: " + scores[i]);
  if (scores[i] > highScore) {
    highScore = scores[i];
  }
}

这是完整版:

 var scores = [60, 50, 60, 58, 54, 54, 58, 50, 52, 54, 48, 69, 34, 55, 51, 52, 44, 51, 69, 64, 66, 55, 52, 61, 46, 31, 57, 52, 44, 18, 41, 53, 55, 61, 51, 44 ]; var totalTest = scores.length; function bubbleScore() { for (var i = 0, highScore = 0; i < scores.length; i++) { console.log("Bubble solution #" + i + " score: " + scores[i]); if (scores[i] > highScore) { highScore = scores[i]; } } console.log("Bubbles tests: " + scores.length); return console.log("Highest bubble score: " + highScore); } bubbleScore();

你必须使用符号; 声明
初始条件;继续条件;迭代后的动作


在这种情况下,它会像

 for (var i = 0, highScore = 0; i < scores.length; i++) {

或者

for (let score of scores) {

PS 查找最大元素尝试Math.max(...scores); 或旧浏览器Math.max.apply(null, scores);

const scores = [60, 50, 60, 58, 54, 54,
              58, 50, 52, 54, 48, 69,
              34, 55, 51, 52, 44, 51,
              69, 64, 66, 55, 52, 61,
              46, 31, 57, 52, 44, 18,
              41, 53, 55, 61, 51, 44];

function bubbleScore() {
  console.log("Bubbles tests: " + scores.length);
  return console.log("Highest bubble score: " + Math.max(...scores));
}
bubbleScore();

你可以试试这个

var scores = [60, 50, 60, 58, 54, 54,
              58, 50, 52, 54, 48, 69,
              34, 55, 51, 52, 44, 51,
              69, 64, 66, 55, 52, 61,
              46, 31, 57, 52, 44, 18,
              41, 53, 55, 61, 51, 44];

var totalTest = scores.length;
var highScore = 0;
function bubbleScore() {
  for (var i = 0; i < totalTest; i++) {
  console.log("Bubble solution #" + i + " score: " + scores[i]);
  if (scores[i] > highScore) {
    highScore = scores[i];
  }
  }
  console.log("Bubbles tests: " + scores.length);
  return console.log("Highest bubble score: " + highScore);
}
bubbleScore();

暂无
暂无

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

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