Home > Back-end >  How do I use the spread operator in this function?
How do I use the spread operator in this function?

Time:06-16

This is on my index.js file in VSC

describe('appendCat(name)', function () {
  it('appends a cat to the cats array and returns a new array, leaving the cats array unchanged', function () {
    expect(appendCat("Broom")).to.have.ordered.members(["Milo", "Otis", "Garfield", "Broom"]);

    expect(cats).to.have.ordered.members(["Milo", "Otis", "Garfield"]);
  });
});

I am supposed to append a cat to the cats array leaving the cats array unchanged.

I have no idea how to write the function for this

CodePudding user response:

You mean something like this?

This will spread the cats array, plus add a new element to it named "Broom", but the initial cats array will not be changed.

describe('appendCat(name)', function () {
  it('appends a cat to the cats array and returns a new array, leaving the cats array unchanged', function () {
    expect(appendCat("Broom")).to.have.ordered.members([...cats, "Broom"]);

    expect(cats).to.have.ordered.members(["Milo", "Otis", "Garfield"]);
  });
});

CodePudding user response:

You can do like this

[...cats, "name of cat"]
  • Related