...

# Problems with the Euro Calculator

enter_oblivion
08-11-2006, 06:30 AM
I've been learning JavaScript for a two months now, however I have learnt quite a lot. However I've had difficulty with an example from a book called "Eways - Javascript for Beginners". The exercise is a euro calculator in which you simply enter the amount in the old Francs etc. and it converts the currency. Simply however, there nothing happens.

Here is the code script:
[INDENT]
<script language="text/javascript">
var a=0
function calculate()

if (a==0)
{
document.forms[0].euro.value=Math.round(document.forms[0].amount.value / document.forms[0].currency[document.forms[0].currency.selectedIndex].value * 100 / 100
}

if (a==1)
{
document.forms[0].amount.value=Math.round(document.forms[0].euro.value * document.forms[0].currency[document.forms[0].currency.selectedIndex].value * 100 / 100
}
</script>

And body code:

<input name="amount" size="10" onChange="a=0">
<select name="currency" onChange="calculate()" onFocus="calculate()">
<option value="13.7603">ATS</option>
<option value="40.3399">BEF/LUF</option>
<option value="1.95583">DEM</option>
<option value="166.386">ESP</option>
<option value="5.94573">FIM</option>
<option value="6.55957">FRF</option>
<option value="0.787564">IEP</option>
<option value="1936.27">ITL</option>
<option value="2.20371">NLG</option>
<option value="200.482">PTE</option>
</select>
corresponds to
<input name="euro" size="10" onChange="a=1">
Euros.<br>
<input type="button" value="calculate" onClick="calculate()">

</INDENT>

Thanks, any help will be appreciated. I am surprised why its not working cause it's from a published book.

felgall
08-11-2006, 08:11 AM
You are missing a ) on the end of each of the calculations to close the Math.round(...)

function calculate()

if (!a)
{
document.forms[0].euro.value=Math.round(document.forms[0].amount.value / document.forms[0].currency[document.forms[0].currency.selectedIndex].value * 100) / 100;
}
else
{
document.forms[0].amount.value=Math.round(document.forms[0].euro.value * document.forms[0].currency[document.forms[0].currency.selectedIndex].value * 100) / 100;
}

enter_oblivion
08-11-2006, 12:18 PM
You're a lifesaver. I've been so confused. I've even parsed the code in Notepad++ to no avail. Thankyou for the help. I also checked out your web site. It's very informative and I'll use it a reference when I strike trouble in the future. Cheers