【问题标题】:Trying to use for...in loop and adding an if/else statement and keeps saying i am missing a ")"尝试使用 for...in 循环并添加 if/else 语句并一直说我缺少“)”
【发布时间】:2021-08-05 21:42:38
【问题描述】:
const testScores = {
  John : 99,
  Jess : 78,
  Ryan : 89,
  Tom : 62,
  Jane: 57,
  Ben: 83
};

for (person in testScores){
  
       if (testScores[person] > 90){
       console.log("Well done "+ person ". You scored " + testScores[person])
  };

       else{
       console.log(person + " scored " + testScores[person])};
}

【问题讨论】:

  • 您在第一个 console.log 中有一个错误的字符串连接,并且您在 else 声明之前有一个分号。纠正这些,你会没事的。

标签: javascript object if-statement for-in-loop


【解决方案1】:
  • 首先是错字,您忘记在person". You scored " 之间添加+
  • 第二次你在 if 块之后使用;,正因为如此,它的程序没有编译。

const testScores = {
  John: 99,
  Jess: 78,
  Ryan: 89,
  Tom: 62,
  Jane: 57,
  Ben: 83
};

for (person in testScores) {
  if (testScores[person] > 90) {
    console.log("Well done " + person + ". You scored " + testScores[person]);
  } else {
    console.log(person + " scored " + testScores[person]);
  }
}

【讨论】:

  • 太棒了!非常感谢!感谢帮助
  • 如果这是您想要的答案,请点赞并接受答案。
猜你喜欢
  • 1970-01-01
  • 2018-03-27
  • 2019-10-19
  • 2017-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多