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 4 of 4
  1. #1
    New Coder
    Join Date
    Sep 2010
    Posts
    67
    Thanks
    9
    Thanked 0 Times in 0 Posts

    noob function help

    Trying to learn JavaScript and i've built a function but the only output i get is:
    What better trade than //? Could someone please tell me where i've gone wrong so it will output: What better employment than brick laying.

    Code:
    var trade = function(brick laying) {
         document.write("What better employment than" + " " + trade);
    }
    
    trade()
    Last edited by Frump; 05-24-2013 at 08:41 PM.

  • #2
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    The "brick laying" in the function is supposed to be an argument. Arguments can be variables (without quotation marks) or strings (with quotation marks, but that would be ridiculous.) You could put quotes around brick laying, and you'd be off to making it work.

    But I'd do something like:
    Code:
    var thisTrade = "brick laying";
    var trade = function(thisTrade){
      document.write("What better employment than " + thisTrade);
    }
    trade();
    I also recommend getting away from ancient things like document.write. Better to give it some HTML, create a div, give it an id that begins with a letter or underscore, and using the DOM to write to it, instead.
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #3
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    @ WolfShade

    That's non-sense and will return "What better employment than undefined".

    If the string you want to output is constant, why not just do

    Code:
    var trade = function() {
        document.write("What better employment than brick laying");
    };
    
    trade();
    But if you do want it to be a function argument, do

    Code:
    var trade = function(employment) {
        document.write("What better employment than " + employment);
    };
    
    trade("brick laying");
    As for document.write being obsolete, WolfShade already said it.

  • Users who have thanked Airblader for this post:

    Frump (05-24-2013)

  • #4
    New Coder
    Join Date
    Sep 2010
    Posts
    67
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Big thanks from one very grateful noob. Cheers!


  •  

    Posting Permissions

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