? ? I am trying to calculate the miles per gallons. I am a newbie so spare with me. I need to prompt a visitor for total miles and gallons used…set a sentinel such as -1 for total miles to exit. Then for each tankful, display miles driven, gallons used, and mileage. Also display total miles driven, total gallons used and over all mileage. I want use prompt and document.write. This is what I got so far but I can’t get it to work. It work when I had the prompt and document.write but then when I add the rest….nothing seems to work. I assumed I got the codes in the wrong place but can’t figure out where exactly it is suppose to go. I really appreciate any suggestion or help on this matter. It seems the more I keep moving the codes around….I get confused. Thanks!
<html>
<head>
<title>MPG Calculator</title>
</head>
<body bgcolor=”#FFFF99″ text=”#000000″>
<script language=”javascript” type=”text/javascript”><!–
gallonsUsed = prompt(“Enter gallons used, -1 to Exit”, “0”);
milesDriven = prompt(“Enter miles driven, -1 to Exit”, “0”);
mpg = (milesDriven/gallonsUsed)
alert(“Your miles per gallons is:” + mpg)
document.write(“<h3>Gallons used: ” + gallonsUsed + “</h3>”)
document.write(“<h3>Miles driven: ” + milesDriven + “</h3>”)
document.write(“<h3>Miles per gallons: ” + mpg + “</h3>”)
document.write(“<h3>Total miles driven: ” + totMpg + “</h3>”)
document.write(“<h3>Total gallons used: ” + totGallonsUsed + “</h3>”)
document.write(“<h3>Total miles per gallons: ” + totMpg + “</h3>”)
// go through loop until the user enters -1
for (gallonsUsedCounter = 1; gallonsUsedCounter < gallonsUsedValue; gallonsUsedCounter++) {
gallonsUsedValue += gallonsUsedCounter;
}
gallonsUsed = prompt(“Enter gallons used, -1 to Exit”, “0”) {
gallonsUsedValue = parseInt(gallonsUsed);
if (gallonsUsedValue == -1);
break;
}
mpg = milesDrivenValue/gallonsUsedValue;
milesDriven = prompt(“Enter miles driven, -1 to Exit”, “0”);
milesDrivenValue = parseInt(milesDriven);
if (milesDrivenValue == -1)
break;
}
if (gallonsUsedCounter != 0)
totMpg = totMpg/gallonsUsedCounter;
totMpg = totMpg + mpg;
</script>
</body>
</html>