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

时间:2014-07-18 16:53:59

标签: javascript function

我在for循环之上的函数中声明了变量 highScore = 0 ,当我尝试在内部声明相同的变量(highScore = 0)时我做了相同的功能 for loop 。在这两种情况下,输出都是不同的。以下是我的代码。

/*
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** 

3 个答案:

答案 0 :(得分:0)

全局变量名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;
}

答案 1 :(得分:0)

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

答案 2 :(得分:0)

当你在循环中使用highScore = 0时,它会在循环的每次迭代中重置为0。这意味着与其进行比较的下一个得分(即下一个数组项)将始终高于它,并且将被视为迄今为止的最高得分。这会一直重复,直到最后一个数组项,它成为highScore的最后一个值 - 导致最终数组项(44)得分最高的错误结论。

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

希望这会有所帮助。如果您有任何问题,请告诉我们!