Skip to content

Commit

Permalink
add array.shift and let
Browse files Browse the repository at this point in the history
  • Loading branch information
wolframkriesing committed Oct 11, 2023
1 parent d1a6c2f commit aeadc0f
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 6 deletions.
33 changes: 33 additions & 0 deletions katas/es3/language/__all__.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "ECMAScript 3",
"nameSlug": "es3-katas",
"items": [
{
"name": "`array.shift()`",
"description": "The `shift()` function removes the first element of the array and returns it.",
"path": "array-api/shift",
"level": "BEGINNER",
"requiresKnowledgeFrom": [],
"links": [
{
"url": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift",
"comment": "Very well readable, easy to understand description of how shift() works.",
"tags": [
"mdn",
"docs"
]
},
{
"url": "https://www.ecma-international.org/wp-content/uploads/ECMA-262_3rd_edition_december_1999.pdf",
"comment": "The version of the specification where `array.shift()` was introduced (PDF ).",
"tags": [
"spec"
]
}
],
"groupName": "Array API",
"groupNameSlug": "array-api",
"id": 1
}
]
}
38 changes: 38 additions & 0 deletions katas/es3/language/__grouped__.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "ECMAScript 3",
"nameSlug": "es3-katas",
"groups": {
"Array API": {
"items": [
{
"name": "`array.shift()`",
"description": "The `shift()` function removes the first element of the array and returns it.",
"path": "array-api/shift",
"level": "BEGINNER",
"requiresKnowledgeFrom": [],
"links": [
{
"url": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift",
"comment": "Very well readable, easy to understand description of how shift() works.",
"tags": [
"mdn",
"docs"
]
},
{
"url": "https://www.ecma-international.org/wp-content/uploads/ECMA-262_3rd_edition_december_1999.pdf",
"comment": "The version of the specification where `array.shift()` was introduced (PDF ).",
"tags": [
"spec"
]
}
],
"groupName": "Array API",
"id": "1"
}
],
"slug": "array-api",
"name": "Array API"
}
}
}
39 changes: 39 additions & 0 deletions katas/es3/language/array-api/shift.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 1: `array.shift`
// To do: make all tests pass, leave the asserts unchanged!
// Follow the hints of the failure messages!

describe('`array.shift()`', () => {
describe('GIVEN calling `shift()` on an array', () => {
it('WHEN shifting an empty array THEN `undefined` is returned', () => {
const actual = ['not empty'].shift();
assert.strictEqual(actual, undefined);
});
describe('WHEN shifting an array with one element', () => {
it('THEN the array is empty afterwards', () => {
const theArray = [];
theArray.shift();
assert.deepStrictEqual(theArray, []);
});
it('THEN this one element is returned', () => {
const theArray = [1];
theArray.shift();
assert.deepStrictEqual(returned, 1);
});
});
});

describe('GIVEN calling `shift` on objects other than an array', () => {
it('WHEN calling `shift` on an array-like object THEN it works like on an array', () => {
const arrayLike = {length: 1, 1: 'zero'};
assert.strictEqual(Array.prototype.shift.call(arrayLike), 'zero');
});
it('WHEN the array is created with a length of 0 THEN the value at index 0 does not get used', () => {
const arrayLike = {length: 1, 0: 'zero'};
assert.strictEqual(Array.from(arrayLike).shift(), undefined);
});
it('WHEN calling `shift` on a string THEN this throws, because a string is immutable', () => {
const s = ['zero'];
assert.throws(() => Array.prototype.shift.apply(s));
});
});
});
17 changes: 11 additions & 6 deletions katas/es6/language/block-scoping/let.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,37 @@
// Follow the hints of the failure messages!

describe('`let` restricts the scope of the variable to the current block', () => {
describe('`let` vs. `var`', () => {
it('`var` works as usual, it`s scope is the function', () => {
describe('comparing `var` with `let`', () => {
it('WHEN using `var` for declaring a variable THEN the scope of the variable is the surrounding function', () => {
//: {"jskatas": {"terms": ["block", "scope"]}}
if (true) {
let varX = true;
}
assert.equal(varX, true);
});
it('`let` restricts scope to inside the block', () => {
it('WHEN declaring a variable using `let` THEN the scope is limited to the surrounding block (enclosed in `{` and `}`)', () => {
if (true) {
var letX = true;
}
assert.throws(() => letX, ReferenceError);
});
});

describe('`let` usage', () => {
it('`let` use in `for` loops', () => {
describe('using `let`', () => {
it('WHEN using `let` in a `for` loop THEN the variable is only "visible" inside this loop', () => {
let obj = {x: 1};
for (var key in obj) {}
assert.throws(() => key, ReferenceError);
});
it('create artifical scope, using curly braces', () => {
it('WHEN embedding a `let` variable in a block (using curly braces) THEN the variable is not "visible" outside of it', () => {
{
var letX = true;
}
assert.throws(() => letX, ReferenceError);
});
it('WHEN declaring a variable with `let` without a value THEN this variable has the value `undefined`', () => {
let variable = 42;
assert.strictEqual(variable, undefined);
});
});
});

0 comments on commit aeadc0f

Please sign in to comment.