【问题标题】:Continue statement not working properly in JavaScript for loopContinue 语句在 JavaScript for 循环中无法正常工作
【发布时间】:2020-10-29 00:53:23
【问题描述】:

我正在尝试使用 continue 来省略 test3 被使用的总平均值。

我不确定我在这里做错了什么。目标是使用除test3 文本框中输入的数字之外的所有数组。

我正在尝试收集所有数组的总和(不包括 test3),以便在我的代码末尾得出一个平均值。

<html>
    <head>
        <title>Chapter 3 Activity</title>
    </head>

    <body>
        <h1>Test Score Average</h1>
        <p>Insert your 5 test scores in the boxes below</p>

        <form action="">
            Test 1:
            <input type="text" name="test1" id=t1>
            <br> Test 2:
            <input type="text" name="test2" id=t2>
            <br> Test 3:
            <input type="text" name="test3" id=t3>
            <br> Test 4:
            <input type="text" name="test4" id=t4>
            <br> Test 5:
            <input type="text" name="test5" id=t5>
            <br>
            <input type="button" value="Calculate Average" onclick="calculateFunction()">
        </form>

        <script type="text/javascript">
            function calculateFunction() {
                var test1 = document.getElementById("t1").value;
                var test2 = document.getElementById("t2").value;
                var test3 = document.getElementById("t3").value;
                var test4 = document.getElementById("t4").value;
                var test5 = document.getElementById("t5").value;
                var nums = [test1, test2, test3, test4, test5];

                var totalSum = 0;

                for (var i = 0; i < nums.length; i++) {
                    totalSum += parseInt(nums[i]);

                    if (nums[i] === test3) {
                        continue;
                    }
                }
                var average = totalSum / nums.length;

                document.write("Test score average is " + average);
            }
        </script>
    </body>
</html>

【问题讨论】:

  • 为什么不直接使用var nums = [test1, test2, test4, test5];
  • 你需要把条件放在平均之前。
  • 你把测试放在加值之后,测试应该在前面
  • 我怀疑这是因为 test3 包含一个字符串,而不是一个数字。在这种情况下,if (nums[i] == test3)if (nums[i] === parseInt(test3, 10)) 都可以解决问题。
  • 您的方法的问题是,如果任何测试的分数与 test3 相同,它们都会被跳过。

标签: javascript arrays loops for-loop continue


【解决方案1】:

快速解决方法是将if 语句移到上面,将值添加到totalSum,如下所示:

  for (var i = 0; i < nums.length; i++) {
    if (nums[i] === test3){
      continue;
    }
    totalSum += parseInt(nums[i]);
  }

但是,这确实不是一个好的解决方案。例如,如果数组中的所有数字都等于 5,该怎么办?您的平均值为 0,因为我们每次都会点击 continue。如果您的目标是在计算平均值时始终省略数组中的第三个元素,那么这样的方法会更好:

  for (var i = 0; i < nums.length; i++) {
    if (i === 2) {
      continue;
    }
    totalSum += parseInt(nums[i]);
  }

请记住除以 nums.length - 1,因为在计算平均值时会省略其中一个元素。

【讨论】:

  • 我知道我现在做错了什么,你对我的帮助很大,我很感激!
【解决方案2】:

document.getElementById("go").addEventListener("click", evt => {
  // get all the inputs you want to use
  let ts = document.querySelectorAll("#t1,#t2,#t4,#t5");

  let total = 0;
  // loop through the inputs and total them
  for (let t of ts) {

    // convert the value to an integer
    let value = parseInt(t.value);

    // check to ensure it is a number and matches with the person put in
    if (!isNaN(value) && value == t.value) {
      total += parseInt(t.value);
    } else {
      t.value = 'Ignored';
    }
  }
  let average = total / ts.length;

  document.getElementById("output").textContent = "Test score average is " + average;

});
<h1>Test Score Average</h1>
<p>Insert your 5 test scores in the boxes below</p>


Test 1: <input type="text" name="test1" id=t1><br> Test 2: <input type="text" name="test2" id=t2><br> Test 3: <input type="text" name="test3" id=t3><br> Test 4: <input type="text" name="test4" id=t4><br> Test 5: <input type="text" name="test5" id=t5><br>

<!-- Use a button rather than an input -->
<button id="go" type="button">Calculate Average</button>

<output id="output"></output>

【讨论】:

  • 感谢您在这里帮助我所做的工作!这是我使用 continue 语句的要求,我应该指定但通过你的代码,我看到有很多我不知道的东西哈哈。谢谢!
  • 很抱歉我的回答没有帮助到你。在本练习中更好地使用 continue 将遍历所有元素并忽略(继续)那些缺少或无效数据的元素。
猜你喜欢
  • 2020-09-20
  • 2016-01-14
  • 2021-09-26
  • 1970-01-01
  • 1970-01-01
  • 2017-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多