Skip to content

Commit 0140eca

Browse files
committed
Fix all the things around Elyses Enchantments
1 parent 2e8ffc5 commit 0140eca

File tree

13 files changed

+523
-270
lines changed

13 files changed

+523
-270
lines changed

concepts/arrays/.meta/config.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"blurb": "TODO: add blurb for arrays concept",
3-
"authors": ["ovidiu141"],
4-
"contributors": ["peterchu999", "SleeplessByte"]
2+
"blurb": "Arrays in JavaScript are list-like objects without a fixed length or fixed item type.",
3+
"authors": ["ovidiu141", "SleeplessByte"],
4+
"contributors": ["peterchu999"]
55
}

concepts/arrays/about.md

Lines changed: 118 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# About
22

3-
In Javascript, an array is actually just a regular object where the elements are properties of that object. It includes the `length` property and also lots of [useful methods][array-docs] for traversing and mutating the array.
3+
In Javascript, an array is actually a regular `object` where the elements are properties of that object.
4+
It includes the `length` property and also lots of [useful methods][array-docs] for traversing and mutating the array.
45

56
Declaring an array and accessing an element:
67

@@ -10,4 +11,120 @@ names[1];
1011
// => Laura
1112
```
1213

14+
Arrays cannot use `strings` as element indexes, but must use integers ([`number`][concept-numbers]).
15+
Setting or accessing via non-integers using bracket notation (or dot notation) will not set or retrieve an element from the array list itself, but will set or access a variable associated with that array's object property collection.
16+
The array's object properties and list of array elements are separate, and the array's traversal and mutation operations cannot be applied to these named properties.
17+
18+
```javascript
19+
const names = ['Jack', 'Laura', 'Paul', 'Megan'];
20+
names.length
21+
// => 3
22+
23+
// Properties can be set on arrays using bracket ['property'] or dot .property
24+
// notation, and this will affect the length, as shown below.
25+
26+
names.magician = 'Elyse'
27+
names.length
28+
// => 4
29+
30+
// The property shows up when logging the array, making it seem that the
31+
// property is somehow incorporated in the array.
32+
33+
names
34+
// => ["Jack", "Laura", "Paul", "Megan", magician: "Elyse"]
35+
36+
// However, be aware. Properties added via non-numeric keys are NOT part of the
37+
// array's internal list, and are not traversed or mutated when using one of
38+
// the traversal or mutation operations.
39+
40+
names.forEach((name) => console.log(name))
41+
// => Jack
42+
// => Laura
43+
// => Paul
44+
// => Megan
45+
```
46+
47+
## Deleting items from an array
48+
49+
Arrays in JavaScript are regular `objects`, and items can be deleted using the `delete` keyword.
50+
However, this does not change the _length_ of the array, and leaves a hole of `empty`.
51+
In other languages this is similar to a sparse array.
52+
The `empty` holes are skipped when using traversal or mutation operations.
53+
54+
```javascript
55+
const names = ['Jack', 'Laura', 'Paul', 'Megan'];
56+
delete names[1]
57+
58+
names
59+
// =>  ["Jack", empty, "Paul", "Megan"]
60+
61+
names.length
62+
// => 4
63+
64+
names.forEach((name) => console.log(name))
65+
// => Jack
66+
// => Paul
67+
// => Megan
68+
```
69+
70+
If there should be no holes, and if the `length` should reflect the amount of items that will be traversed or mutated, use `splice` instead:
71+
72+
```javascript
73+
const names = ['Jack', 'Laura', 'Paul', 'Megan'];
74+
names.splice(1, 1)
75+
76+
names
77+
// =>  ["Jack", "Paul", "Megan"]
78+
79+
names.length
80+
// => 3
81+
82+
names.forEach((name) => console.log(name))
83+
// => Jack
84+
// => Paul
85+
// => Megan
86+
```
87+
88+
## Array length can be mutated
89+
90+
The `length` property of an array is connected to the list of items the array holds.
91+
It can be mutated.
92+
When the length is increased, it creates `empty` holes, that are not considered when traversing or mutating the array.
93+
When the length is decreased, it _removes_ the elements at the end of the array.
94+
95+
```javascript
96+
const names = ['Jack', 'Laura', 'Paul', 'Megan'];
97+
names.length = 6
98+
99+
names
100+
// => ["Jack", "Laura", "Paul", "Megan", empty × 2]
101+
102+
names.length = 2
103+
// =>  ["Jack", "Laura"]
104+
```
105+
106+
## Checking is something is an Array
107+
108+
Because arrays are `objects`, `typeof names` gives `"object"`.
109+
To check if something is an Array, use `Array.isArray`:
110+
111+
```javascript
112+
const names = ['Jack', 'Laura', 'Paul', 'Megan'];
113+
114+
typeof names
115+
// => "object"
116+
117+
Array.isArray(names)
118+
// => true
119+
120+
const object = {}
121+
Array.isArray(object)
122+
// => false
123+
```
124+
125+
You might be tempted to use `names instanceof Array`, and that can work, but not under all circumstances.
126+
Read [this article][instanceof-vs-array-is-array] for more information.
127+
13128
[array-docs]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Instance_methods
129+
[concept-numbers]: /tracks/javascript/concepts/numbers
130+
[instanceof-vs-array-is-array]: https://web.mit.edu/jwalden/www/isArray.html

concepts/arrays/introduction.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Introduction
22

3-
In Javascript, an array is a list-like structure with no fixed length which can hold any type of primitives or ojects, even mixed types. The array elements can be accesed by their index.
3+
In Javascript, an array is a list-like structure with no fixed length which can hold any type of primitives or objects, even mixed types.
4+
The array elements can be accessed by their index.
45

56
Example of an array declaration and accessing an element by index:
67

concepts/arrays/links.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[
22
{
33
"url": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Instance_methods",
4-
"description": "array-docs"
4+
"description": "MDN: Arrays"
55
}
66
]

exercises/concept/elyses-enchantments/.docs/hints.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,28 @@
99

1010
- The array is a mutable structure, you can change its content anytime.
1111
- You can find an example [here][change_array_elements_resource], inside the 'Changing an Array Element' section.
12-
1312
## 3. Insert a card at the top of the stack
1413

1514
- There is a [built-in][push_method_docs] method to add a new value to the end of the array.
1615

17-
## 4. Remove a card from the stack
16+
## 4. Insert a card at the bottom of the stack
1817

19-
- There is a [built-in][splice_method_docs] method that, among other use cases, can be used to remove elements starting at a certain position.
18+
- There is a [built-in][unshift_method_docs] method to add a new value to the beginning of the array.
2019

21-
## 5. Remove the top card from the stack
2220

23-
- There is a [built-in][pop_method_docs] method to remove the last element from the array.
21+
## 5. Remove a card from the stack
2422

25-
## 6. Insert a card at the bottom of the stack
23+
- There is a [built-in][splice_method_docs] method that, among other use cases, can be used to remove elements starting at a certain position.
2624

27-
- There is a [built-in][unshift_method_docs] method to add a new value to the beginning of the array.
25+
## 6. Remove the top card from the stack
26+
27+
- There is a [built-in][pop_method_docs] method to remove the last element from the array.
2828

2929
## 7. Remove a card from the bottom of the stack
3030

3131
- There is a [built-in][shift_method_docs] method to remove the first element from the array.
3232

33-
## 8. Check size of the stack
33+
## 8. Check the size of the stack
3434

3535
- Arrays have a [property][length_property_docs] to retrieve their length.
3636

exercises/concept/elyses-enchantments/.docs/instructions.md

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,68 +6,73 @@ To make things a bit easier she only uses the cards 1 to 10.
66

77
## 1. Retrieve a card from a stack
88

9-
Return the card at position `index` from the given stack.
9+
In order to pick a card, return the card at position `position` from the given stack.
1010

1111
```javascript
12-
const index = 2;
13-
getItem([1, 2, 4, 1], index);
12+
const position = 2;
13+
getItem([1, 2, 4, 1], position);
1414
// => 4
1515
```
1616

1717
## 2. Exchange a card in the stack
1818

19-
Exchange the card at position `index` with the new card provided and return the adjusted stack.
20-
Note that this will also change the input slice which is ok.
19+
Perform some sleight of hand and exchange the card at position `position` with the new card provided.
20+
Return the adjusted stack.
2121

2222
```javascript
23-
const index = 2;
23+
const position = 2;
2424
const newCard = 6;
25-
setItem([1, 2, 4, 1], index, newCard);
25+
setItem([1, 2, 4, 1], position, newCard);
2626
// => [1, 2, 6, 1]
2727
```
2828

29-
## 3. Insert a card at the of top the stack
29+
## 3. Insert a card at the top of the stack
3030

31-
Insert new card at the top of the stack and return the stack.
31+
Make a card appear, by inserting a new card at the top of the stack.
32+
Return the adjusted stack.
3233

3334
```javascript
3435
const newCard = 8;
3536
insertItemAtTop([5, 9, 7, 1], newCard);
3637
// => [5, 9, 7, 1, 8]
3738
```
3839

39-
## 4. Remove a card from the stack
40+
## 4. Insert a card at the bottom of the stack
4041

41-
Remove the card at position `index` from the stack and return the stack.
42+
Make a card appear, by inserting a new card at the bottom of the stack.
43+
Return the adjusted stack.
4244

4345
```javascript
44-
const index = 2;
45-
removeItem([3, 2, 6, 4, 8], index);
46-
// => [3, 2, 4, 8]
46+
const newCard = 8;
47+
insertItemAtBottom([5, 9, 7, 1], newCard);
48+
// => [8, 5, 9, 7, 1]
4749
```
4850

49-
## 5. Remove the top card from the stack
51+
## 5. Remove a card from the stack
5052

51-
Remove the card at the top of the stack and return the stack.
53+
Make a card disappear by removing the card the given `position` from the stack.
54+
Return the adjusted stack.
5255

5356
```javascript
54-
removeItemFromTop([3, 2, 6, 4, 8]);
55-
// => [3, 2, 6, 4]
57+
const position = 2;
58+
removeItem([3, 2, 6, 4, 8], position);
59+
// => [3, 2, 4, 8]
5660
```
5761

58-
## 6. Insert a card at the bottom of the stack
62+
## 6. Remove the top card from the stack
5963

60-
Insert new card at the bottom of the stack and return the stack.
64+
Make a card disappear by removing the card at the top of the stack.
65+
Return the adjusted stack.
6166

6267
```javascript
63-
const newCard = 8;
64-
insertItemAtBottom([5, 9, 7, 1], newCard);
65-
// => [8, 5, 9, 7, 1]
68+
removeItemFromTop([3, 2, 6, 4, 8]);
69+
// => [3, 2, 6, 4]
6670
```
6771

6872
## 7. Remove a card from the bottom of the stack
6973

70-
Remove the card at the bottom of the stack and return the stack.
74+
Make a card disappear by removing the card at the bottom of the stack.
75+
Return the adjusted stack.
7176

7277
```javascript
7378
removeItemAtBottom([8, 5, 9, 7, 1]);

