为什么在循环外声明一个变量给出不同的输出,在循环内声明一个变量给出不同的输出

why declaring a varialble ouside the loop giving different output and declaring inside the loop giving different output?

本文关键字:变量 输出 一个 循环 声明 为什么      更新时间:2023-09-26

我已经声明了一个变量highScore = 0在for循环上面的函数内,当我尝试在for循环内的相同函数内声明相同的变量(highScore = 0)时,我做了。在这两种情况下,输出是不同的。下面是我的代码。

/*
i = [0, 1, 2, 3, 4, 5,
     6, 7, 8, 9, 10, 11,
     12, 13, 14, 15, 16, 17,
     19, 20, 21, 22, 23, 24,
     25, 26, 27, 28, 29, 30,
     31, 32, 33, 34, 35]
   */
   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];
  function printandgetHighScore(scores) {
var highScore = 0;
var output;
for (var i = 0; i < scores.length; i++) {
 output =  "Bubble solution # " + i + "scores: " + scores[i];
console.log(output);
if(scores[i] > highScore) {
    highScore = scores[i];
    }
}
return highScore;
}            
  var highScore = printandgetHighScore(scores);
  console.log("Bubbles tests: " + scores.length);
   console.log("Highest bubble score: " + highScore);
var bestSolutions = [];
for (var i = 0; i < scores.length; i++) {
if (scores[i]== highScore) {
    bestSolutions.push(i);
}
}
console.log("Solutions with the highest score: " + bestSolutions);

 Output when the **variable highScore = 0; inside function and above for loop**

 Bubble solution # 0scores: 60 bubble.js:29
 Bubble solution # 1scores: 50 bubble.js:29
 Bubble solution # 2scores: 60 bubble.js:29
 Bubble solution # 3scores: 58 bubble.js:29
 Bubble solution # 4scores: 54 bubble.js:29
 Bubble solution # 5scores: 54 bubble.js:29
 Bubble solution # 6scores: 58 bubble.js:29
 Bubble solution # 7scores: 50 bubble.js:29
 Bubble solution # 8scores: 52 bubble.js:29
 Bubble solution # 9scores: 54 bubble.js:29
 Bubble solution # 10scores: 48 bubble.js:29
 Bubble solution # 11scores: 69 bubble.js:29
 Bubble solution # 12scores: 34 bubble.js:29
 Bubble solution # 13scores: 55 bubble.js:29
 Bubble solution # 14scores: 51 bubble.js:29
 Bubble solution # 15scores: 52 bubble.js:29
 Bubble solution # 16scores: 44 bubble.js:29
 Bubble solution # 17scores: 51 bubble.js:29
 Bubble solution # 18scores: 69 bubble.js:29
 Bubble solution # 19scores: 64 bubble.js:29
 Bubble solution # 20scores: 66 bubble.js:29
 Bubble solution # 21scores: 55 bubble.js:29
 Bubble solution # 22scores: 52 bubble.js:29
 Bubble solution # 23scores: 61 bubble.js:29
 Bubble solution # 24scores: 46 bubble.js:29
 Bubble solution # 25scores: 31 bubble.js:29
 Bubble solution # 26scores: 57 bubble.js:29
 Bubble solution # 27scores: 52 bubble.js:29
 Bubble solution # 28scores: 44 bubble.js:29
 Bubble solution # 29scores: 18 bubble.js:29
 Bubble solution # 30scores: 41 bubble.js:29
 Bubble solution # 31scores: 53 bubble.js:29
 Bubble solution # 32scores: 55 bubble.js:29
 Bubble solution # 33scores: 61 bubble.js:29
 Bubble solution # 34scores: 51 bubble.js:29
 Bubble solution # 35scores: 44 bubble.js:29
 Bubbles tests: 36 bubble.js:41
 **Highest bubble score: 69 bubble.js:42
 Solutions with the highest score: 11,18 bubble.js:55**
And only difference when i declare the variable **highScore = 0;** inside the function 
**inside for loop** i get output is 
  **Highest bubble score: 44 bubble.js:42
  Solutions with the highest score: 16,28,35** 

当您在循环中使用highScore = 0时,它将在每次循环迭代时重置为0。这意味着下一个分数与它相比(即。下一个数组项总是比它高,并被认为是到目前为止的最高分。如此反复,直到最后一个数组项成为highScore的最后一个值,从而得出错误的结论,即最后一个数组项(44)是最高分。

在循环之外声明highScore是正确的方法,这会导致记录真正的最高分。这是一个实现您的代码的JSFiddle,也是它的错误版本。注意,在printandgetHighScoreIncorrect()中,变量不断地在for循环内部重新声明,导致最后一个数组项44成为"高分"。尝试更改数组的最后一项,您应该会看到与该函数输出的直接关联。

希望这对你有帮助。如果你有任何问题,请告诉我!

全局变量名scoresprintandgetHighScore函数内部传递的变量名不匹配。试试这个:

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

当highScore在循环中声明时,它的值在每次迭代中被重置为零。这就是为什么最后你看到highScore的值是44,这是你数组中的最后一个元素。在循环上面声明它以使它工作。这样,先前分配的值将被保留,而不会重置为0。