Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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!

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,980
    Thanks
    56
    Thanked 557 Times in 554 Posts
    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>

  • #3
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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!

  • #4
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,980
    Thanks
    56
    Thanked 557 Times in 554 Posts
    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>

  • #5
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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?

  • #6
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,980
    Thanks
    56
    Thanked 557 Times in 554 Posts
    can we see your code?

  • #7
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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

  • #8
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,980
    Thanks
    56
    Thanked 557 Times in 554 Posts
    the console log runs straight away, before val has had a chance to get defined

  • #9
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    So is there a way to prioritize functions? Is there a different way I should be going about this?

  • #10
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,980
    Thanks
    56
    Thanked 557 Times in 554 Posts
    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

  • Users who have thanked xelawho for this post:

    acolod (01-06-2012)

  • #11
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •