Guest User

Untitled

a guest
Jul 16th, 2018
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. // 20: spread - with-arrays
  2. // To do: make all tests pass, leave the assert lines unchanged!
  3.  
  4. describe('spread with arrays', () => {
  5.  
  6. it('extracts each array item', function() {
  7. //a and b were switched in the assignment
  8. const [a, b] = [...[1, 2]];
  9. assert.equal(a, 1);
  10. assert.equal(b, 2);
  11. });
  12.  
  13. it('in combination with rest', function() {
  14. //Use a leading comma to move over 1 index in the array to properly assign a and b
  15. const [,a, b, ...rest] = [...[0, 1, 2, 3, 4, 5]];
  16. assert.equal(a, 1);
  17. assert.equal(b, 2);
  18. assert.deepEqual(rest, [3, 4, 5]);
  19. });
  20.  
  21. it('spreading into the rest', function() {
  22. //Didn't need the leading comma this time
  23. const [...rest] = [...[1, 2, 3, 4, 5]];
  24. assert.deepEqual(rest, [1, 2, 3, 4, 5]);
  25. });
  26.  
  27. describe('used as function parameter', () => {
  28. it('prefix with `...` to spread as function params', function() {
  29. const magicNumbers = [1, 2];
  30. //Destructure to assign magicA and magicB to the two elements in the array
  31. const fn = ([magicA, magicB]) => {
  32. assert.deepEqual(magicNumbers[0], magicA);
  33. assert.deepEqual(magicNumbers[1], magicB);
  34. };
  35. fn(magicNumbers);
  36. });
  37.  
  38. it('pass an array of numbers to Math.max()', function() {
  39. //Make sure 42 is the largest number in the array so change 43 to 40
  40. const max = Math.max(...[23, 0, 42, 40]);
  41. assert.equal(max, 42);
  42. });
  43. });
  44. });
Add Comment
Please, Sign In to add comment