Thread titleFromLast replied
Now searching...
Neoseeker Forums » » Web Coding » Need help building a javascript arithmetic form » Post Reply

Note to Guests: For the enjoyment of other visitors, and to help keep this forum tidy and working smoothly, please make sure you search the forum before posting your question.

hideOriginal Post

Jul 30, 12 at 10:25amhiigaran

so currently i have instructions on how to calculate the extra cost on an electricity bill when a computer is constantly running at full load for a specified set of hours. currently, i have these instructions:

Maximum power consumption of CPU and/or GPU - idle power consumption = Power difference
Power difference / PSU efficiency (between 0.8 to 0.95) = Power draw
Power draw * average hours of usage per day * 0.03 = Kilowatt-hours per month
Kilowatt-hours per month * cost per kilowatt-hours = Cost per month
i would like to have something a little more sophisticated. id like a form to allow a user to input five different variables: max power consumption, idle power consumption, efficiency, hours of use, cost per kw-h. id then need the scripting required to make the necessary arithmetic and output the cost per month.

heres the catch...the only javascript i have ever done was copying and pasting a show/hide script, so i pretty much have no idea how the language even looks like (kinda sorta).

while i would appreciate someone doing it all for me ( ), id like to learn along the way as well, if possible.

Thread Recap (last 10 posts from newest to oldest)

Jul 31, 12 at 12:13pm

perfect! thanks.

i think that was everything i needed. appreciate the help, mate.

Jul 31, 12 at 12:01pm

You need to use the isNaN function

if(isNaN(cost)) { cost = 0.00; }

Jul 31, 12 at 11:19am

after some searching around, and a little help from someone, i found the right code (and added two more bits to it):

if(cost === NaN){cost = 0.00};
if(cost < 0){cost = 0.00};
cost = cost.toFixed(2);

EDIT: except the first line doesnt work

Jul 31, 12 at 11:03am

You'd have to do something like

roundedCost = Math.round(cost*100)/100;

To get the rounded amount to 2 decimal places.

Jul 31, 12 at 9:58am

perfect. time to get to work!

thanks for your help, both you, and tekmosis. cheers!

EDIT: one more there a way to limit the output numbers? for instance, you might get a result of, say, $10.455, but i would like to limit it to two decimal places, and round off (in this case, to $10.46).

Jul 31, 12 at 9:42am

I honestly have no idea, I rewrote the JavaScript myself, I just copied the form.

Yeah you can make the fields shorter with a bit of CSS.

input[type='text'] {
    width: 100px;

Edit: Actually I just tested your code in JsFiddle and it seems to work fine, did you click "update" before testing it?

Jul 31, 12 at 2:58am

yes, that works! what did you add (or remove)? to get it to work?

also, am i able to shorten the length of the text fields?

Jul 31, 12 at 12:53am

hiigaran, I just threw this together, seems to be getting the right amount.

<!doctype html>
<html lang="en">
		<meta charset="utf-8"/>
		<form id="calculations">
			<input type="text" id="max_power" /> Max Power<br /><br />
			<input type="text" id="idle_power" /> Idle Power<br /><br />
			<input type="text" id="efficiency" /> Efficiency<br /><br />
			<input type="text" id="hours_use" /> Hours of Use<br /><br />
			<input type="text" id="cost_kwh" /> Cost per kw-h<br /><br />
			<input type="button" id="calculate" value="Calculate" />
		<div id="output"></div>
		<script type="text/javascript">
			document.getElementById('calculate').addEventListener('click',function() {
				var max_power = document.getElementById('max_power').value;
				var idle_power = document.getElementById('idle_power').value;
				var efficiency = document.getElementById('efficiency').value;
				var hours_used = document.getElementById('hours_use').value;
				var cost_kwh = document.getElementById('cost_kwh').value;
				var powerDifference = max_power - idle_power;
				var powerDraw = powerDifference/efficiency;
				var kwh = powerDraw*hours_used*0.03;
				var cost = kwh*cost_kwh;
				document.getElementById('output').innerHTML = cost;

Jul 30, 12 at 3:20pm

thanks for the help so far, anyway.

Jul 30, 12 at 3:14pm

I don't do the maths so well. Perhaps someone else can weigh in on what the problem might be. It definitely has something to do with the equations. The HTML/JS is working as is though so I hope you can get it worked out!

Powered by neoforums v2.3.7 (Bolieve)
Copyright Neo Era Media, Inc. 1999-2015   |   Forum Rules   |   Forum FAQ   |   Neoseeker Terms of Use   |   Supermods On Duty [ server id: web3 ··· elapsed: 0.0503]
Chat and Lounges
Game Platforms
Mobile Platforms