skip to Main Content

for this test, it should works, but i don’t know what’s wrong

i don’t know why , but in webstorm it is working, but on course website no, i guess on website there is other return structure, and i should add this. somewhere, but i don’t know where.

It looks like your post is mostly code; please add some more details. thank you stack overflow, but i want to post my question

it(`Push method should work with many items

input: [1, 2, 3].push(72, 12, 41)`, () => {
    const source = [1, 2, 3];
    source.push2(72, 12, 41);

    expect(source)
      .toEqual([1, 2, 3, 72, 12, 41]);
});

source.push2 = function(...param){

  const parameters = [...param];
  for (const item of parameters) {
    source[source.length] = item;
  }

  return source;
};

2

Answers


  1. You mean something like below

    Array.prototype.push2 = function(...args) {
      for (const arg of args) {
        this[this.length] = arg;
      }
      return this.length;
    };
    
    const myArray = [1, 2, 3];
    
    myArray.push2(72, 12, 41);
    
    Login or Signup to reply.
    1. You should use this
    anotherArray.push2 = source.push2;
    anotherArray.push2(1) // pushes 1 into *source*
    
    1. You should use Array.prototype
    anotherArray2.push2(1) // error: anotherArray2 doesn't have .push2
    Array.prototype.push2 = source.push2
    anotherArray2.push2(1) // ok
    
    1. You should return length
    console.log(['a','b','c'].push('d')) // 4
    console.log(['a','b','c'].push2('d')) // ['a','b','c','d']
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search