Skip to content

Instantly share code, notes, and snippets.

@underwindfall
Created March 1, 2017 11:12
Show Gist options
  • Save underwindfall/60f1492049a53aaa7af2d4509f85674b to your computer and use it in GitHub Desktop.
Save underwindfall/60f1492049a53aaa7af2d4509f85674b to your computer and use it in GitHub Desktop.
Answers of Es6katas [http://es6katas.org/]
// 10: destructuring - array
// To do: make all tests pass, leave the assert lines unchanged!
describe('destructuring arrays makes shorter code', () => {
it('extract value from array, e.g. extract 0 into x like so `let [x] = [0];`', () => {
let [firstValue] = [1];
assert.strictEqual(firstValue, 1);
});
it('swap two variables, in one operation', () => {
let [x, y] = ['ax', 'why'];
[x, y] = [y, x];
assert.deepEqual([x, y], ['why', 'ax']);
});
it('leading commas', () => {
const all = ['ax', 'why', 'zet'];
const [,,z] = all;
assert.equal(z, 'zet');
});
it('extract from nested arrays', () => {
const user = [['Some', 'One'], 23];
const [[firstName, surname], age] = user;
const expected = 'Some One = 23 years';
assert.equal(`${firstName} ${surname} = ${age} years`, expected);
});
it('chained assignments', () => {
let c, d;
let [a, b] = [c, d] = [1, 2];
assert.deepEqual([a, b, c, d], [1, 2, 1, 2]);
});
it('in for-of loop', () => {
for (var [,a, b] of [[0, 1, 2]]) {}
assert.deepEqual([a, b], [1, 2]);
});
});
@underwindfall
Copy link
Author

Find something bizzare : if I use these code the browser give me a hint i was right about format
it('leading commas', () => {
const all = ['ax', 'why', 'zet'];
const [,...z] = all;
assert.equal(z, ['why','zet']);
});

here is error list:
AssertionError: ["why","zet"] == ["why","zet"]
at n.eval (eval at runSpecs (spec-runner.js:95163:7), :29:12)
at r (http://cdnjs.cloudflare.com/ajax/libs/mocha/3.1.2/mocha.min.js:2:8479)
at r.run (http://cdnjs.cloudflare.com/ajax/libs/mocha/3.1.2/mocha.min.js:2:9550)
at i.runTest (http://cdnjs.cloudflare.com/ajax/libs/mocha/3.1.2/mocha.min.js:2:14329)
at http://cdnjs.cloudflare.com/ajax/libs/mocha/3.1.2/mocha.min.js:2:14969
at r (http://cdnjs.cloudflare.com/ajax/libs/mocha/3.1.2/mocha.min.js:2:13794)
at http://cdnjs.cloudflare.com/ajax/libs/mocha/3.1.2/mocha.min.js:2:13770
at n (http://cdnjs.cloudflare.com/ajax/libs/mocha/3.1.2/mocha.min.js:2:13561)
at http://cdnjs.cloudflare.com/ajax/libs/mocha/3.1.2/mocha.min.js:2:13625
at i (http://cdnjs.cloudflare.com/ajax/libs/mocha/3.1.2/mocha.min.js:1:572)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment