Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
3 changes: 3 additions & 0 deletions concepts/callbacks/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ You see this pattern often when dealing with asynchronous functions to assist wi
Common `Array` functions use callback functions to define their behaviour:

- `Array.prototype.forEach`:

- Accepts a callback, which applies the callback to each element of an array.

```javascript
Expand All @@ -91,6 +92,7 @@ Common `Array` functions use callback functions to define their behaviour:
```

- `Array.prototype.map`

- Accepts a callback, which applies the callback to each element of an array using the result to create a new array.

```javascript
Expand All @@ -101,6 +103,7 @@ Common `Array` functions use callback functions to define their behaviour:
```

- `Array.prototype.reduce`

- Accepts a callback, which applies the callback to each element of an array, passing the result forward to the next invocation.

```javascript
Expand Down
2 changes: 2 additions & 0 deletions concepts/closures/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ The name _closure_ is historically derived from [_λ-calculus_][wiki-lambda-calc
## Reasons to use closures in JavaScript

1. Data Privacy / Data Encapsulation

- Unlike other languages, in 2020, there was no way to specify _private_ variables. So closures can be used to effectively emulate _private_ variables (there was a proposal to introduce private variable notation, which might have become standard by the time you read this).

```javascript
Expand All @@ -36,6 +37,7 @@ The name _closure_ is historically derived from [_λ-calculus_][wiki-lambda-calc
```

2. Partial Application

- Functions may return functions, and when a returned function uses the argument of the function that created it, this is an example of using a closure to perform partial application. Sometimes this is called _currying_ a function.

```javascript
Expand Down
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2770,6 +2770,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "error-handling",
"name": "Error Handling",
"uuid": "de1c75f2-2461-4347-b5ca-b3cfaafe4d79",
"practices": [],
"prerequisites": [],
"difficulty": 1
}
]
},
Expand Down
4 changes: 4 additions & 0 deletions exercises/concept/amusement-park/.meta/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,26 @@ This exercise could benefit from the following rules in the [analyzer][analyzer]
The comment types mentioned below only serve as a proposal.

1. `createVisitor`

- `actionable`: If the student used a helper variable, give feedback that the result can be returned directly.
- `celebratory`: If the student used classes, celebrate but let them know it is not necessary throughout this exercise.
- `informative`: If the student did not use the short-hand notation but wrote `name: name` etc instead, let them know how to shorten that.
The solution should be accepted nevertheless.

2. `revokeTicket`

- `essential`: Check the ticketId field is not deleted and re-added.
- `celebratory`: If they used a method on a visitor class, celebrate but let them know it is not necessary for this exercise.

3. `ticketStatus`

- `essential`: Using a type switch should be discouraged since it is confusing to read because of the `typeof null === 'object'` quirk.
- `informative`: If the student did not use early returns, maybe let them know about this alternative.
- `celebratory`: Congratulate if the student used a template string for the "sold" case
- `celebratory`: Congratulate if the student used a `value` helper variable.

4. `simpleTicketStatus`

- `essential`: Check `??` was used and not an if-statement or something else.
- `actionable`: If the student used a helper variable, give feedback that the result can be returned directly.

Expand Down
2 changes: 2 additions & 0 deletions exercises/concept/bird-watcher/.meta/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ This exercise could benefit from the following rules in the [analyzer][analyzer]
For all tasks check that the student actually used a for loop.

1. `totalBirdCount`

- Verify that the condition is written with `< x.length` instead of `<= y.length -1`.
- Check whether a shorthand assignment `+=` was used to increase the sum (non-essential feedback).
- Verify the total was properly initialized with `0` instead of e.g. `null`
- Verify the increment operator was used in loop header step

2. `birdsInWeek`

- Verify a helper variable was used instead of duplicating the calculation in the initialization and condition of the loop
- Other checks should be the same as for `totalBirdCount`

Expand Down
5 changes: 5 additions & 0 deletions exercises/concept/high-score-board/.meta/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,27 @@ The Concepts this exercise unlocks are:
This exercise could benefit from the following rules in the [analyzer][analyzer]:

