Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-06-2012, 04:58 PM   PM User | #1
acolod
New to the CF scene

 
Join Date: Jan 2012
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
acolod is an unknown quantity at this point
Using the result of a function in next funtion

Hello all,

I am new to these forms, and new to Javascript as well. I have gone through some lessons, and have been trying to write a simple script on my own. Here is what I am trying to do:

I am calculating a volume, and want to display the result of that calculation. I then want to take the result and multiply it by 2 and display that as well. Here is what I have so far:

Code:
function volume (l, w, h) {
  return l * w * h;
}

console.log("Volume = " + volume (2, 2, 2));
So far so good... but I cant figure out how to then take that result that was displayed in the console and save it as a variable (if that would be the correct way to do it) so that I can modify the result by 2.

I have tried several things with no luck.

How can I save the result of a function or pass it to another function?

Thanks in advance!
acolod is offline   Reply With Quote
Old 01-06-2012, 05:16 PM   PM User | #2
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
it's kind of the same as what you are doing - just create a variable to hold the value of the returned function:

Code:
<html>
<head>
</head>
<body>
<input type="button" value="calculate" onclick="checkVal()"/>
<script>
function volume (l, w, h) {
return l * w * h;
}

function checkVal(){
vol=volume (2, 2, 2);
alert(vol)
}
</script>
</body>
</html>
xelawho is offline   Reply With Quote
Old 01-06-2012, 05:29 PM   PM User | #3
acolod
New to the CF scene

 
Join Date: Jan 2012
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
acolod is an unknown quantity at this point
Thanks for the reply. So the checkVal function saves the result of the volume funtion as the variable vol? What does alert(vol) do? Also, if I tired writing a function using the vol variable and got the error "ReferenceError: vol is not defined". My code is below:

Code:
function volume (l, w, h) {
  return l * w * h;
}
 
console.log("Volume = " + volume (2, 2, 2));
 
function checkVal(){
vol = volume (2, 2, 2);
alert(vol)
}
 
function volumeX2 (vol, x2) {
  return vol * x2;
}
 
console.log("Volume times 2 = " + volumex2 (vol, 2));
Run
Any more help is appreciated!
acolod is offline   Reply With Quote
Old 01-06-2012, 05:42 PM   PM User | #4
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
the alert is just a way of checking the value of the variable.

what you're up against now is a scope issue - you have declared the vol variable as local, but that value is only available to the checkValue function. You can either declare it globally (and make it available to all functions):

Code:
<html>
<head>
</head>

<body>
<input type="button" value="calculate" onclick="checkVal()"/>
<input type="button" value="show log" onclick="showLog()"/>
<script>

var vol;

function volume (l, w, h) {
return l * w * h;
}

function checkVal(){
vol=volume (2, 2, 2);
}

function volumeX2 (vol, x2) {
  return vol * x2;
}

function showLog () {
console.log("Volume times 2 = " + volumeX2 (vol, 2));
}

</script>
</body>
</html>
or pass it directly to the next function, keeping it local (and so inaccessible to other functions):

Code:
<html>
<head>
</head>

<body>
<input type="button" value="calculate" onclick="checkVal()"/>
<script>

function volume (l, w, h) {
return l * w * h;
}

function checkVal(){
vol=volume (2, 2, 2);
showLog (vol)
}

function volumeX2 (vol, x2) {
  return vol * x2;
}

function showLog (vol) {
console.log("Volume times 2 = " + volumeX2 (vol, 2));
}

</script>
</body>
</html>
xelawho is offline   Reply With Quote
Old 01-06-2012, 06:41 PM   PM User | #5
acolod
New to the CF scene

 
Join Date: Jan 2012
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
acolod is an unknown quantity at this point
Alright thanks, I think I understand all of that. I proceeded by defining it globally as you suggested first. The only problem I am having now is that it is returning NaN. If checkVal() is defining vol as the result of the volume funtion, and volumeX2 is multiplying vol by 2, then why would it say that is isn't a number?
acolod is offline   Reply With Quote
Old 01-06-2012, 07:00 PM   PM User | #6
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
can we see your code?
xelawho is offline   Reply With Quote
Old 01-06-2012, 07:09 PM   PM User | #7
acolod
New to the CF scene

 
Join Date: Jan 2012
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
acolod is an unknown quantity at this point
Sure:

Code:
var vol;

function volume (l, w, h) {
  return l * w * h;
}
 
console.log("Volume = " + volume (2, 2, 2));
 
function checkVal(){
vol = volume (2, 2, 2);
alert(vol)
}
 
function volumeX2 (vol, x2) {
  return vol * x2;
}

console.log("Volume * 2 = " + volumeX2 (vol, 2));
This is what I get when I run it:

Volume = 8
Volume * 2 = NaN
acolod is offline   Reply With Quote
Old 01-06-2012, 07:18 PM   PM User | #8
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
the console log runs straight away, before val has had a chance to get defined
xelawho is offline   Reply With Quote
Old 01-06-2012, 07:21 PM   PM User | #9
acolod
New to the CF scene

 
Join Date: Jan 2012
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
acolod is an unknown quantity at this point
So is there a way to prioritize functions? Is there a different way I should be going about this?
acolod is offline   Reply With Quote
Old 01-06-2012, 07:30 PM   PM User | #10
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
the console log is just one way of checking what your code is doing. To prevent it from running before you want it to, put it in a function like I did in post#4

or use alerts to check values, or breakpoints in the debugger, or, or...

code outside of a function runs in the order it is placed on the page, as soon as the page loads. Code inside a function is not run until that function has been called
xelawho is offline   Reply With Quote
Users who have thanked xelawho for this post:
acolod (01-06-2012)
Old 01-06-2012, 08:45 PM   PM User | #11
acolod
New to the CF scene

 
Join Date: Jan 2012
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
acolod is an unknown quantity at this point
Alright, its all starting to make a little bit more sense now. I will play around with a few options and see what I come up with. Thank you very much, I really appreciate the you helping me out!
acolod is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:57 PM.


Advertisement
Log in to turn off these ads.