Javascript 为什么我能';t声明`var highScore=0;`内循环
当我试图声明Javascript 为什么我能';t声明`var highScore=0;`内循环,javascript,variables,Javascript,Variables,当我试图声明var highScore=0时,您好内部for loop在chrome控制台上显示Uncaught SyntaxError:意外标记“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,
var highScore=0时,您好代码>内部for loop
在chrome控制台上显示Uncaught SyntaxError:意外标记“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();
var得分=[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=分数。长度;
函数bubbleScore(){
对于(var i=0;var highScore=0;i高分){
高分=分数[i];
}
}
日志(“气泡测试:+分数.长度”);
返回控制台.log(“最高气泡分数:+highScore”);
}
泡泡芯();
正如@Nick早些时候所说的for
循环得到了三个类似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];
}
}
var高分=0;
对于(变量i=0;i高分){
高分=分数[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];
}
}
for(var i=0,highScore=0;i高分){
高分=分数[i];
}
}
以下是完整版本:
var得分=[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=分数。长度;
函数bubbleScore(){
对于(变量i=0,高分=0;i高分){
高分=分数[i];
}
}
日志(“气泡测试:+分数.长度”);
返回控制台.log(“最高气泡分数:+highScore”);
}
泡泡芯()代码>您可以试试这个
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();
var得分=[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=分数。长度;
var高分=0;
函数bubbleScore(){
对于(var i=0;i高分){
高分=分数[i];
}
}
日志(“气泡测试:+分数.长度”);
返回控制台.log(“最高气泡分数:+highScore”);
}
泡泡芯();
您必须使用符号代码>声明
(初始条件;
继续条件;
迭代后的操作)
在这种情况下,它会像
for (var i = 0, highScore = 0; i < scores.length; i++) {
要找到最大元素,请尝试Math.max(…分数)代码>或旧浏览器Math.max.apply(空,分数)代码>
var i=0;var highScore=0
应该是var i=0,highScore=0
为什么?我自己在学习,所以我不知道太多,请你解释一下,我们是否必须在for循环中只声明一次var关键字?afor
表达式只有3部分,初始化;终止条件;增量。你试图有4个部分(2个初始化),这是无效的。你需要结合两个变量分配,以适应它的意思。好的,我得到了正确的答案,谢谢!请记住,如果其中一个答案对您有效,请将其标记为答案,以帮助社区中的其他窥视者在面临相同问题时更容易找到解决方案。您可以在答案旁边使用灰色标记(勾号)(您只能选择一个),有关更多信息,请阅读。
for (let score of 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();