I'm having a truly horrible day :( This is telling me that totalMpg is undefined... It was working just fine until I changed my while loop into a for loop... Can someone please help me with this???
Thank you :)
var milesDriven, // miles driven per tank - entered by user
gallonsUsed, // gallons used per tank - entered by user
mpg, // miles per gallon
tankCounter // tank - counter
//tanks, // number of tanks - entered by user
totalMpg, // total miles per gallon - accumulator
mdValue, // miles driven value
guValue, // gallons used value
tankValue,
totalMpg = 0; // set accumulator to zero
tankCounter = 0; // set counter to zero
//read in number of tanks from user as a string
tankCounter = window.prompt("Enter the number of tanks, -1 to Quit:", "0");
// read in first number from user as a string
milesDriven = window.prompt("Enter miles, -1 to Quit:", "0");
// convert numbers from strings to integers
mdValue = parseInt(milesDriven);
tankValue = parseInt(tankCounter);
// write heading and create table
document.writeln("<H1>Miles Per Gallon</H1>");
document.writeln("<TABLE BORDER = '1' WIDTH = '75%'>");
// go through this loop until the user enters -1
for (var tankCounter = 1; tankCounter <= tankValue; tankCounter++)
tankValue += tankCounter;
{
// read in second number from user as a string
gallonsUsed = window.prompt("Enter gallons used, -1 to Quit:", "0");
// convert numbers from strings to integers
guValue = parseInt(gallonsUsed);
tankCounter = tankCounter + 1; // keep a count of how many tanks
mpg = mdValue / guValue; // divide miles driven by gas used to get miles per gallon
totalMpg = totalMpg + mpg; // accumulate total miles per gallon
// read in first number from user as a string
milesDriven = window.prompt("Enter miles, -1 to Quit:", "0");
// convert numbers from strings to integers
mdValue = parseInt(milesDriven);
}
// do this after the user enters -1
if (tankCounter != 0) // check to see if there are any tanks
{
totalMpg = totalMpg / tankCounter; // divide accumulated miles per gallon by # of tanks to get total mpg
document.writeln("<TR><TD><H2>Total mpg is " + totalMpg + ".</H2></TD></TR>");
document.writeln("</TABLE>");
}
else
document.writeln("<TR><TD>No numbers were entered.</TD></TR>");
This means that only one string ' tankValue += tankCounter;' is in the loop. The code after the '{' is not in the loop. Are you sure it is OK?
its420here
02-04-2003, 12:29 AM
It was working when I had this:
var milesDriven, // miles driven per tank - entered by user
gallonsUsed, // gallons used per tank - entered by user
mpg, // miles per gallon
tankCounter, // number of tanks - counter
totalMpg, // total miles per gallon - accumulator
mdValue, // miles driven value
guValue, // gallons used value
totalMpg = 0; // set accumulator to zero
tankCounter = 0; // set counter to zero
// read in first number from user as a string
milesDriven = window.prompt("Enter mpg, -1 to Quit:", "0");
// convert numbers from strings to integers
mdValue = parseInt(milesDriven);
// write heading and create table
document.writeln("<H1>Miles Per Gallon</H1>");
document.writeln("<TABLE BORDER = '1' WIDTH = '75%'>");
// go through this loop until the user enters -1
while (mdValue != -1)
{
// read in second number from user as a string
gallonsUsed = window.prompt("Enter gallons used, -1 to Quit:", "0");
// convert numbers from strings to integers
guValue = parseInt(gallonsUsed);
tankCounter = tankCounter + 1; // keep a count of how many tanks
mpg = mdValue / guValue; // divide miles driven by gas used to get miles per gallon
totalMpg = totalMpg + mpg; // accumulate total miles per gallon
// read in first number from user as a string
milesDriven = window.prompt("Enter mpg, -1 to Quit:", "0");
// convert numbers from strings to integers
mdValue = parseInt(milesDriven);
}
// do this after the user enters -1
if (tankCounter != 0) // check to see if there are any tanks
{
totalMpg = totalMpg / tankCounter; // divide accumulated miles per gallon by # of tanks to get total mpg
document.writeln("<TR><TD><H2>Total mpg is " + totalMpg + ".</H2></TD></TR>");
document.writeln("</TABLE>");
}
else
document.writeln("<TR><TD>No numbers were entered.</TD></TR>");
But now I am trying to replace the while loop with a for loop... And trying to make it where the user enters the number of tanks... And no, it is not working :(
Sergey Smirnov
02-04-2003, 12:47 AM
You miss comma during initialization(marked with red) :
var milesDriven, // miles driven per tank - entered by user
gallonsUsed, // gallons used per tank - entered by user
mpg, // miles per gallon
tankCounter, // tank - counter
//tanks, // number of tanks - entered by user
totalMpg, // total miles per gallon - accumulator
....
However, the problem I mentioned above exist also. You need to put '{' befor
tankValue += tankCounter;
its420here
02-04-2003, 12:54 AM
Ok, Thank you :) ... Now I have this:
var milesDriven, // miles driven per tank - entered by user
gallonsUsed, // gallons used per tank - entered by user
mpg, // miles per gallon
tankCounter, // tank - counter
//tanks, // number of tanks - entered by user
totalMpg, // total miles per gallon - accumulator
mdValue, // miles driven value
guValue, // gallons used value
tankValue,
totalMpg = 0; // set accumulator to zero
tankCounter = 0; // set counter to zero
//read in number of tanks from user as a string
tankCounter = window.prompt("Enter the number of tanks, -1 to Quit:", "0");
// read in first number from user as a string
milesDriven = window.prompt("Enter miles, -1 to Quit:", "0");
// convert numbers from strings to integers
mdValue = parseInt(milesDriven);
tankValue = parseInt(tankCounter);
// write heading and create table
document.writeln("<H1>Miles Per Gallon</H1>");
document.writeln("<TABLE BORDER = '1' WIDTH = '75%'>");
// go through this loop until the user enters -1
for (var tankCounter = 1; tankCounter <= tankValue; tankCounter++)
{
tankValue += tankCounter;
// read in second number from user as a string
gallonsUsed = window.prompt("Enter gallons used, -1 to Quit:", "0");
// convert numbers from strings to integers
guValue = parseInt(gallonsUsed);
tankCounter = tankCounter + 1; // keep a count of how many tanks
mpg = mdValue / guValue; // divide miles driven by gas used to get miles per gallon
totalMpg = totalMpg + mpg; // accumulate total miles per gallon
// read in first number from user as a string
milesDriven = window.prompt("Enter miles, -1 to Quit:", "0");
// convert numbers from strings to integers
mdValue = parseInt(milesDriven);
}
// do this after the user enters -1
if (tankCounter != 0) // check to see if there are any tanks
{
totalMpg = totalMpg / tankCounter; // divide accumulated miles per gallon by # of tanks to get total mpg
document.writeln("<TR><TD><H2>Total mpg is " + totalMpg + ".</H2></TD></TR>");
document.writeln("</TABLE>");
}
else
document.writeln("<TR><TD>No numbers were entered.</TD></TR>");
And the problem I am having now is that it does not seem to recognize the user input of tanks... I put in 3 when it asks for tanks and it just keeps going and even my -1 to quit does not work :( Infinite loop :( I am in tears trying to make this work :(
its420here
02-04-2003, 01:04 AM
Ok... Well now I see why my -1 to quit isn't working :( I got rid of it when I got rid of my while loop... But how do I implement it into my for loop and why doesn't it recognize my user input of number of tanks??? :(
its420here
02-04-2003, 01:12 AM
Ok... Now I figured out how to implement my -1 to quit... But it is still not recognizing user input of how many tanks :( This is what I have now...
var milesDriven, // miles driven per tank - entered by user
gallonsUsed, // gallons used per tank - entered by user
mpg, // miles per gallon
tankCounter, // tank - counter
//tanks, // number of tanks - entered by user
totalMpg, // total miles per gallon - accumulator
mdValue, // miles driven value
guValue, // gallons used value
tankValue,
totalMpg = 0; // set accumulator to zero
tankCounter = 0; // set counter to zero
//read in number of tanks from user as a string
tankCounter = window.prompt("Enter the number of tanks, -1 to Quit:", "0");
// read in first number from user as a string
milesDriven = window.prompt("Enter miles, -1 to Quit:", "0");
// convert numbers from strings to integers
mdValue = parseInt(milesDriven);
tankValue = parseInt(tankCounter);
// write heading and create table
document.writeln("<H1>Miles Per Gallon</H1>");
document.writeln("<TABLE BORDER = '1' WIDTH = '75%'>");
// go through this loop until the user enters -1
for (var tankCounter = 1; tankCounter <= tankValue; tankCounter++)
{
if (mdValue != -1)
{
tankValue += tankCounter;
// read in second number from user as a string
gallonsUsed = window.prompt("Enter gallons used, -1 to Quit:", "0");
// convert numbers from strings to integers
guValue = parseInt(gallonsUsed);
tankCounter = tankCounter + 1; // keep a count of how many tanks
mpg = mdValue / guValue; // divide miles driven by gas used to get miles per gallon
totalMpg = totalMpg + mpg; // accumulate total miles per gallon
// read in first number from user as a string
milesDriven = window.prompt("Enter miles, -1 to Quit:", "0");
// convert numbers from strings to integers
mdValue = parseInt(milesDriven);
}
else
document.writeln("<TR><TD>No numbers were entered.</TD></TR>");
}
// do this after the user enters -1
if (tankCounter != 0) // check to see if there are any tanks
{
totalMpg = totalMpg / tankCounter; // divide accumulated miles per gallon by # of tanks to get total mpg
document.writeln("<TR><TD><H2>Total mpg is " + totalMpg + ".</H2></TD></TR>");
document.writeln("</TABLE>");
}
else
document.writeln("<TR><TD>No numbers were entered.</TD></TR>");
Sergey Smirnov
02-04-2003, 01:23 AM
simplest soluton for exit from loop was:
gallonsUsed = window.prompt("Enter gallons used, -1 to Quit:", "0");
// convert numbers from strings to integers
guValue = parseInt(gallonsUsed);
if (guValue==-1) break;
its420here
02-04-2003, 01:34 AM
Now I have this and my -1 is not working again :( And input from user for how many tanks is still not working :(
var milesDriven, // miles driven per tank - entered by user
gallonsUsed, // gallons used per tank - entered by user
mpg, // miles per gallon
tankCounter, // tank - counter
//tanks, // number of tanks - entered by user
totalMpg, // total miles per gallon - accumulator
mdValue, // miles driven value
guValue, // gallons used value
tankValue,
totalMpg = 0; // set accumulator to zero
tankCounter = 0; // set counter to zero
//read in number of tanks from user as a string
tankCounter = window.prompt("Enter the number of tanks, -1 to Quit:", "0");
// read in first number from user as a string
milesDriven = window.prompt("Enter miles, -1 to Quit:", "0");
// convert numbers from strings to integers
mdValue = parseInt(milesDriven);
tankValue = parseInt(tankCounter);
// write heading and create table
document.writeln("<H1>Miles Per Gallon</H1>");
document.writeln("<TABLE BORDER = '1' WIDTH = '75%'>");
// go through this loop until the user enters -1
for (var tankCounter = 1; tankCounter <= tankValue; tankCounter++)
{
tankValue += tankCounter;
// read in second number from user as a string
gallonsUsed = window.prompt("Enter gallons used, -1 to Quit:", "0");
// convert numbers from strings to integers
guValue = parseInt(gallonsUsed);
if (guValue==-1) break;
tankCounter = tankCounter + 1; // keep a count of how many tanks
mpg = mdValue / guValue; // divide miles driven by gas used to get miles per gallon
totalMpg = totalMpg + mpg; // accumulate total miles per gallon
// read in first number from user as a string
milesDriven = window.prompt("Enter miles, -1 to Quit:", "0");
// convert numbers from strings to integers
mdValue = parseInt(milesDriven);
}
// do this after the user enters -1
if (tankCounter != 0) // check to see if there are any tanks
{
totalMpg = totalMpg / tankCounter; // divide accumulated miles per gallon by # of tanks to get total mpg
document.writeln("<TR><TD><H2>Total mpg is " + totalMpg + ".</H2></TD></TR>");
document.writeln("</TABLE>");
}
else
document.writeln("<TR><TD>No numbers were entered.</TD></TR>");
Sergey Smirnov
02-04-2003, 01:38 AM
one more:
// convert numbers from strings to integers
mdValue = parseInt(milesDriven);
if (mdValue ==-1) break;
}
its420here
02-04-2003, 01:44 AM
Do both??? Or just the last one??? And any suggestions on how to fix the user input of number of tanks??? I sure do appreciate all of your help btw :)
Sergey Smirnov
02-04-2003, 01:49 AM
BTW, what do you mean when say "input from user for how many tanks is still not working".?
tankValue contains the input number befor loop
its420here
02-04-2003, 01:54 AM
//read in number of tanks from user as a string
tankCounter = window.prompt("Enter the number of tanks, -1 to Quit:", "0");
Starts out asking the user to input how many tanks he is going to input miles and gallons for... Then it should quit asking for miles and gallons from user...
I enter the number 3 when it asks me for the number of tanks and it still keeps asking for miles and gallons after the 3rd set of numbers are entered :(
Sergey Smirnov
02-04-2003, 02:05 AM
remove couple statments from your loop
tankValue += tankCounter;
....
tankCounter = tankCounter + 1; // keep a count of how many tanks
its420here
02-04-2003, 02:18 AM
Ok... I removed those, but still have the problem of user input how many tanks :(
Still keeps asking for miles and gallons after the 3rd set of numbers are entered :(
Sergey Smirnov
02-04-2003, 02:20 AM
post your code again. I want to see it.
its420here
02-04-2003, 02:26 AM
I'm trying to tell it here to get input from user...
tankCounter, // tank - counter
tankValue, // value of tankCounter
for (var tankCounter = 1; tankCounter <= tankValue; tankCounter++)
Sergey Smirnov
02-04-2003, 02:26 AM
I have the following:
<script>
var milesDriven, // miles driven per tank - entered by user
gallonsUsed, // gallons used per tank - entered by user
mpg, // miles per gallon
tankCounter, // tank - counter
//tanks, // number of tanks - entered by user
totalMpg, // total miles per gallon - accumulator
mdValue, // miles driven value
guValue, // gallons used value
tankValue,
totalMpg = 0; // set accumulator to zero
tankCounter = 0; // set counter to zero
//read in number of tanks from user as a string
tankCounter = window.prompt("Enter the number of tanks, -1 to Quit:", "0");
// read in first number from user as a string
milesDriven = window.prompt("Enter miles, -1 to Quit:", "0");
// convert numbers from strings to integers
mdValue = parseInt(milesDriven);
tankValue = parseInt(tankCounter);
// write heading and create table
document.writeln("<H1>Miles Per Gallon</H1>");
document.writeln("<TABLE BORDER = '1' WIDTH = '75%'>");
// go through this loop until the user enters -1
for (var tankCounter = 1; tankCounter <= tankValue; tankCounter++) {
//tankValue += tankCounter;
// read in second number from user as a string
gallonsUsed = window.prompt("Enter gallons used, -1 to Quit:", "0");
// convert numbers from strings to integers
guValue = parseInt(gallonsUsed);
if (guValue==-1) break;
//tankCounter = tankCounter + 1; // keep a count of how many tanks
mpg = mdValue / guValue; // divide miles driven by gas used to get miles per gallon
totalMpg = totalMpg + mpg; // accumulate total miles per gallon
// read in first number from user as a string
milesDriven = window.prompt("Enter miles, -1 to Quit:", "0");
// convert numbers from strings to integers
mdValue = parseInt(milesDriven);
if (mdValue==-1) break;
}
// do this after the user enters -1
if (tankCounter != 0) // check to see if there are any tanks
{
totalMpg = totalMpg / tankCounter; // divide accumulated miles per gallon by # of tanks to get total mpg
document.writeln("<TR><TD><H2>Total mpg is " + totalMpg + ".</H2></TD></TR>");
document.writeln("</TABLE>");
}
else
document.writeln("<TR><TD>No numbers were entered.</TD></TR>");
</script>
its420here
02-04-2003, 02:27 AM
var milesDriven, // miles driven per tank - entered by user
gallonsUsed, // gallons used per tank - entered by user
mpg, // miles per gallon
tankCounter, // tank - counter
//tanks, // number of tanks - entered by user
totalMpg, // total miles per gallon - accumulator
mdValue, // miles driven value
guValue, // gallons used value
tankValue,
totalMpg = 0; // set accumulator to zero
tankCounter = 0; // set counter to zero
//read in number of tanks from user as a string
tankCounter = window.prompt("Enter the number of tanks, -1 to Quit:", "0");
// read in first number from user as a string
milesDriven = window.prompt("Enter miles, -1 to Quit:", "0");
// convert numbers from strings to integers
mdValue = parseInt(milesDriven);
tankValue = parseInt(tankCounter);
// write heading and create table
document.writeln("<H1>Miles Per Gallon</H1>");
document.writeln("<TABLE BORDER = '1' WIDTH = '75%'>");
// go through this loop until the user enters -1
for (var tankCounter = 1; tankCounter <= tankValue; tankCounter++)
{
if (mdValue != -1)
{
//tankValue += tankCounter;
// read in second number from user as a string
gallonsUsed = window.prompt("Enter gallons used, -1 to Quit:", "0");
// convert numbers from strings to integers
guValue = parseInt(gallonsUsed);
//tankCounter = tankCounter + 1; // keep a count of how many tanks
mpg = mdValue / guValue; // divide miles driven by gas used to get miles per gallon
totalMpg = totalMpg + mpg; // accumulate total miles per gallon
// read in first number from user as a string
milesDriven = window.prompt("Enter miles, -1 to Quit:", "0");
// convert numbers from strings to integers
mdValue = parseInt(milesDriven);
}
else
if (tankCounter != 0) // check to see if there are any tanks
{
totalMpg = totalMpg / tankCounter; // divide accumulated miles per gallon by # of tanks to get total mpg
document.writeln("<TR><TD><H2>Total mpg is " + totalMpg + ".</H2></TD></TR>");
document.writeln("</TABLE>");
}
else
document.writeln("<TR><TD>No numbers were entered.</TD></TR>");
}
// do this after the user enters -1
if (tankCounter != 0) // check to see if there are any tanks
{
totalMpg = totalMpg / tankCounter; // divide accumulated miles per gallon by # of tanks to get total mpg
document.writeln("<TR><TD><H2>Total mpg is " + totalMpg + ".</H2></TD></TR>");
document.writeln("</TABLE>");
}
else
document.writeln("<TR><TD>No numbers were entered.</TD></TR>");
Sergey Smirnov
02-04-2003, 02:36 AM
I have tested your code. It works. I entered 2 for number of tanks and it was only two cycles
OK. I have to go from this point. It is to late for me. Good luck.
its420here
02-04-2003, 02:46 AM
Ok... Thank you for all of your help :) I don't know what you have different though because mine is still not working :(
webdeveloper.com
Copyright Internet.com Inc., All Rights Reserved.