This question already has an answer here:
这个问题在这里已有答案:
- javascript var declaration within loop 4 answers
- 循环4答案中的javascript var声明
- Javascript function scoping and hoisting 14 answers
- Javascript函数范围和提升14个答案
- Scoping rules for variables initialized in a for loop [duplicate] 1 answer
- 在for循环中初始化的变量的范围规则[重复] 1个答案
Is the var highScore = 0
apart of the loop? Isn't scores[i]
always greater than 0
? I need someone to break down how the if statement is working, and I need to understand how highScore = scores[i]
is giving me back the highest number. This exercise was in a book I'm reading to learn JavaScript, and I just feel it's way over my head. Can anyone shed light? Thank you.
循环的var highScore = 0?得分[i]总是大于0吗?我需要有人来分解if语句是如何工作的,我需要了解highScore =得分[i]是如何让我回到最高数字的。这个练习出现在我正在阅读的一本书中,以学习JavaScript,我觉得这是我的想法。谁能摆脱光明?谢谢。
How is the if statement working in this code? How is highScore even relevant as a variable to be used in the if statement, if it's value is 0? It doesn't seem logical for it to suddenly output the value is the highest number in the array.
if语句如何在此代码中工作?如果它的值为0,那么highScore如何作为在if语句中使用的变量相关?它突然输出值似乎不合逻辑是数组中的最大数字。
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 highScore = 0;
for (i = 0; i < scores.length; i++) {
output = "Bubble #: " + i + " scores: " + scores[i];
console.log(output);
if (scores[i] > highScore){
var highScore = scores[i];
}
}
5 个解决方案
#1
2
The problem lies here:
问题在于:
if (scores[i] > highScore){
**var highScore = scores[i];**
}
You should simply change that to:
你应该简单地改为:
if (scores[i] > highScore){
highScore = scores[i];
}
Everything should work perfectly.
一切都应该完美。
#2
2
var highScore
you are initialising again inside
var highScore你在里面再次初始化
if (scores[i] > highScore){
var highScore = scores[i];
}
Remove var it will add to global highScore
删除var将添加到全局highScore
#3
2
if (scores[i] > highScore){
var highScore = scores[i];
}
If the scores i'th index is more than highScore(which starts are 0), highScore
is then reassigned to that value.
如果指数得分高于highScore(开始时为0),则将highScore重新分配给该值。
So essentially, assuming the first index of the array is higher than 0, which is is since it's 60 - that's the new highscore.
所以基本上,假设数组的第一个索引高于0,这是因为它是60 - 这是新的高分。
Then, at index 1 which is 50, this is ran again:
然后,在索引1为50时,再次运行:
if (scores[i] > highScore){
var highScore = scores[i];
}
Is 50 higher than 60? No, hence, highScore
remains at value 60. And so forth.
50高于60?不,因此,highScore保持在60的价值。依此类推。
Edit:
编辑:
However your code is wrong, you are creating a new variable highScore
in the scope. You need to reassign your initial variable.
但是,您的代码错误,您在范围内创建了一个新的变量highScore。您需要重新分配初始变量。
Hence,
因此,
highScore = scores[i];
highScore = scores [i];
#4
2
I think you are getting confused on the scope of the variable. If you declare your variable with the keyword var
in your program anywhere it will treat as global scope. It means you can access the updated values in anywhere of your program. Thats why it is giving the highest number as a output after the for loop execute. Your code will work fine because of this reason.DEMO HERE. You can see the output 69 as alert. Suppose in your code if you change the code from
我认为你对变量的范围感到困惑。如果您在程序中的任何位置使用关键字var声明变量,它将被视为全局范围。这意味着您可以在程序的任何位置访问更新的值。这就是为什么它在for循环执行后给出最高数字作为输出。由于这个原因,你的代码将正常工作。想知道这里。您可以将输出69视为警报。假设您在代码中更改了代码
if (scores[i] > highScore){
var highScore = scores[i];
}
to
至
if (scores[i] > highScore){
let highScore = scores[i];
}
Now you won't get the biggest number and it will alert the value 0
, because the variable highScore
declared as let
and it will treat as a block level scope not a global scope. DEMO HERE. So when you put alert outside of the for loop it is getting the value from global scope highScore
varibale.
现在你不会获得最大的数字,它会提醒值0,因为变量highScore声明为let,它将被视为块级范围而不是全局范围。在这里演示。因此,当您在for循环之外放置警报时,它将从全局范围highScore varibale获取值。
I hope now you can easily understand how the if condition is working.
我希望你现在可以很容易地理解if条件是如何工作的。
#5
1
Javascript works perfectly fine. You have initialized highScore
twice.
Javascript工作得非常好。您已经两次初始化了highScore。
This is simple scoping of the variables.
这是变量的简单范围。
var highScore = 0;
for (i = 0; i < scores.length; i++) {
output = "Bubble #: " + i + " scores: " + scores[i];
console.log(output);
if (scores[i] > highScore){
var highScore = scores[i]; // ---- (2)
}
}
When you use var
for a declaration of variables it will become a global variable that is the reason you are getting the highest value of the array.
当您使用var作为变量声明时,它将成为一个全局变量,这是您获得数组最高值的原因。
Try using let(in place of two) which has block scoping
尝试使用具有块范围的let(代替两个)
Hope this helps
希望这可以帮助
#1
2
The problem lies here:
问题在于:
if (scores[i] > highScore){
**var highScore = scores[i];**
}
You should simply change that to:
你应该简单地改为:
if (scores[i] > highScore){
highScore = scores[i];
}
Everything should work perfectly.
一切都应该完美。
#2
2
var highScore
you are initialising again inside
var highScore你在里面再次初始化
if (scores[i] > highScore){
var highScore = scores[i];
}
Remove var it will add to global highScore
删除var将添加到全局highScore
#3
2
if (scores[i] > highScore){
var highScore = scores[i];
}
If the scores i'th index is more than highScore(which starts are 0), highScore
is then reassigned to that value.
如果指数得分高于highScore(开始时为0),则将highScore重新分配给该值。
So essentially, assuming the first index of the array is higher than 0, which is is since it's 60 - that's the new highscore.
所以基本上,假设数组的第一个索引高于0,这是因为它是60 - 这是新的高分。
Then, at index 1 which is 50, this is ran again:
然后,在索引1为50时,再次运行:
if (scores[i] > highScore){
var highScore = scores[i];
}
Is 50 higher than 60? No, hence, highScore
remains at value 60. And so forth.
50高于60?不,因此,highScore保持在60的价值。依此类推。
Edit:
编辑:
However your code is wrong, you are creating a new variable highScore
in the scope. You need to reassign your initial variable.
但是,您的代码错误,您在范围内创建了一个新的变量highScore。您需要重新分配初始变量。
Hence,
因此,
highScore = scores[i];
highScore = scores [i];
#4
2
I think you are getting confused on the scope of the variable. If you declare your variable with the keyword var
in your program anywhere it will treat as global scope. It means you can access the updated values in anywhere of your program. Thats why it is giving the highest number as a output after the for loop execute. Your code will work fine because of this reason.DEMO HERE. You can see the output 69 as alert. Suppose in your code if you change the code from
我认为你对变量的范围感到困惑。如果您在程序中的任何位置使用关键字var声明变量,它将被视为全局范围。这意味着您可以在程序的任何位置访问更新的值。这就是为什么它在for循环执行后给出最高数字作为输出。由于这个原因,你的代码将正常工作。想知道这里。您可以将输出69视为警报。假设您在代码中更改了代码
if (scores[i] > highScore){
var highScore = scores[i];
}
to
至
if (scores[i] > highScore){
let highScore = scores[i];
}
Now you won't get the biggest number and it will alert the value 0
, because the variable highScore
declared as let
and it will treat as a block level scope not a global scope. DEMO HERE. So when you put alert outside of the for loop it is getting the value from global scope highScore
varibale.
现在你不会获得最大的数字,它会提醒值0,因为变量highScore声明为let,它将被视为块级范围而不是全局范围。在这里演示。因此,当您在for循环之外放置警报时,它将从全局范围highScore varibale获取值。
I hope now you can easily understand how the if condition is working.
我希望你现在可以很容易地理解if条件是如何工作的。
#5
1
Javascript works perfectly fine. You have initialized highScore
twice.
Javascript工作得非常好。您已经两次初始化了highScore。
This is simple scoping of the variables.
这是变量的简单范围。
var highScore = 0;
for (i = 0; i < scores.length; i++) {
output = "Bubble #: " + i + " scores: " + scores[i];
console.log(output);
if (scores[i] > highScore){
var highScore = scores[i]; // ---- (2)
}
}
When you use var
for a declaration of variables it will become a global variable that is the reason you are getting the highest value of the array.
当您使用var作为变量声明时,它将成为一个全局变量,这是您获得数组最高值的原因。
Try using let(in place of two) which has block scoping
尝试使用具有块范围的let(代替两个)
Hope this helps
希望这可以帮助