[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

I'm New To JavaScript (Pt. II)

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 17
Thread images: 2

I am trying to make my code work, but it won't work! I want error messages to come up beside each input field depending on whether the input is a proper number, and I want the "clear" button to clear everything, not just the input fields. What needs to change?

<script>

var $ = function (id) {
return document.getElementById(id);
}

var mpgCalc = function() {
var milesDriven = document.getElementById("miles_driven").value;
var gallonsUsed = document.getElementById("gallons_used").value;
var priceGallon = document.getElementById("price_gallon").value;
var milesOverGallons;
var gasMileage;
var gallonsByDollars;
var tripCost;
var milesDrivenError;
var gallonsUsedError;
var priceGallonError;

switch(true) {
case (isNaN(milesDriven) || (milesDriven < 0)):
milesDrivenError = "You must input a proper number.";
gasMileage = "";
tripCost = "";
case (isNaN(gallonsUsed) || (gallonsUsed < 0)):
gallonsUsedError = "You must input a proper number.";
gasMileage = "";
tripCost = "";
case (isNaN(priceGallon) || (priceGallon < 0)):
priceGallonError = "You must input a proper number.";
gasMileage = "";
tripCost = "";
break;
case ((gallonsUsed >=0) && (priceGallon >= 0)):
gallonsByDollars = (gallonsUsed * priceGallon);
tripCost = "The cost of your trip is $" + parseFloat(gallonsByDollars.toFixed(2)) + ".";
case ((milesDriven >=0) && (gallonsUsed >= 0)):
milesOverGallons = (milesDriven / gallonsUsed);
break;
}

(continued...)
>>
>>299787

Hopefully 4chan does not think it's SPAM on the fourth try....
>document.getElementById("mpg").innerHTML = gasMileage;
>document.getElementById("cost").innerHTML = tripCost;
>document.getElementById("miles_driven_error").innerHMTL = milesDrivenError;
>document.getElementById("gallons_used_error").innerHMTL = gallonsUsedError;
>document.getElementById("price_gallon_error").innerHMTL = priceGallonError;
}
>>
>>299787

Isn't there a special way to format code on 4chan?

It keeps registering my thing as spam when I'm just in a bind with this programming assignment I've got...


</head>

<body>
<h1>Calculate Your MPG</h1>
<h2>Enter the information below to see if you are getting good mileage!</h2>

<form id="mpgform">

<label for="miles_driven">Miles Driven: </label>
<input type="text" id="miles_driven" name="miles_driven">
<span class="error" id="miles_driven_error"></span>

<label for="gallons_used">Gallons of Gas Used: </label>
<input type="text" id="gallons_used" name="gallons_used">
<span class="error" id="gallons_used_error"></span>

<label for="price_gallon">Price per gallon: </label>
<input type="text" id="price_gallon" name="price_gallon">
<span class="error" id="price_gallon_error"></span>

<input type="button" id="btnCalc" value="Calculate the MPG" class="clear">
<input type="button" id="btnClear" value="Clear Form">

<p id="mpg"></p>

<p id="cost"></p>

</form>

</body>
</html>
>>
Put it on pastebin or similar and then link it, that might make it easier to display it.
>>
>>299805

That's a great suggestion, here is my thing!

https://pastebin.com/qRee8G8U
>>
>>299823

The 'true' is there so that the switch can use boolean operators, or can I have both?
>>
>>299787
from what I can see so far, you need to replace the ".value" in lines 113 through 121 with ".innerHTML".
This doesn't fix everything though so I'll keep looking.
>>
>>299819
Line 97, try replace gasMileage with milesOverGallons.
>>
>>299826
>>299829

Okay, I'll do that, just a sec...
>>
>>299829
well what he needs to do is replace all instances of "gasMileage" with "milesOverGallons" or vise versa since they are the same thing.
>>
>>299829

I accidentally clipped part of my code, I'll make a new pastebin:

https://pastebin.com/Ge0S6pxh

>>299826

And your suggestion worked! It now clears the stuff in the HTML. Very groovy.
>>
>>299831

I just made a mistake on that spot, sorry about that, please see: https://pastebin.com/Ge0S6pxh
>>
>>299836
I fixed the error messages not showing up by putting the "document.getElementById("whatever").innerHTML = variable" inside of the case statements. not sure why this made any difference but it did. I noticed however that when you input a NaN in any field all of the fields after it show an error too even when they shouldn't. After further testing this seems to be a bug with the switch statement itself, so you are probably better off just using if statements here instead.
>>
>>299836
Holy shit, finally found it...

Lines 100, 101 an 102 is mistyped... HMTL instead of HTML.
>>
>>299850
Wow, I can't believe I didn't see that. I guess that's why this >>299849 made a difference...
>>
>>299787

So things are looking good so far, and really now all I'm wondering about is my switch statement.

I understand things like conditionals and booleans, but I am not so familiar with switches.

I am under the impression I need this because I need multiple conditions to be evaluated and yield outputs independently of each other, and I'm not certain how to do that with an if-else that wouldn't be gigantic.

But this switch statement seems to ignore the first three cases, where I am saying EITHER this input is not a number OR it's a number but less than zero.

In which case it should output an error message.

It's not doing that though.
>>
>>299849
>>299850
>>299855

Okay...

That explains it!

It works now.

Thanks everyone for that.
Thread posts: 17
Thread images: 2


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.