Question

In: Computer Science

<!DOCTYPE html> <html> <body> <script> // // A car's miles-per-gallon (MPG) can be calculated with the...

<!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>

Solutions

Expert Solution

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:


Related Solutions

<!DOCTYPE html> <html> <body> <script> // // The colors red, blue, and yellow are known as...
<!DOCTYPE html> <html> <body> <script> // // The colors red, blue, and yellow are known as the primary colors because they // cannot be made by mixing other colors. When you mix two primary colors, you // get a secondary color, as shown here: // // When you mix red and blue, you get purple. // When you mix red and yellow, you get orange. // When you mix blue and yellow, you get green. // // Design a program...
A car manufacturer claims that the miles per gallon (mpg) of all its midsize cars can...
A car manufacturer claims that the miles per gallon (mpg) of all its midsize cars can be modeled with a normal model with N(33, 1.70). What proportion of cars have miles per gallon less than 31.2 [P(x ≤31.2 mpg)]? What proportion of cars will have miles per gallon greater than 36 [P(x ≥36 mpg)]? What proportion of cars will have miles per gallon less than 30[P(x ≤30 mpg)]? What proportion of cars will have miles per gallon between 32 and...
It is necessary for an automobile producer to estimate the number of miles per gallon (mpg)...
It is necessary for an automobile producer to estimate the number of miles per gallon (mpg) achieved by its cars. Suppose that the sample mean for a random sample of 5050 cars is 30.630.6 mpg and assume the standard deviation is 3.63.6 mpg. Now suppose the car producer wants to test the hypothesis that μμ, the mean number of miles per gallon, is 31.631.6 against the alternative hypothesis that it is not 31.631.6. Conduct a test using a significance level...
It is necessary for an automobile producer to estimate the number of miles per gallon (mpg)...
It is necessary for an automobile producer to estimate the number of miles per gallon (mpg) achieved by its cars. Suppose that the sample mean for a random sample of 5050 cars is 30.630.6 mpg and assume the standard deviation is 3.63.6 mpg. Now suppose the car producer wants to test the hypothesis that μμ, the mean number of miles per gallon, is 31.631.6 against the alternative hypothesis that it is not 31.631.6. Conduct a test using a significance level...
Your car gets 22 miles per gallon (MPG) at 55 miles per hour (MPH) and 18...
Your car gets 22 miles per gallon (MPG) at 55 miles per hour (MPH) and 18 MPG at 65 MPH. At what speed should you make a 450-mile trip 1. If gas costs $2.95 per gallon and your time is worth $17/hour? 2. If gas costs $3.80 per gallon and your time is worth $11.5/hour? 3. If gas costs $4.75 per gallon and your time is worth $8.9/hour? 4. Building an Excel spreadsheet to calculate the total trip cost for...
Your car gets 25 miles per gallon (mpg) at 60 miles per hour (mph) and 18...
Your car gets 25 miles per gallon (mpg) at 60 miles per hour (mph) and 18 mpg at 70 mph. At what speed should you make a 600-mile trip: 1. If gas costs $3 per gallon and your time is worth $12 per hour? 2. If gas costs $4 per gallon and your time is worth $15 per hour? 3. If gas costs $5 per gallon and your time is worth $10 per hour? 4. Build a spreadsheet to calculate...
Your car gets 29 miles per gallon (mpg) at 60 miles per hour (mph) and 25...
Your car gets 29 miles per gallon (mpg) at 60 miles per hour (mph) and 25 mpg a 70 mph. At what speed should you make a 525-mile trip: If gas costs $3 per gallon and your time is worth $18 per hour If gas costs $4 per gallon and your time is worth $12 per hour If gas costs $5 per gallon and your time is worth $9 per hour
Can someone verify why my code isnt running <!DOCTYPE html> <html> <body bgcolor=aqua text=purple> <script type="text/javascript">...
Can someone verify why my code isnt running <!DOCTYPE html> <html> <body bgcolor=aqua text=purple> <script type="text/javascript"> funtion oldMacVerse(animal, sound) //Assumes: animal is the name of an animal, sound is the sound it makes //Results: displays a version of the song "Old MacDonals Had a Farm" in outputDiv { document.getElementById('outputDiv').innerHTML =    '<p>Old MacDonald had a farm, E-I-E-I-O.<br>' +    'And on that farm he had a ' + animal + ', E-I-E-I-O.<br>' +    'With a ' + sound +...
<!DOCTYPE html> <html> <body> <!-- replace the text below with your name!--> <!-- --> <!-- -->...
<!DOCTYPE html> <html> <body> <!-- replace the text below with your name!--> <!-- --> <!-- --> <title> TYPE YOUR NAME HERE</title> <script> // // A bug collector collects bugs every day for five days. Write a function that keeps // a running total of the number of bugs collected during the five days. The loop // should ask for the number of bugs collected for each day, and when the loop is // finished, the program should display the total...
<!DOCTYPE html> <html> <body> <!-- replace the text below with your name!-->    <!-- -->   ...
<!DOCTYPE html> <html> <body> <!-- replace the text below with your name!-->    <!-- -->    <!-- -->    <title> name </title> <script> // // Write a function that calculates the amount of money a person would earn over // a period of years if his or her salary is one penny the first day, two pennies // the second day, and continues to double each day. The program should ask the // user for the number of years and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT