...

View Full Version : Resolved Calculator Help



ThatGuy16
05-29-2012, 01:21 AM
Hello there.

I'm writing a very simple calculator that allows the user to input values so the "engine" (what I call it) can return a product. It uses .innerHTML to grab a form element's value and replace with the user-supplied variable.

When I run the code, it returns NaN as a result. O_O I'm only familiar with basic Javascript, so this is about the limit of my programming knowledge. XD

Despite that, my suspicions for why the code won't work lies with the 'engine'. I want it to parse user-supplied variables to return a proper result; it won't do this. Also, the engine should only parse integers and decimals. How can I set about doing both of these, so my calculator can work?

Thanks in advance for your help! :)



<head>
<script type="text/javascript">
function exchangeRate()
{
var num1 = document.getElementById('num1').value;
var num2 = document.getElementById('num2').value;
var num3 = document.getElementById('num3').value;
document.getElementById('result').innerHTML = ((num1*100)/num2)*num3); //the engine
}
</head>
</script>

<body>
<p>The exchange rate is <b id='result'>null</b></p>
<form name="input">
<input type='text' id='num1' value='Cash Here'/>
<br>
<input type='text2' id='num2' value='Exchange Rate Here'/>
<br>
<input type='text3' id='num3' value='Units Here'/>
<br>
<input type='button' onclick='exchangeRate()' value='Calculate'/>
</form>
</body>

xelawho
05-29-2012, 01:40 AM
I'm surprised it returns anything at all. You need to learn to use the debugger (Chrome has a good one).
You have uneven brackets here:

((num1*100)/num2)*num3);

and your script closing tag is after your head tag.

fix those and it works better. You can validate the input fields to make sure that only numbers are entered. This has been covered here and elsewhere endlessly - try searching and if you run into real problems, post back

ThatGuy16
05-29-2012, 01:55 AM
I'm surprised it returns anything at all. You need to learn to use the debugger (Chrome has a good one).
You have uneven brackets here:

((num1*100)/num2)*num3);

and your script closing tag is after your head tag.

fix those and it works better. You can validate the input fields to make sure that only numbers are entered. This has been covered here and elsewhere endlessly - try searching and if you run into real problems, post back

It works! Thanks. :)

low tech
05-29-2012, 02:43 AM
Hi


It works!

That's great:thumbsup:

tip: now look into

Number()
parseFloat()
parseInt()

LT

ThatGuy16
05-29-2012, 02:58 AM
Hi



That's great:thumbsup:

tip: now look into

Number()
parseFloat()
parseInt()

LT

Sounds like good advice. I'll check it out. :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum