-3
function showAndCalculateDistance(speed, time) {

for (var i=0; i<5; i++){

var mph = vehicleSpeed* timeTraveled;

return mph

}

var vehicleSpeed = parseInt(prompt('How fast is the vehicle traveling (in miles per hour)? '));
var timeTraveled = parseFloat(prompt('How long has this vehicle traveled (in hours)? '));
var totalDistanceTraveled = showAndCalculateDistance(vehicleSpeed,timeTraveled);
alert('In ' + timeTraveled + ' hours, a vehicle traveling at ' + vehicleSpeed + ' miles/hour will have traveled ' + totalDistanceTraveled + ' miles.');

I want to write the program so it calls the function using a loop to display the number of miles the vehicle traveled for each hour of the time period, and returning the total number of miles traveled

3 Answers3

0

You are missing a closing parenthesis to terminate your for loop. In the future, you should indent your code to make catching errors like this easier. Although I'm not sure what exactly the purpose of the for loop is in your code, since you are just assigning the same value to mph 5 times.

function showAndCalculateDistance(speed, time) {

    for (var i=0; i<5; i++){

        var mph = vehicleSpeed* timeTraveled;

    }

    return mph

} // <-- you did not put this.

var vehicleSpeed = parseInt(prompt('How fast is the vehicle traveling (in miles per hour)? '));
var timeTraveled = parseFloat(prompt('How long has this vehicle traveled (in hours)? '));
var totalDistanceTraveled = showAndCalculateDistance(vehicleSpeed,timeTraveled);
alert('In ' + timeTraveled + ' hours, a vehicle traveling at ' + vehicleSpeed + ' miles/hour will have traveled ' + totalDistanceTraveled + ' miles.');
chiragzq
  • 388
  • 1
  • 14
0

You're missing an ending bracket. Either it was for the closing of the for loop or the function.

0

Be sure to check your brackets. you can tell that this is the problem when you see an error such as: Uncaught SyntaxError: Unexpected end of input Also, make sure the name of the parameters in the function match what you typed inside of the function. This doesn't matter at the moment because you declared vehicleSpeed and timeTraveled as global variables, but if you plan to expand this, make sure to fix it so it doesn't cause any unexpected problems.

function showAndCalculateDistance(speed, time) {

    for (var i=0; i<5; i++){

        //you typed: var mph = vehicleSpeed* timeTraveled
        var mph = speed*time;

    }

    return mph

} // <-- close bracket here

var vehicleSpeed = parseInt(prompt('How fast is the vehicle traveling (in miles per hour)? '));
var timeTraveled = parseFloat(prompt('How long has this vehicle traveled (in hours)? '));
var totalDistanceTraveled = showAndCalculateDistance(vehicleSpeed,timeTraveled);
alert('In ' + timeTraveled + ' hours, a vehicle traveling at ' + vehicleSpeed + ' miles/hour will have traveled ' + totalDistanceTraveled + ' miles.');
wilson wilson
  • 113
  • 1
  • 6