1. `createScoreBoard`

- `essential`: Make sure no class, map etc. was created, there should be just an object.
- `actionable`: If the student created an empty object first and then added the value, give feedback to include the entry in the object literal directly.
- `actionable`: Check that the object was returned directly, no intermediate assignment to a variable necessary.

2. `addPlayer`

- `essential`: Check the assignment operator was used and no additional variables were declared.

3. `removePlayer`

- `essential`: Make sure `delete` was used and not set to undefined or null.
- `actionable`: If there is additional code to check whether the key is present before deleting it, give feedback that this is not necessary.

4. `updateScore`

- `actionable`: If the student used a separate variable to calculate the new value first, tell them it is not necessary.
- `actionable`: If the student did not use the shorthand assignment operator, tell them about it. If they used it already, give a `celebratory` comment.

5. `applyMondayBonus`

- `essential`: Check the student actually used `for...in`.
- Same feedback as in `updateScore` applies.
- Using `updateScore` in the solution should be treated as equally correct as the exemplar solution.
Expand Down
2 changes: 2 additions & 0 deletions exercises/concept/mixed-juices/.meta/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ This exercise could benefit from the following rules in the [analyzer][analyzer]
The comment types mentioned below only serve as a proposal.

1. `timeToMixJuice`

- `essential`: Verify the student used a switch statement.
Would be nice if we could give different feedback depending on what the student used instead.
If it was if-else, comment that switch is better suited for so many different variants.
Expand All @@ -52,6 +53,7 @@ The comment types mentioned below only serve as a proposal.
```

2. `limesToCut`

- A solution that uses `if (limes.length < 0) break;` instead of combining the conditions should be considered equally correct to the exemplar solution.
The version in the exemplar file is shorter but the break version emphasizes that there is a special edge case.
- `essential`: Verify that `while` was used.
Expand Down
1 change: 1 addition & 0 deletions exercises/concept/ozans-playlist/.meta/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ This exercise could benefit from the following rules in the [analyzer][analyzer]
For all tasks, verify that the student actually used a `Set`.

1. `addTrack`

- Verify that there was no redundant `Set.has()` call

2. `deleteTrack`
Expand Down
7 changes: 7 additions & 0 deletions exercises/practice/error-handling/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Instructions

Implement various kinds of error handling and resource management.

An important point of programming is how to handle errors and close resources even if errors occur.

This exercise requires you to handle various errors.
31 changes: 31 additions & 0 deletions exercises/practice/error-handling/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Error Handling

In this exercise, you will implement a function called `processString` that processes a given input string with proper error handling.

You will learn how to:

- Check input types and throw errors for invalid inputs.
- Throw errors for specific cases (e.g., empty strings).
- Return the uppercase version of the string if it is valid.
- Handle and catch errors using a try..catch block


Implement the processString function using a `try…catch` block.

Inside the `try` block:

- If the input is not a string, throw a `TypeError`.

- If the input is an empty string, return `null`.

- If input length is greater than 100, throw a `RangeError`.

- If input length is less than 10, throw a `RangeError`.

- If input contains a mix of letters and numbers, throw a `SyntaxError`.

- Otherwise, return the input in `uppercase`.

Inside the `catch` block:
- log the error's message using `console.log`
- `throw` the `error` so it can be tested for its type.
5 changes: 5 additions & 0 deletions exercises/practice/error-handling/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/node_modules
/bin/configlet
/bin/configlet.exe
/package-lock.json
/yarn.lock
17 changes: 17 additions & 0 deletions exercises/practice/error-handling/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"authors": [
"A-O-Emmanuel"
],
"files": {
"solution": [
"error-handling.js"
],
"test": [
"error-handling.spec.js"
],
"example": [
".meta/proof.ci.js"
]
},
"blurb": "Implement various kinds of error handling and resource management."
}
26 changes: 26 additions & 0 deletions exercises/practice/error-handling/.meta/proof.ci.js
Comment thread
A-O-Emmanuel marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export const processString = (input) => {
try {
if (typeof input !== 'string') {
throw new TypeError('Input must be a string');
}
if (input === '') {
return null;
}
if (input.length > 100) {
throw new RangeError('Input is too long');
}
if (input.length < 10) {
throw new RangeError('Input is too short');
}
if (/[a-zA-Z]/.test(input) && /\d/.test(input)) {
throw new SyntaxError(
'Input cannot contain a mix of letters and numbers',
);
}

return input.toUpperCase();
} catch (error) {
console.log(error.message);
throw error;
}
};
1 change: 1 addition & 0 deletions exercises/practice/error-handling/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
audit=false
21 changes: 21 additions & 0 deletions exercises/practice/error-handling/LICENSE
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.
4 changes: 4 additions & 0 deletions exercises/practice/error-handling/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
presets: [['@exercism/babel-preset-javascript', { corejs: '3.40' }]],
plugins: [],
};
8 changes: 8 additions & 0 deletions exercises/practice/error-handling/error-handling.js
Comment thread
A-O-Emmanuel marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//
// This is only a SKELETON file for the 'Error handling' exercise. It's been provided as a
// convenience to get you started writing code faster.
//

export const processString = (input) => {
throw new Error('Remove this line and implement the function');
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the way this currently is, it'll pass one of the tests by default, without the student changing anything.

};
72 changes: 72 additions & 0 deletions exercises/practice/error-handling/error-handling.spec.js
Comment thread
A-O-Emmanuel marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { describe, expect, test, xtest } from '@jest/globals';
import { processString } from './error-handling';

describe('Error Handling', () => {
xtest('throws TypeError if input is not a string', () => {
expect(() => processString(42)).toThrow(
expect.objectContaining({
name: 'TypeError',
message: expect.stringMatching(/.+/),
}),
);
});

xtest('returns null if string is empty', () => {
expect(processString('')).toBeNull();
});

xtest('throws error if input is too short', () => {
expect(() => processString('short')).toThrow(
expect.objectContaining({
name: 'RangeError',
message: expect.stringMatching(/.+/),
}),
);
});

xtest('throws error if input is too long', () => {
const longString = 'a'.repeat(101);
expect(() => processString(longString)).toThrow(
expect.objectContaining({
name: 'RangeError',
message: expect.stringMatching(/.+/),
}),
);
});

xtest('throws error if input contains a mix of letters and numbers', () => {
expect(() => processString('12345test6789text')).toThrow(
expect.objectContaining({
name: 'SyntaxError',
message: expect.stringMatching(/.+/),
}),
);
});

xtest('returns uppercase string if input is valid', () => {
expect(processString('hellotherefriend')).toBe('HELLOTHEREFRIEND');
});

xtest('never throws a generic Error for any invalid input', () => {
const invalidInputs = [
42, // TypeError
'short', // RangeError (too short)
'a'.repeat(101), // RangeError (too long)
'12345test6789text', // SyntaxError (mixed)
];

for (const input of invalidInputs) {
let error;

try {
processString(input);
} catch (err) {
error = err;
}

expect(error).toBeInstanceOf(Error);
expect(error.constructor).not.toBe(Error);
expect(error.message).toEqual(expect.stringMatching(/.+/));
}
});
});
45 changes: 45 additions & 0 deletions exercises/practice/error-handling/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// @ts-check

import config from '@exercism/eslint-config-javascript';
import maintainersConfig from '@exercism/eslint-config-javascript/maintainers.mjs';

import globals from 'globals';

export default [
...config,
...maintainersConfig,
{
files: maintainersConfig[1].files,
rules: {
'jest/expect-expect': ['warn', { assertFunctionNames: ['expect*'] }],
},
},
{
files: ['scripts/**/*.mjs'],
languageOptions: {
globals: {
...globals.node,
},
},
},
// <<inject-rules-here>>
{
ignores: [
// # Protected or generated
'/.appends/**/*',
'/.github/**/*',
'/.vscode/**/*',

// # Binaries
'/bin/*',

// # Configuration
'/config',
'/babel.config.js',

// # Typings
'/exercises/**/global.d.ts',
'/exercises/**/env.d.ts',
],
},
];
Loading