Hudson Mckie Hudson Mckie - 1 month ago 6
Javascript Question

Addition with the same prompt, but different values

For a task I have to create four sets of averages, each set using 3 input values. I have the user able to put values and four sets being displayed, but the averages aren't being displayed. Does anyone know why? My code is displayed below.

NOTE - I have this task completed using one loop and variables, but I'm told I should be using two, just in-case anyone tries to suggest that.



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>

Answer

first, your inputTime function does not return any value; second, inputTime is not a number. this is the correct code

        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();
        }
    }
Comments