-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
464818c
commit 2d1076e
Showing
12 changed files
with
813 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"root": true, | ||
"extends": "@exercism/eslint-config-javascript", | ||
"env": { | ||
"jest": true | ||
}, | ||
"overrides": [ | ||
{ | ||
"files": [".meta/proof.ci.js", ".meta/exemplar.js", "*.spec.js"], | ||
"excludedFiles": ["custom.spec.js"], | ||
"extends": "@exercism/eslint-config-javascript/maintainers" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/node_modules | ||
/bin/configlet | ||
/bin/configlet.exe | ||
/pnpm-lock.yaml | ||
/yarn.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
audit=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# Help | ||
|
||
## Running the tests | ||
|
||
## Setup | ||
|
||
Go through the setup [instructions for JavaScript][docs-exercism-javascript] to install the necessary dependencies. | ||
|
||
## Requirements | ||
|
||
Install assignment dependencies: | ||
|
||
```shell | ||
# Using npm | ||
npm install | ||
|
||
# Alternatively using yarn | ||
yarn | ||
``` | ||
|
||
## Making the test suite pass | ||
|
||
All exercises come with a test suite to help you validate your solution before submitting. | ||
You can execute these tests by opening a command prompt in the exercise's directory, and then running: | ||
|
||
```bash | ||
# Using npm | ||
npm test | ||
|
||
# Alternatively using yarn | ||
yarn test | ||
``` | ||
|
||
In some test suites all tests but the first have been skipped. | ||
|
||
Once you get a test passing, you can enable the next one by changing `xtest` to `test`. | ||
|
||
## Writing custom tests | ||
|
||
If you wish to write additional, custom, tests, create a new file `custom.spec.js`, and submit it with your solution together with the new file: | ||
|
||
```shell | ||
exercism submit numbers.js custom.spec.js | ||
``` | ||
|
||
[docs-exercism-javascript]: https://exercism.org/docs/tracks/javascript/installation | ||
|
||
## Submitting your solution | ||
|
||
You can submit your solution using the `exercism submit enchantments.js` command. | ||
This command will upload your solution to the Exercism website and print the solution page's URL. | ||
|
||
It's possible to submit an incomplete solution which allows you to: | ||
|
||
- See how others have completed the exercise | ||
- Request help from a mentor | ||
|
||
## Need to get help? | ||
|
||
If you'd like help solving the exercise, check the following pages: | ||
|
||
- The [JavaScript track's documentation](https://exercism.org/docs/tracks/javascript) | ||
- The [JavaScript track's programming category on the forum](https://forum.exercism.org/c/programming/javascript) | ||
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5) | ||
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs) | ||
|
||
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring. | ||
|
||
To get help if you're having trouble, you can use one of the following resources: | ||
|
||
- [/r/javascript](https://www.reddit.com/r/javascript) is the Javascript subreddit. | ||
- [StackOverflow](https://stackoverflow.com/questions/tagged/javascript+exercism) can be used to search for your problem and see if it has been answered already. You can also ask and answer questions. | ||
- [Github issue tracker](https://github.com/exercism/javascript/issues) is where we track our development and maintainance of Javascript exercises in exercism. But if none of the above links help you, feel free to post an issue here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Hints | ||
|
||
## 1. Retrieve a card from a stack | ||
|
||
- Array indices start at `0`. | ||
- [This page][access_array_elements_resource] has more information on how to access array elements. | ||
|
||
## 2. Exchange a card in the stack | ||
|
||
- The array is a mutable structure, you can change its content anytime. | ||
- You can find an example [here][change_array_elements_resource], inside the 'Changing an Array Element' section. | ||
|
||
## 3. Insert a card at the top of the stack | ||
|
||
- There is a [built-in][push_method_docs] method to add a new value to the end of the array. | ||
|
||
## 4. Remove a card from the stack | ||
|
||
- 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. | ||
|
||
## 5. Remove the top card from the stack | ||
|
||
- There is a [built-in][pop_method_docs] method to remove the last element from the array. | ||
|
||
## 6. Insert a card at the bottom of the stack | ||
|
||
- There is a [built-in][unshift_method_docs] method to add a new value to the beginning of the array. | ||
|
||
## 7. Remove a card from the bottom of the stack | ||
|
||
- There is a [built-in][shift_method_docs] method to remove the first element from the array. | ||
|
||
## 8. Check the size of the stack | ||
|
||
- Arrays have a [property][length_property_docs] to retrieve their length. | ||
|
||
[access_array_elements_resource]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Accessing_array_elements | ||
[change_array_elements_resource]: https://www.w3schools.com/js/js_arrays.asp | ||
[push_method_docs]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push | ||
[splice_method_docs]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice | ||
[pop_method_docs]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop | ||
[unshift_method_docs]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift | ||
[shift_method_docs]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift | ||
[length_property_docs]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/length |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2021 Exercism | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,228 @@ | ||
# Elyses Enchantments | ||
|
||
Welcome to Elyses Enchantments on Exercism's JavaScript Track. | ||
If you need help running the tests or submitting your code, check out `HELP.md`. | ||
If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :) | ||
|
||
## Introduction | ||
|
||
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. | ||
|
||
To create an array, add elements between square brackets `[]`. | ||
To read from the array, put the index in square brackets `[]` after the identifier. | ||
The indices of an array start at zero. | ||
|
||
For example: | ||
|
||
```javascript | ||
const numbers = [1, 'two', 3, 'four']; | ||
numbers[2]; | ||
// => 3 | ||
``` | ||
|
||
To retrieve the number of elements that are in an array, use the `length` property: | ||
|
||
```javascript | ||
const numbers = [1, 'two', 3, 'four']; | ||
numbers.length; | ||
// => 4 | ||
``` | ||
|
||
To change an element in the array, you assign a value at the index: | ||
|
||
```javascript | ||
const numbers = [1, 'two', 3, 'four']; | ||
numbers[0] = 'one'; | ||
numbers; | ||
// => ['one', 'two', 3, 'four'] | ||
``` | ||
|
||
## Methods | ||
|
||
Some of the [methods][array_methods] that are available on every Array object can be used to add or remove from the array. | ||
Here are a few to consider when working on this exercise: | ||
|
||
### push | ||
|
||
> The `push()` method adds one or more elements to the end of an array and returns the new length of the array.[^1] | ||
```javascript | ||
const numbers = [1, 'two', 3, 'four']; | ||
numbers.push(5); // => 5 | ||
numbers; | ||
// => [1, 'two', 3, 'four', 5] | ||
``` | ||
|
||
### pop | ||
|
||
> The `pop()` method removes the last element from an array and returns that element. | ||
> This method changes the length of the array.[^2] | ||
```javascript | ||
const numbers = [1, 'two', 3, 'four']; | ||
numbers.pop(); // => four | ||
numbers; | ||
// => [1, 'two', 3] | ||
``` | ||
|
||
### shift | ||
|
||
> The `shift()` method removes the first element from an array and returns that removed element. | ||
> This method changes the length of the array.[^3] | ||
```javascript | ||
const numbers = [1, 'two', 3, 'four']; | ||
numbers.shift(); // => 1 | ||
numbers; | ||
// => ['two', 3, 'four'] | ||
``` | ||
|
||
### unshift | ||
|
||
> The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.[^4] | ||
```javascript | ||
const numbers = [1, 'two', 3, 'four']; | ||
numbers.unshift('one'); // => 5 | ||
numbers; | ||
// => ['one', 1, 'two', 3, 'four'] | ||
``` | ||
|
||
### splice | ||
|
||
> The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place. | ||
> This method returns an array containing the deleted elements.[^5] | ||
```javascript | ||
const numbers = [1, 'two', 3, 'four']; | ||
numbers.splice(2, 1, 'one'); // => [3] | ||
numbers; | ||
// => [1, 'two', 'one', 'four'] | ||
``` | ||
|
||
--- | ||
|
||
[^1]: `push`, MDN. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push (referenced September 29, 2021) | ||
[^2]: `pop`, MDN. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop (referenced September 29, 2021) | ||
[^3]: `shift`, MDN. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift (referenced September 29, 2021) | ||
[^4]: `unshift`, MDN. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift (referenced September 29, 2021) | ||
[^5]: `splice`, MDN. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice (referenced September 29, 2021) | ||
|
||
[array_methods]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array | ||
|
||
## Instructions | ||
|
||
As a magician-to-be, Elyse needs to practice some basics. She has | ||
a stack of cards that she wants to manipulate. | ||
|
||
To make things a bit easier she only uses the cards 1 to 10 so her | ||
stack of cards can be represented by an array of numbers. The position | ||
of a certain card corresponds to the index in the array. That means | ||
position 0 refers to the first card, position 1 to the second card | ||
etc. | ||
|
||
<!-- prettier-ignore-start --> | ||
~~~~exercism/note | ||
All functions should update the array of cards and then return the modified array - a common way of working known as the Builder pattern, which allows you to nicely daisy-chain functions together. | ||
~~~~ | ||
<!-- prettier-ignore-end --> | ||
|
||
## 1. Retrieve a card from a stack | ||
|
||
To pick a card, return the card at index `position` from | ||
the given stack. | ||
|
||
```javascript | ||
const position = 2; | ||
getItem([1, 2, 4, 1], position); | ||
// => 4 | ||
``` | ||
|
||
## 2. Exchange a card in the stack | ||
|
||
Perform some sleight of hand and exchange the card at index `position` | ||
with the replacement card provided. | ||
Return the adjusted stack. | ||
|
||
```javascript | ||
const position = 2; | ||
const replacementCard = 6; | ||
setItem([1, 2, 4, 1], position, replacementCard); | ||
// => [1, 2, 6, 1] | ||
``` | ||
|
||
## 3. Insert a card at the top of the stack | ||
|
||
Make a card appear by inserting a new card at the top of the stack. | ||
Return the adjusted stack. | ||
|
||
```javascript | ||
const newCard = 8; | ||
insertItemAtTop([5, 9, 7, 1], newCard); | ||
// => [5, 9, 7, 1, 8] | ||
``` | ||
|
||
## 4. Remove a card from the stack | ||
|
||
Make a card disappear by removing the card at the given `position` from the stack. | ||
Return the adjusted stack. | ||
|
||
```javascript | ||
const position = 2; | ||
removeItem([3, 2, 6, 4, 8], position); | ||
// => [3, 2, 4, 8] | ||
``` | ||
|
||
## 5. Remove the top card from the stack | ||
|
||
Make a card disappear by removing the card at the top of the stack. | ||
Return the adjusted stack. | ||
|
||
```javascript | ||
removeItemFromTop([3, 2, 6, 4, 8]); | ||
// => [3, 2, 6, 4] | ||
``` | ||
|
||
## 6. Insert a card at the bottom of the stack | ||
|
||
Make a card appear by inserting a new card at the bottom of the stack. | ||
Return the adjusted stack. | ||
|
||
```javascript | ||
const newCard = 8; | ||
insertItemAtBottom([5, 9, 7, 1], newCard); | ||
// => [8, 5, 9, 7, 1] | ||
``` | ||
|
||
## 7. Remove a card from the bottom of the stack | ||
|
||
Make a card disappear by removing the card at the bottom of the stack. | ||
Return the adjusted stack. | ||
|
||
```javascript | ||
removeItemAtBottom([8, 5, 9, 7, 1]); | ||
// => [5, 9, 7, 1] | ||
``` | ||
|
||
## 8. Check the size of the stack | ||
|
||
Check whether the size of the stack is equal to `stackSize` or not. | ||
|
||
```javascript | ||
const stackSize = 4; | ||
checkSizeOfStack([3, 2, 6, 4, 8], stackSize); | ||
// => false | ||
``` | ||
|
||
## Source | ||
|
||
### Created by | ||
|
||
- @ovidiu141 | ||
- @SleeplessByte | ||
|
||
### Contributed to by | ||
|
||
- @peterchu999 | ||
- @pertrai1 | ||
- @nasch |
Oops, something went wrong.