www.webdeveloper.com
Results 1 to 9 of 9

Thread: How can i get additional parameter sent by apply

  1. #1
    Join Date
    Feb 2013
    Posts
    21

    How can i get additional parameter sent by apply

    I have the following function which can take an array as the argument.

    Code:
    prependArray : function(orig){
    
    }
    Now i have some test cases that pass a second parameter (which is a random number of values in an array) to the function using apply.
    How do I access and prepend the array values into my orig array?

  2. #2
    Join Date
    May 2014
    Posts
    912
    Arrays, just like objects, are passed by reference in .js -- so just access it normally.
    Java is to JavaScript as Ham is to Hamburger.

  3. #3
    Join Date
    Feb 2013
    Posts
    21
    The problem is i cant use more than one argument in the function definition. The test case says -

    Code:
    prependArray.apply(originalArray, addArray);
    and the function definition has to be with one argument. How could i access addArray in my function prependArray?

  4. #4
    Join Date
    May 2014
    Posts
    912
    That' doesn't make any sense and/or is the type of arbitrary 'requirements' that... oh... classwork by a teacher who has no business teaching. Gotcha.

    Can you explain what it is you're REALLY trying to do, as, well... what you've said so far doesn't make sense... or wait, do you mean... no... if it was a complex object you'd not be able to reference 'this'...

    Not sure what you are even asking. Your first example shows that it's a object method, where are you expecting the second value to come from, the parent? If so what's it even called ON the parent object?
    Java is to JavaScript as Ham is to Hamburger.

  5. #5
    Join Date
    Feb 2013
    Posts
    21
    Actually i have the testcases file and i need to write the JS methods to make them pass. So the test case is -

    Code:
    assert.equal(prependArray.length, 1, 'prependArray should expect an array as only formal parameter');
    original = [4, 5];
    addArr = randomArray();
    result = prependArray.apply(original, $.merge([original], addArr.slice(0)));
    
    assert.equal(original.length, addArr.length + 2, 'prependArray should add all arguments past the first one to the array');
    assert.equal(original[0], addArr[0], 'the new values should have been added at the front of the array');
    assert.equal(original[1], addArr[1], 'the new values should have been added at the front of the array');
    assert.equal(original.join(','), addArr.join(',') + ',1,2', 'the other values in the array should not be changed');
    When i do a console.log for the parameter passed i get [4, 5] as the output. I am running out of ideas how to make this case pass.

  6. #6
    Join Date
    May 2014
    Posts
    912
    Wow... you surprised me... and that's a very special gift as this doesn't happen to me very often.

    I have NO idea what that's supposed to accomplish. Probably because I don't know what 'assert', 'prependArray' or 'randomArray' are/do, what the data is you are processing, etc, etc..

    It LOOKS like something needlessly convoluted, but without knowing what it's processing or what it's supposed to accomplish, it's gibberish.
    Java is to JavaScript as Ham is to Hamburger.

  7. #7
    Join Date
    Feb 2013
    Posts
    21
    i think this line is giving me errors -
    Code:
    prependArray.apply(original, $.merge([original], addArr.slice(0)));
    as according to an earlier line i can have only one argument in my prependArray function. Here a second argument is passed. So i don't know how to access the second passed parameter which in this case is the merged array.
    I just need a way to access the merged array.

    Code:
    prependArray : function(originalArray) {
                         for(var i=0; i<addArr.length; i++){
    				originalArray.unshift(addArr[i]);
    			}
    This code gives me an error saying addArr not defined.

  8. #8
    Join Date
    May 2014
    Posts
    912
    Well, what do you want it to DO with the second and third parameters? (you're passing THREE total) -- Where is addArr even coming from?

    As it is, you're brute forcing something you don't have to...

    originalArray = addArr.concat(originalArray);

    functionally identical to your for/unshift loop.
    Java is to JavaScript as Ham is to Hamburger.

  9. #9
    Join Date
    Feb 2013
    Posts
    21
    i found a way to do it. The arguments keyword within a functions gives all the arguments passed - even the hidden arguments as in my case.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles