SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Member
    Join Date
    Feb 2008
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PLEASE HELP! VERY EASY! [Functions, variables, etc.]

    This is an assignment for school, and I need to do the following to the code below:



    Modify the getCompanyName() function in the following code so that it returns the company name to another calling function. Also, add statements after the getCompanyName() function definition that call the getCompanyName function and assign the return value to a variable named retValue. Finally, print the contents of the retValue variable.





    Here is the code:
    <HTML>
    <HEAD>
    <TITLE>Company Name</TITLE>
    <PRE>
    <SCRIPT type="text/javascript">
    <!-- HIDE FROM INCOMPATIBLE BROWSERS
    function getCompanyName() {
    var companyName = "Course Technology";
    }
    // STOP HIDING FROM INCOMPATIBLE BROWSERS -->
    </SCRIPT>
    </PRE>
    </HEAD>
    <BODY>
    </BODY>
    </HTML>






    This will probably be easy for most of you... but I just started this stuff today.

    Any help would be greatly appreciated because this is due tomorrow.

    Thanks, Spyder

  2. #2
    SitePoint Evangelist
    Join Date
    Oct 2005
    Location
    Michigan, USA
    Posts
    434
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    If you started today and it's due tomorrow, there must have been something in the lesson about it.
    - Robert

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,696
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Spyder23 View Post
    This is an assignment for school
    Well normally I wouldn't do homework for others, but let's see what I can do here.

    Quote Originally Posted by Spyder23 View Post
    Modify the getCompanyName() function in the following code so that it returns the company name to another calling function. Also, add statements after the getCompanyName() function definition that call the getCompanyName function and assign the return value to a variable named retValue. Finally, print the contents of the retValue variable.
    Code Javascript:
    /**
     * This program is designed to demonstrate the ability of one
     * function to call another function, and then display the end
     * result at the end of the existing content on the page.
     *
     * The global namespace of the browser is protected by wrapping
     * the code inside a function literal. This limits the scope of 
     * functions and variables to within the function literal itself.
     * It is still possible to create global variables by defining them
     * without the "var" keyword, but properly written code is kept
     * much safer with this function literal syntax than without.
     *
     * A function literal is created by defining an anonymous function
     * to contain the code, and then executing the function after
     * it has been defined. The anonymous function is wrapped inside
     * parenthesis in order to allow the function to be executed at the
     * same time as it has been defined.
     */
     
    // Function literal in order to protect the global namespace
    (function () { 
     
        // getCompanyName: This function creates a variable that
        //     contains the name of the company. This name could
        //     be anything, and is defined inside the double quotes.
        //     It then returns the defined value at the end of the
        //     function in order to return the company name back
        //     to the statement that calls this function.
        function getCompanyName() {
            var companyName = "Course Technology";
            // add statement to return the value here
        }
     
        // This next line creates a new variable and assigns the
        // result of the function that it calls to be the value of
        // the variable. This will be used later on as the variable
        // that is printed out.
        // Add assignment of function return value here
     
        // print: This function prints out the passed value to the
        //     current window document. It displays the value at
        //     the bottom of the current page by adding an element
        //     to the bottom of the document body section.
        //     The element that is added is a paragraph element which
        //     has the value converted to a text node. This allows the
        //     value to be placed inside the paragraph so that it can
        //     then be displayed on to the screen.
        function print(value) {
            var para = document.createElement('p');
            var text = document.createTextNode(value);
            para.appendChild(text);
            document.body.appendChild(para);
        }
     
        // This next line prints the company name. It uses the print()
        // function and passes to it the value that was returned to
        // it from the getCompanyName() function from earlier in the
        // code.
        print(returnedValue);
    })();
    Last edited by paul_wilkins; Feb 14, 2008 at 00:24.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    Guru in training bronze trophy SoulScratch's Avatar
    Join Date
    Apr 2006
    Location
    Maryland
    Posts
    1,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <script type="text/javascript">
    function getCompanyName() {
    var companyName = 'Course Technology';
    return companyName;
    }

    alert(getCompanyName());

    var retValue = getCompanyName();
    alert(retValue);

    </script>
    Here's my take on it... I didn't use a self-ex. anon function because I like to call functions via Firebug and if I encapsulated them I dont think I'll have access to them.. when they're ready for production I could wrap everything around though.

  5. #5
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <!-- HIDE FROM INCOMPATIBLE BROWSERS
    // STOP HIDING FROM INCOMPATIBLE BROWSERS -->
    That is not needed. There are no incompatible browsers still in use.
    Also, you should write all of your HTML tags in lowercase.

  6. #6
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Remove the <pre> element (doesn't belong in the <head>). Start with the basic markup:
    Code HTML4Strict:
    <html>
    <head>
    <title>Company Name</title>
    <script type="text/javascript">
    </script>
    </head>
    <body>
    </body>
    </html>
    And here's the script (put this in the script element):
    Code JavaScript:
    function getCompanyName() {
        var companyName = "Course Technology";
        // return the company name
        return companyName;
    }
     
    // call the getCompanyName function and assign the return 
    // value to a variable named retValue
    var retValue = getCompanyName();
     
    // return the company name to another calling function
    function testCompanyName() {
        alert(getCompanyName());
    }
    testCompanyName();
     
    // Finally, print the contents of the retValue variable.
    window.onload = function() {
        document.body.appendChild(document.createTextNode(retValue));
    }

    Note, the last bit on printing the contents is *probably* not how they're teaching you. They're probably teaching you some crap using document.write. It also pisses me off that most of the stuff they teach is so obsolete and old school that it's not even funny. Oh well. In any case, this is as old school as I'm willing to go.

  7. #7
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,696
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    I should say that my script isn't complete yet, because you should always check parameter values to ensure that they are the proper types and within an appropriate range.

    Code Javascript:
    function print(value) {
        if (typeof value !=== 'string') {
            throw new Error('print() asked to print a non-string value');
            return;
        }
        // Remove whitespace from the start and end of the value;
        value = value.replace(/^\s+|\s+$/g,"");
        if (value.length === 0) {
            throw new Error('print() asked to print an empty value');
            return;
        }
        if (!document.createElement) {
            // Using an ancient browser so fail silently
            return;
        }
        var para = document.createElement('p');
        var text = document.createTextNode(value);
        para.appendChild(text);
        document.body.appendChild(para);
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  8. #8
    SitePoint Member
    Join Date
    Feb 2008
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you everyone... I got everything as needed!

    Woooo

  9. #9
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,696
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Fotiman View Post
    That is not needed. There are no incompatible browsers still in use.
    Also, you should write all of your HTML tags in lowercase.
    That incompatible browser is Netscape 1. Because Netscape 1 doesn't support scripts, the comment hack prevents the browser from showing the scripted code as content on the page.

    One day I'll take these old browsers and work out how to most properly write javascript to support all levels of scripting on them.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  10. #10
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pmw57 View Post
    That incompatible browser is Netscape 1. Because Netscape 1 doesn't support scripts, the comment hack prevents the browser from showing the scripted code as content on the page.
    Exactly... like I said, that browser is no longer in use and hasn't been for some time. It's a waste to use those comment hacks.

  11. #11
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,696
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Fotiman View Post
    Exactly... like I said, that browser is no longer in use and hasn't been for some time. It's a waste to use those comment hacks.
    You do still see one or two people using Netscape 1, perhaps they're machochistic but it's still out there.
    So even though it's not being used much, it is still in use. You should use the comment hack if you intent to support Netscape 1.

    Most people decide not to support Netscape 1, nor anything below IE 5.5 or even IE6 nowdays. By using progressive enhancement though, even browsers like Netscape 1 can be supported.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  12. #12
    Guru in training bronze trophy SoulScratch's Avatar
    Join Date
    Apr 2006
    Location
    Maryland
    Posts
    1,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow. Supporting Netscape 1? Give me a break. Think about how badly every other page on the internet looks in Netscape 1 as of now. I think that's taking graceful degredation a little TOOOOOOOOOOOOOOOOOOOOOOOOOO FAR. Hell, I bet the Netscape homepage breaks in Netscape 1.

  13. #13
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pmw57 View Post
    You do still see one or two people using Netscape 1, perhaps they're machochistic but it's still out there.
    So even though it's not being used much, it is still in use.
    Right, there are still appoximately 3 people in the entire world that still use Netscape 1. So you can add that code bloat on the off chance that one of those 3 people might visit your site someday, OR you can save a few bytes and cater to the (likely) 100% of your normal visitors. The choice is yours.

  14. #14
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,696
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by SoulScratch View Post
    Hell, I bet the Netscape homepage breaks in Netscape 1.
    I'm trying it out now. There is some embedded code that's visible, but things still work.

    Of course, if the code is stored in an external .js file the comment hack isn't required at all.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  15. #15
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pmw57 View Post
    Of course, if the code is stored in an external .js file the comment hack isn't required at all.
    An excellent point (which I had meant to make in my last post, but forgot).


Bookmarks

Posting Permissions

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