Почему я не могу объявить var highScore = 0; внутри цикла for - PullRequest
0 голосов
/ 09 мая 2020

Привет, когда я пытаюсь объявить var highScore = 0; внутри for loop в chrome консоли, он говорит 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();

Ответы [ 3 ]

2 голосов
/ 09 мая 2020

Как сказал ранее @Nick for l oop получил трехсекционный, например for (initialization; condition; post-expression), так что вещь, которую вы пытаетесь здесь (объявление двух переменных) в for l oop недопустимо . Поскольку вы хотите повторить (а не определять его в первом из l oop) переменную highScore в вашем for l oop, лучше сделать это следующим образом:

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();
0 голосов
/ 09 мая 2020

Вы должны использовать символ ; для объявления
( условий инициализации ; условия продолжения ; действия после итерации )


В этом случае это будет примерно так:

 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();
0 голосов
/ 09 мая 2020

Вы можете попробовать это

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();
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...