exercises/concept/elyses-enchantments/.docs/introduction.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Introduction
22

3-
In Javascript, an array is a list-like structure with no fixed length which can hold any type of primitives or ojects, even mixed types. The array elements can be accesed by their index.
3+
In Javascript, an array is a list-like structure with no fixed length which can hold any type of primitives or objects, even mixed types.
4+
The array elements can be accessed by their index.
45

56
Example of an array declaration and accessing an element by index:
67

exercises/concept/elyses-enchantments/.meta/config.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"blurb": "TODO: add blurb for elyses-enchantments exercise",
3-
"authors": ["ovidiu141"],
4-
"contributors": ["peterchu999", "SleeplessByte"],
2+
"blurb": "Help Elyse with her Enchantments and learn about arrays in the process.",
3+
"authors": ["ovidiu141", "SleeplessByte"],
4+
"contributors": ["peterchu999"],
55
"files": {
66
"solution": ["enchantments.js"],
77
"test": ["enchantments.spec.js"],
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Design
2+
3+
## Learning objectives
4+
5+
## Out of scope
6+
7+
## Concepts
8+
9+
- `arrays`
10+
11+
## Prerequisites
12+
13+
- `numbers` (and `basics`)
14+
15+
## Analyzer
16+
17+
This exercise could benefit from the following rules added to the the [analyzer][analyzer]:
18+
19+
- Verify the simplicity of every method
20+
21+
[analyzer]: https://github.com/exercism/javascript-analyzer

0 commit comments

Comments
 (0)