Skip to content

Instantly share code, notes, and snippets.

@dennispipper
Created September 10, 2013 16:42
Show Gist options
  • Save dennispipper/6512122 to your computer and use it in GitHub Desktop.
Save dennispipper/6512122 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Calculate Miles Per Gallon</title>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js">
// for versions IE 6-8
</script>
<script>
//create a loop that gets the miles and checks that it is a number
do {
//create a var named miles from the suer prompt box
var miles = prompt("Enter miles driven");
//convert the miles var contents to a number
miles = parseFloat(miles);
//check to make sure it is a number and is more than zero
if (isNaN(miles) || miles <= 0) {
alert("Miles must be a number greater than zero");
}
}
//the while portion of the do-while loop, executes as long as the do is true
while (isNaN(miles) || miles <= 0);
//get input from user in a prompt box
do {
var gallons = prompt("Enter gallons of gas used");
//convert the string to a number
gallons = parseFloat(gallons);
//checking for valid values
if (isNaN(gallons) || gallons <= 0) {
alert("Gallons must be a number greater than zero");
}
}
while (isNaN(gallons) || gallons <= 0);
//create a variable that divides miles/gallons
var mpg = miles/gallons;
//converts it to a number
mpg = parseInt(mpg);
//alerts the user the results
alert("Miles per gallon = " + mpg);
</script>
</head>
<body>
<section>
<h1>This page is displayed after the JavaScript is executed</h1>
</section>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment