/    Sign up×
Community /Pin to ProfileBookmark

user gives a value, javascript converts it and outputs another value

Hi all,

I am pretty stuck on something so simple its untrue. I never really learnt javascript properly and the stuff i did learn was quite basic. I hope you can help me.

I have a form, with an input box. The user is required to input a value, which will be converted and then output as 4 different values. Here’s an example of what i mean:

[CODE]
function calculate() {
var dummy1 = 600;
var dummy2= 700;
var dummy3 = 500;
var dummy4 = 250;
var userValue;
var newDummy1;
var newDummy2;
var newDummy3;
var newDummy4;

userValue = parseFloat(document.myForm.myInputBoxName.value);
userValue = (userValue*1000)*1000;

newDummy1 = userValue*dummy1;
newDummy2 = userValue*dummy2;
newDummy3 = userValue*dummy3;
newDummy4 = userValue*dummy4;
}[/CODE]

So basically the user value is taken as a string, converted to a floating point and then multiplied by a bunch of numbers which are assigned to variables – I need these 4 variables to be shown in a table which is below the form. My question is, basically, HOW!! It’s very simple I know, but I just dont know how to do it and I havent had much luck looking at examples as they dont really teach, they just do, if that makes sense.

I really hope someone can help me – please explain as much as possible – I am a complete novice at JavaScript.

Thanks in advance and I hope for a quick reply as I need to have this finished at work 2moro! (I’m going home now – you have one evening – no pressure hee hee)

Theo

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@boskoApr 25.2007 — First make table and put different id in each td tag, i.e.
[CODE]<td id='your id'></td>[/CODE]
Then add to the end of your javascript function [CODE]document.getElementById('id of first td tag').innerHTML = newDummy1;[/CODE]
You have to repeat this js code for each newdummy value and ofcourse with different id.
Copy linkTweet thisAlerts:
@spaghbolauthorApr 25.2007 — Thank you very much for your reply. I will try it out first thing tomorrow morning at work.

Theo
Copy linkTweet thisAlerts:
@spaghbolauthorApr 26.2007 — Hi,

I did what you said and nothing appears still...do i need to add the id's into my css file and also do i use 'one' or "two" for the id in the td tag?

Thanks

Edit - mr clever over here forgot to put the onClick action in the form button. My form actually does something now yey! Thank you for your very quick reply yesterday, I appreciate it!

Theo
Copy linkTweet thisAlerts:
@spaghbolauthorApr 26.2007 — ok I'm back lol (didnt take long did it!)

The numbers I am getting are floating point numbers but I want to round them to 4 decimal places. How would I do this with the following code added to my original code above in mind:

[CODE]document.getElementById('myID').innerHTML = ("£" + newDummy1);[/CODE]

I know its something to do with Math.round(x) or x=Math.round(100*100)/100; or something like that (no idea what i'm typing there haha) but how would I do it with the code I have as i Have specific calculations in the code which need to be done.

If it doesnt make sense please let me know. I hope someone can help.

Theo
Copy linkTweet thisAlerts:
@boskoApr 26.2007 — I found this on Google (maybe you can try it also?)

[CODE]num = 930.9805;
result = num.toFixed(3);[/CODE]


it will output 930.980

toFixed(value) will round floats to its value decimal places.
Copy linkTweet thisAlerts:
@spaghbolauthorApr 26.2007 — oh yeah i found that too, i used it in the end but I read so much about you shouldnt use it, you should use this and that and the other, and it just confused me so I thought I'd post and see what people suggest for my specific code.

Thanks for your reply.

Theo
Copy linkTweet thisAlerts:
@s_b37Apr 26.2007 — to round numbers, i generally use:
[CODE]
var num=1023.45982;
var dp=1;
var exp = Math.pow(10,dp);
num = Math.round(num*exp)/exp;
[/CODE]
×

Success!

Help @spaghbol spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...