In: Computer Science
<!DOCTYPE html>
<html>
<body>
<script>
//
// A car's miles-per-gallon (MPG) can be calculated with the following formula:
//
// MPG=Milesdriven/Gallonsofgasused
//
// Write a program that asks the user for the number of miles driven and the
// gallons of gas used. It should then call a function to calculate and return
// the car's MPG and display the result.
//
function calculateMPG(miles, gas) {
/////////////////////////////////////////////////////////////////////////////////
// Insert your code between here and the next comment block. Do not alter //
// any code in any other part of this file. //
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
// Insert your code between here and the previous comment block. Do not alter //
// any code in any other part of this file. //
/////////////////////////////////////////////////////////////////////////////////
}
var milesDriven = prompt('How many miles have you driven? ');
var gasUsed = prompt('How much gas have you used driving that far? ')
alert('You car is getting ' + calculateMPG(milesDriven, gasUsed) + ' MPG.');
</script>
</body>
</html>
Below is the solution:
<!DOCTYPE html> <html> <body> <script>
//function to calculate the MPG
function calculateMPG(miles, gas) {
var MPG=miles/gas; //formua to calculate the MPG
(MPG=Milesdriven/Gallonsofgasused)
return MPG; //return
}
var milesDriven = prompt('How many miles have you driven? ');
//ask the user to enter the miles driven
var gasUsed = prompt('How much gas have you used driving that far?
') //ask the user to enter the gas used
alert('You car is getting ' + calculateMPG(milesDriven, gasUsed) + ' MPG.'); //call the function inside the aler to calculate the MPG
</script> </body> </html>
sample output:


