Why doesn't this work?

Tagged: 

Viewing 9 reply threads
  • Author
    Posts
  • September 6, 2019 at 9:26 AM #36694

    D J Leason
    Participant

    Trying to learn some Javascript so I can use the script field. I replaced the Hello World code with code from a W3Schools tutorial but it doesn’t work. What am I doing wrong?

    Attachments:
    You must be logged in to view attached files.
    September 6, 2019 at 2:09 PM #36696

    john cesta
    Participant

    Why var = x

    Then x is nowhere

    September 6, 2019 at 2:13 PM #36697

    Daniel Leu
    Participant

    You have to define your function outside of Script():

    function myFunction(a,b){
       return a * b;
    }
    
    function Script(){
       var x = myFunction(4, 3);
       return x;
    }
    
    Script();
    September 6, 2019 at 3:33 PM #36699

    D J Leason
    Participant

    @John cesta

    Because x is the variable where the result 12 will be stored.Also I can change the arguments there. That’s what I got from it anyway. I’m new to JS and I copy and pasted it exactly as it was in the tutorial where it worked fine.

    When JavaScript reaches a return statement, the function will stop executing.
    If the function was invoked from a statement, JavaScript will “return” to execute the code after the invoking statement.
    Functions often compute a return value. The return value is “returned” back to the “caller”:
    Example
    Calculate the product of two numbers, and return the result:

    var x = myFunction(4, 3);   // Function is called, return value will end up in x

    function myFunction(a, b) {
      return a * b;             // Function returns the product of a and b
    }
    The result in x will be:
    12

    September 6, 2019 at 4:06 PM #36703

    Brendan
    Keymaster

    Hi DJ,

    Daniel is correct in his sample. However, you may be able to just add return x; after your second last } but before the last }. Basically your function is returning x, but you’re not returning the result of x to the Script() function.

    September 7, 2019 at 8:47 AM #36705

    D J Leason
    Participant

    @Brendan

    Thank you Brendan! That works! I went back to the tutorial and ran it again and in their ‘Try it yourself’ editor. It runs in an HTML doc type and has the following line to get x:

    document.getElementById(“demo”).innerHTML = x;

    <!DOCTYPE html>
    <html>
    <body>

    <h2>JavaScript Functions</h2>

    <p>This example calls a function which performs a calculation and returns the result:</p>

    <p id=”demo”></p>

    var x = myFunction(4, 3);
    document.getElementById(“demo”).innerHTML = x;

    function myFunction(a, b) {
    return a * b;
    }

    </body>
    </html>

    September 7, 2019 at 8:57 AM #36706

    D J Leason
    Participant

    @ Daniel Leu

    Thanks again Daniel, I like your way as well. It makes sense.

    September 7, 2019 at 3:41 PM #36707

    Brendan
    Keymaster

    Daniel’s example is a much better way of structuring your calls in my opinion. I was just providing input to show you how your current nested structure could work.

    September 7, 2019 at 3:51 PM #36709

    D J Leason
    Participant

    @ Brendan

    I agree! I wish I was following his JavaScript tutorial!
    You hear that @ Daniel Leu?

    September 8, 2019 at 12:15 AM #36712

    Daniel Leu
    Participant

    :-)

Viewing 9 reply threads

You must be logged in to reply to this topic.