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