Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 20: spread - with-arrays
- // To do: make all tests pass, leave the assert lines unchanged!
- describe('spread with arrays', () => {
- it('extracts each array item', function() {
- //a and b were switched in the assignment
- const [a, b] = [...[1, 2]];
- assert.equal(a, 1);
- assert.equal(b, 2);
- });
- it('in combination with rest', function() {
- //Use a leading comma to move over 1 index in the array to properly assign a and b
- const [,a, b, ...rest] = [...[0, 1, 2, 3, 4, 5]];
- assert.equal(a, 1);
- assert.equal(b, 2);
- assert.deepEqual(rest, [3, 4, 5]);
- });
- it('spreading into the rest', function() {
- //Didn't need the leading comma this time
- const [...rest] = [...[1, 2, 3, 4, 5]];
- assert.deepEqual(rest, [1, 2, 3, 4, 5]);
- });
- describe('used as function parameter', () => {
- it('prefix with `...` to spread as function params', function() {
- const magicNumbers = [1, 2];
- //Destructure to assign magicA and magicB to the two elements in the array
- const fn = ([magicA, magicB]) => {
- assert.deepEqual(magicNumbers[0], magicA);
- assert.deepEqual(magicNumbers[1], magicB);
- };
- fn(magicNumbers);
- });
- it('pass an array of numbers to Math.max()', function() {
- //Make sure 42 is the largest number in the array so change 43 to 40
- const max = Math.max(...[23, 0, 42, 40]);
- assert.equal(max, 42);
- });
- });
- });
Add Comment
Please, Sign In to add comment