javascript - 需要编码作业方面的帮助!如何停止我的 if else 语句,使其只运行一次

标签 javascript arrays if-statement

我需要有关如何使代码的 if else 语句仅运行一次的帮助,我想知道我是否遵循了此作业所要求的所有说明。另外,我可能有一些错误或放置问题。我是一个 JavaScript 初学者,因为我之前从未使用过该程序。说明如下:

  • 运行 if/else 语句来查看年龄是否小于 60 岁,然后根据结果显示警报
  • 将第三个问题插入问题数组
  • 将对应的答案数组插入answers数组
  • 在分数检查中添加 if 和 else if 语句,以增加每个答案不同级别的分数:3 分、2 分或 1 分,具体取决于用户的选择。这些应该针对所有三个问题来实现。尝试使用for循环循环遍历所有问题和答案,并考虑使用数组中的索引作为评分系统。 -添加<br>Score:使用 id='myParagraph' 将用户的分数添加到其段落 ( document.getElementById )更改innerHTML
  • 添加额外提示,要求用户计算基本数学问题的结果
  • 如果用户对数学问题的答案等于同一数学问题作为表达式的结果,则将用户的分数增加 5:确保该方程是 if 语句的一部分。

代码如下:

var question = ['What is your quest?', 'What is the airspeed of an unladen swallow?', 'What planet\'s moon can more liquid water than all of Earth\'s oceans?'];
var score = 0;
var answers = [
  ['To seek the grail', 'I don\'t know that', 'To be in Monty Python'],
  ['African or European?', '92', '24'],
  ['Europa', 'Titan', 'Pandora']
];
console.log(answers);
console.log(answers[1][0]);
var name = prompt('What is your name?');
//alert('A message;);
document.getElementById('myParagraph').innerHTML = 'Hello ' + name + '!';
document.getElementById('myScore').innerHTML = 'You scored ' + score + 'points!';
console.log(typeof name);
var age = prompt('How old are you?');
console.log(typeof age);
age = Number(age);
console.log(typeof age);
if (age < 60) {
  alert(age);
}
for (var i = 0; i <= age; i++) {
  document.write(i);
  document.write(' ');
}

var math = prompt('What is 16+2-1*3=?');
console.log(typeof math);
math = Number(math);
if (math === 15) {
  alert(math);
} else {
  alert('You are incorrect');
}
for (var j = 0; j <= math; i + 5) {
  document.write(j);
  document.write(' ');
}
var guess = prompt(question[0] + '\n\u2022 ' + answers[0][0] + '\n\u2022 ' + answers[0][1] + '\n\u2022 ' + answers[0][2]);
if (guess === answers[0][0]) {
  score += 3;
  console.log(score + ' ' + 'points earned!');

} else if (guess === answers[0][1]) {
  score += 2;
  console.log(score + ' ' + 'points earned!');

} else if (guess === answers[0][2]) {
  score += 1;
  console.log(score + ' ' + 'points earned!');
}

var guess = prompt(question[1] + '\n\u2022 ' + answers[1][0] + '\n\u2022 ' + answers[1][1] + '\n\u2022 ' + answers[1][2]);
if (guess === answers[1][0]) {
  score += 1;
  console.log(score + ' ' + 'points earned!');

} else if (guess === answers[1][1]) {
  score += 2;
  console.log(score + ' ' + 'points earned!');

} else if (guess === answers[1][2]) {
  score += 3;
  console.log(score + ' ' + 'points earned!');
}

var guess = prompt(question[2] + '\n\u2022 ' + answers[2][0] + '\n\u2022 ' + answers[2][1] + '\n\u2022 ' + answers[2][2]);
if (guess === answers[2][0]) {
  score += 3;
  console.log(score + ' ' + 'points earned!');

} else if (guess === answers[2][1]) {
  score += 2;
  console.log(score + ' ' + 'points earned!');

} else if (guess === answers[2][2]) {
  score += 1;
  console.log(score + ' ' + 'points earned!');
}
<br id="myScore">
<p id="myParagraph">A paragraph!</p>
</br>

最佳答案

这里有一个无限循环:

for (var j = 0; j <= math; i + 5) {
  document.write(j);
  document.write(' ');
}

因为你从未修改j , j <= math始终为真,因此循环永远不会结束。

我一开始就没有看到这个循环的任何原因。问题规范规定,如果用户答对了数学问题,则将其分数加 5。所以你应该这样做:

if (math === 15) {
  alert(math);
  score += 5; // Add 5 to the user's score
} else {
  alert('You are incorrect');
}

关于javascript - 需要编码作业方面的帮助!如何停止我的 if else 语句,使其只运行一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39561733/

相关文章:

javascript - 使用 A、B、C、D 而不是 0、1、2、3 ...... 使用 JavaScript 进行计数

javascript - HTML5/JavaScript : open text file, 加载到文本区域/将文本区域内容保存到文本文件

javascript - 无法将 websocket 连接到 service-worker

java - Java中的字符串数组排序

c++ - 使用排序函数对并行数组进行排序

Java boolean if问题

javascript - 难以在 IE 中使用 Javascript 捕获日语字符输入

java - 谁能给我更好的解决方案,将两个排序数组合并到另一个排序数组中

c - 循环错误,程序停止并显示 "INPUT NOT VALID!!"但再次要求输入?

javascript - JavaScript 中的 if else/for 循环