User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 401,430 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,909 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 7499 | Replies: 11
Reply
Join Date: Jan 2005
Posts: 6
Reputation: mr woo is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
mr woo mr woo is offline Offline
Newbie Poster

currency convertor

  #1  
Jan 20th, 2005
Hi there,
I'm a complete novice on javascript and am trying to make a basic currency convertor as part of my college course.
I have the beginning sorted, but am struggling to get the code to output the answer.

Can any kind soul tell me where I'm going wrong?

// Aim; to imput an amount in £'s and output in $'s using an exchange rate of 1.78 //

var amountInPounds, amountInDollars, dollarRate;

document.write('Currency Conversion Application A' + '<BR><BR>');

dollarRate = 1.78;


amountInPounds = window.prompt('PLease enter your sterling amount' ,'');
amountinPounds = parsefloat(amountinPounds);
amountInDollars = amountinPounds * dollarRate;
amountinDollars = parsefloat(amountinDollars);
document.write('total is ' + amountinDollars + '<br>');



Any help gratefully appreciated!

Phil
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2004
Location: Washington DC
Posts: 10
Reputation: Javaknight is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 1
Javaknight's Avatar
Javaknight Javaknight is offline Offline
Newbie Poster

Solution Re: currency convertor

  #2  
Jan 21st, 2005
Originally Posted by mr woo
var amountInPounds, amountInDollars, dollarRate;
amountinPounds = parsefloat(amountinPounds);
amountInDollars = amountinPounds * dollarRate;
amountinDollars = parsefloat(amountinDollars);
document.write('total is ' + amountinDollars + '<br>');

Hello Mr. Woo.

I would just like to bring to your attention the suspect lines of code above. (Very simple but effective logic by the way! ) Keep in mind that Javascript is case sensitive so parseFloat() and parsefloat() are two different animals. As is the case with 'amountinPounds' and 'amountInPounds'.

Here is the corrected code back-at-ya:

var amountInPounds, amountInDollars, dollarRate;

document.write('Currency Conversion Application A' + '<BR><BR>');

dollarRate = 1.78;
amountInPounds = window.prompt('PLease enter your sterling amount' ,'');
amountInPounds = parseFloat(amountInPounds);
amountInDollars = amountInPounds * dollarRate;
amountInDollars = parseFloat(amountInDollars);
document.write('total is ' + amountInDollars + '<br>');
Reply With Quote  
Join Date: Jan 2005
Posts: 6
Reputation: mr woo is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
mr woo mr woo is offline Offline
Newbie Poster

Re: currency convertor

  #3  
Jan 21st, 2005
Many thanks for the reply!
With your help I'm getting the hang of it slowly as well as trying to be more precise with the spelling etc

best wishes
Reply With Quote  
Join Date: Jan 2005
Posts: 6
Reputation: mr woo is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
mr woo mr woo is offline Offline
Newbie Poster

Re: currency convertor

  #4  
Jan 27th, 2005
Well I'm back again after hitting another stumbling block.
This time I'm trying to input 4 values which the program will display before outputting as a total.

So far I have managed to get it to prompt for & display the values, but instead of giving a total it displays each individual value.

I'm close, but can't see the error.......any idea's ??
...................................................................
<HTML>
<HEAD>
<TITLE>Electric Bills
</TITLE>
<SCRIPT >

/* Program to read in a known number of data items and store them in an array */

var paymentArray = new Array (4);
var total;

document.write('Array program to display a customers four quarterly bills to an Electricity company & show the total due');

total = 0

for (var quarter = 0; quarter < paymentArray.length; quarter = quarter + 1)
{
paymentArray[quarter] = window.prompt('Enter payment value','')

};
document.write('<BR>' + '<BR>');
document.write('Confirmation of amounts payable' + '<BR>' + '<BR>');


for (var quarter = 0; quarter < paymentArray.length; quarter = quarter + 1)
{
document.write(paymentArray[quarter] + '<BR>')

total = total + paymentArray[quarter]
total = parseFloat(total)
}
document.write('total amount paid is ' + total + '<br>')

</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>
...............................................................................
Reply With Quote  
Join Date: Dec 2004
Location: Washington DC
Posts: 10
Reputation: Javaknight is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 1
Javaknight's Avatar
Javaknight Javaknight is offline Offline
Newbie Poster

Solution Re: currency convertor

  #5  
Jan 27th, 2005
Originally Posted by mr woo
I'm close, but can't see the error.......any idea's ??

Boy do I know how you feel :!: You were very close! Remember this about Javascript code: the '+' operator acts as both an agent to add numbers and concatenate strings. The latter, concatenation of strings, is what was happening in your code. Notice the two changes that I made to make it work:

/* Program to read in a known number of data items and store them in an array */

var paymentArray = new Array (4);
var total = new Number;

document.write('Array program to display a customers four quarterly bills to an Electricity company & show the total due');

total = 0

for (var quarter = 0; quarter < paymentArray.length; quarter = quarter + 1)
{
paymentArray[quarter] = window.prompt('Enter payment value','')

}; 
document.write('<BR>' + '<BR>');
document.write('Confirmation of amounts payable' + '<BR>' + '<BR>');


for (var quarter = 0; quarter < paymentArray.length; quarter = quarter + 1)
{
document.write(paymentArray[quarter] + '<BR>')

total = total + Number(paymentArray[quarter])
total = parseFloat(total) 
}
document.write('total amount paid is ' + total + '<br>')

You are definitely well on your way.
Reply With Quote  
Join Date: Jan 2005
Posts: 6
Reputation: mr woo is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
mr woo mr woo is offline Offline
Newbie Poster

Re: currency convertor

  #6  
Jan 28th, 2005
Many thanks.
It really is tricky, even when I copied the changes over I wrote 'number' with a small 'n' and it had a wobbly, I wish it wasn't so case sensitive!

I'm working on a Function Library now and some string things so watch this space....something tells me ....... 'I'll be back' :lol:
Reply With Quote  
Join Date: Mar 2005
Posts: 1
Reputation: fatkid is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
fatkid fatkid is offline Offline
Newbie Poster

Re: currency convertor

  #7  
Mar 11th, 2005
wish i had thought to ask the answers to my open uni m150 tma in here
Reply With Quote  
Join Date: Feb 2005
Location: Essex
Posts: 5
Reputation: MrsParadox is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
MrsParadox MrsParadox is offline Offline
Newbie Poster

Solution Re: currency convertor

  #8  
Mar 12th, 2005
Originally Posted by fatkid
wish i had thought to ask the answers to my open uni m150 tma in here
Well like me it feels a million times better to actually get it done by yourself plus tutors arent stupid I`m sure they are fully aware of forums like these so not worth the risk.
Interesting tho
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb JavaScript / DHTML / AJAX Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 12:37 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC