Results 1 to 7 of 7

Thread: Javascript Exam question

  1. #1
    Join Date
    Oct 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Javascript Exam question

    I am a complete novice with regard to Javascript and I had an exam where I was asked the following questions. I am done with the exam but I couldn't find a few answers. Can some one help by explaining with answers.

    Thanks!

    Dan :-)


    Question:

    1)Write a one-line piece of JavaScript code that concatenates all strings passed into a function:

    function concatenate(/*any number of strings*/) {
    var string = /*your one line here*/
    return string;
    }


    2) What do these two examples have in common?

    Ex 1:
    var obj = document.getElementById(’adiv’);
    document.getElementById(’adiv’).ptr = obj;

    Ex 2:
    function assignClick() {
    var el = document.createElement(’div’);
    function handleClick() {
    el.innerHTML = ‘clicked!’;
    }
    el.attachEvent(”onclick”, handleClick);
    }

  2. #2
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default

    I know the answer to #1 (so I think):

    I rewrote the function:

    Code:
    function concatenate(/*arguments*/)
    {
    var string = "";
    for(i = 0; i < arguments.length; i++) // either arguments.length or this.arguments.length
    {
    string += arguments[i].value; // I doubt this works - might work
    }
    return string;
    }
    This may work. I haven't tested it, so it's up to you. Were the qustions multiple choiced? If so, what were the choices?

    -magicyte

  3. #3
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Wrong magic, to make it work you would do this:
    Code:
    function concatenate(arguments)
    {
    var string = "";
    for(i = 0; i < arguments.length; i++) // either arguments.length or this.arguments.length
    {
    string += arguments[i]; // I doubt this works - might work
    }
    return string;
    }
    And to call it you would need to make an array:
    Code:
    alert(concatenate(['Yo, ','Im connected']));
    Also, I don't understand that question. But it wants ONE line... So idk.
    On your second question, I'd have to say that there both messing with objects and elements.
    Btw, if you know / can contact the person that made the quiz and ask:
    Why they put one function in another function?
    Last edited by Nile; 10-01-2008 at 12:57 AM.
    Jeremy | jfein.net

  4. #4
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default

    Quote Originally Posted by Nile View Post
    Wrong magic, to make it work you would do this:
    Code:
    function concatenate(arguments)
    {
    var string = "";
    for(i = 0; i < arguments.length; i++) // either arguments.length or this.arguments.length
    {
    string += arguments[i]; // I doubt this works - might work
    }
    return string;
    }
    And to call it you would need to make an array:
    Code:
    alert(concatenate(['Yo, ','I'm connected']));
    With your posted code, that would work. Now I can change my code. Thanks, Nile!

    Here's the code:

    Code:
    function concatenate(txt)
    {
    var string = "";
    for(i = 0; i < txt.length; i++)
    {
    string += " " + txt[i];
    }
    return string;
    }
    
    // Example:
    alert(concatenate(['hi','who']);
    Then again, you can use this code:

    Code:
    function concatenate(/* args */)
    {
    var string = "";
    for(x = 0; x < arguments.length; x++)
    {
    string += " "+arguments[x];
    }
    return string;
    }
    
    // Example:
    alert(concatenate('this','should','work'));
    I'm very sure this will work.

    -magicyte

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    function concatenate() {
      return Array.prototype.join.call(arguments, "");
    }
    The thing both the pieces of code have in common is that they create a circular reference, and therefore a memory leak — in the first, obj -> ptr -> obj, and in the second, el -> closure -> onclick -> el.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Hey, Twey:
    What does ptr do?
    Last edited by Nile; 10-01-2008 at 01:23 PM.
    Jeremy | jfein.net

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    In this code, 'ptr' is a property attached to obj, which refers back to obj. It is arbitrary: it doesn't do anything special.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

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
  •