...

View Full Version : Using the result of a function in next funtion



acolod
01-06-2012, 04:58 PM
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:



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!

xelawho
01-06-2012, 05:16 PM
it's kind of the same as what you are doing - just create a variable to hold the value of the returned function:



<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>

acolod
01-06-2012, 05:29 PM
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:



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!

xelawho
01-06-2012, 05:42 PM
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):


<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):



<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>

acolod
01-06-2012, 06:41 PM
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?

xelawho
01-06-2012, 07:00 PM
can we see your code?

acolod
01-06-2012, 07:09 PM
Sure:


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

xelawho
01-06-2012, 07:18 PM
the console log runs straight away, before val has had a chance to get defined

acolod
01-06-2012, 07:21 PM
So is there a way to prioritize functions? Is there a different way I should be going about this?

xelawho
01-06-2012, 07:30 PM
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

acolod
01-06-2012, 08:45 PM
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!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum