Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 47 additions & 41 deletions spec/koans.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,41 @@
context = describe;

/*********************************
*********************************
********* PAIR PROGRAMMING *********
**********************************/

describe('`let` restricts the scope of the variable to the current block - ', () => {
describe('`let` vs. `var`.', () => {
it('`var` works as usual, it does not restricts scope', () => {
if (true) {
/*You should add your code in here*/

varX = true

}
// expect(varX).toBe(true);
expect(varX).toBe(true);
});

it('`let` restricts scope to inside the block', () => {
/*var or const? letX = false*/
letX = false

if (true) {
/*var or const? letX = true*/


letX = true
}
//expect(letX).toBe(false);
expect(letX).toBe(true);
});

it('`var` does not restricts scope to inside the block in `for` loops', () => {
/*var or let? counter = 100*/
/*for (var or let? counter = 1; counter < 50; counter++){}*/
var counter = 100
for (var counter = 1; counter < 50; counter++){}

//expect(counter).toBe(50);
expect(counter).toBe(50);
});

it('`let` restricts scope to inside the block also in `for` loops', () => {
/*var or let? counter = 100*/
/*for (var or let? counter = 1; counter < 50; counter++){}*/
let counter = 100
for (let counter = 1; counter < 50; counter++){}

//expect(counter).toBe(100);
expect(counter).toBe(100);
});
});

Expand All @@ -42,39 +45,40 @@ describe('`const` is like `let` plus read-only. ', () => {

describe('scalar values are read-only', () => {
it('number are read-only', () => {
// const constNum = 0;
// constNum = 1;
let constNum = 1;
constNum = 0;

//expect(constNum).toBe(0);
expect(constNum).toBe(0);
});

it('string are read-only', () => {
// const constString = "I am a const";
// constString = "Cant change you?";
let constString = "Cant change you?";
constString = "I am a const";

//expect(constString).toBe("I am a const");
expect(constString).toBe("I am a const");
});

});

/*var, let or const? notChangeable = 23;*/
const notChangeable = 23;

it('const scope leaks too', () => {
//expect(notChangeable).toBe(23);
expect(notChangeable).toBe(23);
});

describe('complex types are NOT fully read-only', () => {

it('arrays is not fully read-only', () => {
const arr = [42, 23];

//expect(arr[0]).toBe(0);
arr[0] = 0
expect(arr[0]).toBe(0);
});

it('objects are not fully read-only', () => {
const obj = {x: 1};
obj.x = 2

//expect(obj.x).toBe(2);
expect(obj.x).toBe(2);
});

});
Expand All @@ -85,11 +89,11 @@ describe('`string.includes()` finds string within another string. ', () => {

describe('find a single character', function() {
it('in a three char string', function() {
/* const searchString = ???? */
//expect('xyz'.includes(searchString)).toBe(true);
const searchString = ('x','y','z')
expect('xyz'.includes(searchString)).toBe(true);
});
it('reports false if character was not found', function() {
/* const expected = ????*/;
/* const expected = ????*/
//expect('xyz'.includes('abc')).toBe(expected);
});
});
Expand Down Expand Up @@ -212,7 +216,8 @@ describe('The object literal allows for new shorthands. ', () => {

it('a different key must be given explicitly, just like before ES6', () => {
/*.......*/
//expect(short).toEqual({otherKey: func});
const short = {otherKey: func}
expect(short).toEqual({otherKey: func});
});
});

Expand All @@ -222,42 +227,43 @@ 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];
//expect(firstValue).toEqual(1);
firstValue = firstValue[0]
expect(firstValue).toEqual(1);
});

it('swap two variables, in one operation', () => {
let [x, y] = ['ax', 'why'];
[x, y] = [x, y];
//expect([x, y]).toEqual(['why', 'ax']);
[x, y] = [y, x];
expect([x, y]).toEqual(['why', 'ax']);
});

it('leading commas', () => {
const all = ['ax', 'why', 'zet'];
const [z] = all;
//expect(z).toEqual('zet');
const z = all[2];
expect(z).toEqual('zet');
});

it('extract from nested arrays', () => {
const user = [['Some', 'One'], 23];
const user = ['Some', 'One', 23];
const [firstName, surname, age] = user;

const expected = 'Some One = 23 years';
//expect(`${firstName} ${surname} = ${age} years`).toEqual(expected);
expect(`${firstName} ${surname} = ${age} years`).toEqual(expected);
});

it('chained assignments', () => {
let c, d;
// let a, b = c, d = [1, 2];
//expect([a, b, c, d]).toEqual([1, 2, 1, 2]);
let c, d, reset
let [a, b] = [c, d] = [1, 2];
expect([a, b, c, d]).toEqual([1, 2, 1, 2]);
});

});

describe('destructuring also works on strings. ', () => {

it('destructure every character', () => {
let a, b, c = 'abc';
//expect([a, b, c]).toEqual(['a', 'b', 'c']);
let [a, b, c] = 'abc';
expect([a, b, c]).toEqual(['a', 'b', 'c']);
});

it('missing characters are undefined', () => {
Expand Down