/    Sign up×
Community /Pin to ProfileBookmark

Passing variables between functions

Can anyone tell me how to pass a variable that is established in one function on to a second function for further calculations.
The script I have now looks like this:

function price(size){
if (size==1) var a=.5;
if (size==2) var a=1;
if (size==3) var a=1.5;
if (size==4) var a=2;
if (size==5) var a=2.5;
if (size==6) var a=3;
if (size==7) var a=3.5;
if (size==8) var a=4;
if (size==9) var a=4.5;
if (size==10) var a=5;
if (size==11) var a=5.5;
if (size==12) var a=6;
parent.bottom.document.body.innerHTML=”your total is: $”+ a * len;
}

var shad=0;

function shadow(value){
if (value==1) var shad=2;
else var shad=1;
parent.bottom.document.body.innerHTML=”your total is: $”+ a * len * shad;
}

I keep getting an error with the second function that says ” a is undefined”

Again, I would like “var a” (which is established in function price() ) to be passed to function shadow() for further calculations.

Any help will be appreciated. Thank you.

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@JHLJul 11.2003 — in prize():

shadow(a);

this will pass the variable a from prize() to shadow().

another way is that you can declare 'a' as a global variable so that it can be access by any function.
Copy linkTweet thisAlerts:
@drgnfli4authorJul 11.2003 — Thanx JHL,

the method you first described will work perfectly for this script but for future refrence, How do I declare 'a' as a global variable?
Copy linkTweet thisAlerts:
@CharlesJul 11.2003 — [font=georgia]Client side JavaScript doesn't allow you to make global variables. THe best you can do is to give a Window object a property. You can do that explicitly, [font=monospace]self.giantSays = 'fee, fie, foe, fum'[/font] or implicitly by omitting the word 'var'.

And you can shorten your code and get rid of all of those 'if' statements by using an Array literal and the ternary operator. In JavaSCript every line of code has a lot of overhead so it's good to keep things short.[/font]

[font=monospace]<script type="text/javascript">

<!--

function price(size){

var value = ['', 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6][size];

var shad = value == 1 ? 2 : 1;

parent.bottom.document.body.innerHTML="your total is: $"+ a * len * shad;

}

// -->

</script>[/font]
Copy linkTweet thisAlerts:
@drgnfli4authorJul 11.2003 — Thanx Charles,

I will certainly use your suggestions.
×

Success!

Help @drgnfli4 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 6.1,
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: @meenaratha,
tipped: article
amount: 1000 SATS,

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

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...