/    Sign up×
Community /Pin to ProfileBookmark

Please help me!

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

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@NedalsSep 11.2005 — Is this a school project?

There are a number of problems with this code.

1.

<script type="text/javascript">

<!--

...

//--> and don't forget this

</script>

2.

Once you enter gallons and miles the first time, you write the result, but how does it write the second time. Think about putting your 'prompts' in a function and call it 'onload' and again after the 'write' statements.

3.

totMpg, totGallonsUsed, totMpg have not been declared, nor set to any value, before the write. So they will be undefined. You'll need global variables for these.

4.

Using document.write will give you unexpected results on the repeated writes. Consider using innerHTML here instead.

5.

Put the rest of your computations in the function mentioned in (1) above.

6.

Consider moving all the javascript into the <head> section.
Copy linkTweet thisAlerts:
@pj_59authorSep 11.2005 — Thanks...for pointed out the //--> I forgot to add that and few other things you mentioned plus I wasn't completely done with my scripts. I wanted to make sure I was on the right track before continuing. Needless to say, my book doesn't give a very good example how the layout should be written out. It only shows section of it...it would be nice if the book would show step by step on how to keep adding each control structure. I have read and reread the books and even did the exercises. I had no problem doing the exercises because they were only 3 to 5 lines of control structure with only one or two variables. I took Programming and Logic Design and didn't have any trouble but I don't know why I can't do it in JavaScript. It is so different.

Anyway, I want to thank you for your suggestion and want to let you know that I am not asking anyone to do my work for me. I thought maybe someone could explain it better to me than what was in the book. I will keep on trying and do what you suggested I do. Again, thank you!!
Copy linkTweet thisAlerts:
@NedalsSep 11.2005 — Here's a guide, but no code, that may help.
<i>
</i>In the &lt;head&gt;
&lt;script type="text/javascript"&gt;
&lt;!--
declare globals using 'var'

function input() {
// input prompts
..
if (input_data&gt;0) {
//do all computations including totals
// don't forget to protect against divide by 0
...
// build an output string
...
// display string using 'innerHTML'
}

// add a routine to slightly delay repeat.
// This will stop any recursive action that would
// occure if you simply called 'input()' again
repeat();
}

function repeat(){
call input() again using 'setTimeout'
return false;
}
//--&gt;
&lt;/script&gt;
&lt;/head&gt;

&lt;body onload=???&gt; ?? do you know what goes here ??
&lt;div id="result"&gt;&lt;/div&gt;
&lt;/body&gt;

Happy coding ?

Call back if you need more help.
Copy linkTweet thisAlerts:
@pj_59authorSep 12.2005 — Thanks Nedals! I finally got it figure out...I did a little bit different than what you suggested but at least it is working now. I know there are many ways of doing it and it can be confusing! Again, thank you so much for your help!
×

Success!

Help @pj_59 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...