SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2007
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    What's wrong with my script?

    var Homer = {
    "name": "Homer Simpson",
    "age": 34,
    "email": "homersimpson@yahoo.com",
    "job": "gardener"
    };


    function DisplayPropertyNames(obj) {
    var names
    for(var x in obj) names = x + ": " + Homer[x] + "\n";
    document.write(names);
    }

    DisplayPropertyNames(Homer);
    The script supposed to loop through the object Homer, but it didn't, instead only displaying one property of the object.

  2. #2
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    change to

    Code javascript:
    for(var x in obj) names += x + ": " + Homer[x] + "\n";

    You are setting the variable names to the last item in the for loop as your not concatenating.

    The above, will append the current value to the already assigned variable names.


  3. #3
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by alcatraz678 View Post
    The script supposed to loop through the object Homer, but it didn't, instead only displaying one property of the object.
    Try including the document.write statement in the loop.
    Tab-indentation is a crime against humanity.

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    Here is an improved version of the code.

    Code javascript:
    var names = '';
    for (var x in obj) if (obj.hasOwnProperty(x)) {
    	names += x + ": " + Homer[x] + "\n";
    }
    document.write(names);

    The unassigned names variable will default to undefined, so if we make it an empty string then that undefined won't be a part of the final result.

    When using the for...in technique, it's a great practice to make sure that the property is one that actually belongs to the object, rather than being inherited from elsewhere.

    It's also a great practice to use braces wit the for...in loop, as with the previous code it was difficult to tell that it was only one line that related to it.

    Finally, if you're goig to show the content on a webpage, you may want to change the \n for a linebreak, or even surround each line with paragraph marks

    Code javascript:
    	names += '<p>' + x + ': ' + Homer[x] + '</p>\n';
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •