添加相同的提示,但不同的值

Addition with the same prompt, but different values

对于一项任务,我必须创建四组平均值,每组使用 3 个输入值。我让用户能够输入值并显示四组,但未显示平均值。有谁知道为什么?我的代码如下所示。

注意 - 我使用一个循环和变量完成了这项任务,但我被告知我应该使用两个,以防万一有人试图提出建议。

    function inputTime () {
        var delayTime = parseInt(prompt("Delay time is:", "0 seconds"));
    }   

    function performanceTest() {        

        for (var j = 0; j < 3; j++) {
          parseInt(inputTime());
        }

        var delayTimeAvg = (inputTime)/3;

      document.write("The average delay time is:" +" " + delayTimeAvg);
      } 

    function fourTests () {

        for (var i = 0; i < 4; i++) {
          performanceTest();
        }
    }
    </script>

首先,您的 inputTime 函数没有 return 任何值;其次,inputTime 不是数字。 这是正确的代码

        function inputTime() {
        return  parseInt(prompt("Delay time is:", "0 seconds"));
    }

    function performanceTest() {
        var inputValue = 0;
        for (var j = 0; j < 3; j++) {
            inputValue += inputTime();
        }

        var delayTimeAvg = (inputValue) / 3;

        document.write("The average delay time is:" + " " + delayTimeAvg + "<br/>");
    }

    function fourTests() {

        for (var i = 0; i < 4; i++) {
            performanceTest();
        }
    }