Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 21: spread - with-strings
- // To do: make all tests pass, leave the assert lines unchanged!
- describe('spread with strings', () => {
- it('simply spread each char of a string', function() {
- //Another case of a and b being switched
- const [a, b] = [...'ab'];
- assert.equal(a, 'a');
- assert.equal(b, 'b');
- });
- it('extracts each array item', function() {
- //Use a leading comma to skip over the 'Q' and '...' to break up '12'
- const [,a,b] = ['Q', ...'12'];
- assert.equal(a, 1);
- assert.equal(b, 2);
- });
- it('works anywhere inside an array (must not be last)', function() {
- //Destructure 'bcd' so that all 3 characters are counted separately
- const letters = ['a', ...'bcd', 'e', 'f'];
- assert.equal(letters.length, 6);
- });
- it('dont confuse with the rest operator', function() {
- //Use spread operator to break up '1234'
- const [...rest] = [...'1234', '5'];
- assert.deepEqual(rest, [1, 2, 3, 4, 5]);
- });
- it('passed as function parameter', function() {
- //Cast 12345 to a string so that we can destructure it
- const max = Math.max(...'12345');
- assert.deepEqual(max, 5);
- });
- });
Add Comment
Please, Sign In to add comment