-
-
Notifications
You must be signed in to change notification settings - Fork 161
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
0f8953b
commit 8a2f320
Showing
19 changed files
with
604 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,30 @@ | ||
# Hints | ||
|
||
## 1. Define the expected oven time in minutes | ||
|
||
- Define a [constant][constants] which should contain the [`number`][numbers] value specified in the recipe. | ||
- [`export`][export] the constant. | ||
|
||
## 2. Calculate the remaining oven time in minutes | ||
|
||
- [Explicitly return a number][return] from the function. | ||
- Use the [mathematical operator for subtraction][operators] to subtract values. | ||
|
||
## 3. Calculate the preparation time in minutes | ||
|
||
- [Explicitly return a number][return] from the function. | ||
- Use the [mathematical operator for multiplication][operators] to multiply values. | ||
- Use the extra constant for the time in minutes per layer. | ||
|
||
## 4. Calculate the total working time in minutes | ||
|
||
- [Explicitly return a number][return] from the function. | ||
- [Invoke][invocation] one of the other methods implemented previously. | ||
- Use the [mathematical operator for addition][operators] to add values. | ||
|
||
[return]: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Return_values | ||
[export]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export | ||
[operators]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators | ||
[constants]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const | ||
[invocation]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions#Calling_functions | ||
[numbers]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type |
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,38 @@ | ||
# Instructions | ||
|
||
Lucian's girlfriend is on her way home, and he hasn't cooked their anniversary dinner! | ||
|
||
In this exercise, you're going to write some code to help Lucian cook an exquisite lasagna from his favorite cookbook. | ||
|
||
You have four tasks related to the time spent cooking the lasagna. | ||
|
||
## 1. Define the expected oven time in minutes | ||
|
||
Define the `EXPECTED_MINUTES_IN_OVEN` constant that represents how many minutes the lasagna should be in the oven. It must be exported. According to the cooking book, the expected oven time in minutes is `40`. | ||
|
||
## 2. Calculate the remaining oven time in minutes | ||
|
||
Implement the `remainingMinutesInOven` function that takes the actual minutes the lasagna has been in the oven as a _parameter_ and _returns_ how many minutes the lasagna still has to remain in the oven, based on the **expected oven time in minutes** from the previous task. | ||
|
||
```javascript | ||
remainingMinutesInOven(30); | ||
// => 10 | ||
``` | ||
|
||
## 3. Calculate the preparation time in minutes | ||
|
||
Implement the `preparationTimeInMinutes` function that takes the number of layers you added to the lasagna as a _parameter_ and _returns_ how many minutes you spent preparing the lasagna, assuming each layer takes you 2 minutes to prepare. | ||
|
||
```javascript | ||
preparationTimeInMinutes(2); | ||
// => 4 | ||
``` | ||
|
||
## 4. Calculate the total working time in minutes | ||
|
||
Implement the `totalTimeInMinutes` function that takes _two parameters_: the `numberOfLayers` parameter is the number of layers you added to the lasagna, and the `actualMinutesInOven` parameter is the number of minutes the lasagna has been in the oven. The function should _return_ how many minutes in total you've worked on cooking the lasagna, which is the sum of the preparation time in minutes, and the time in minutes the lasagna has spent in the oven at the moment. | ||
|
||
```javascript | ||
totalTimeInMinutes(3, 20); | ||
// => 26 | ||
``` |
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,71 @@ | ||
# Introduction | ||
|
||
JavaScript is a dynamic language, supporting object-oriented, imperative, and declarative (e.g. functional programming) styles. | ||
|
||
## (Re-)Assignment | ||
|
||
There are a few primary ways to assign values to names in JavaScript - using variables or constants. On Exercism, variables are always written in [camelCase][wiki-camel-case]; constants are written in [SCREAMING_SNAKE_CASE][wiki-snake-case]. There is no official guide to follow, and various companies and organizations have various style guides. _Feel free to write variables any way you like_. The upside from writing them the way the exercises are prepared is that they'll be highlighted differently in the web interface and most IDEs. | ||
|
||
Variables in JavaScript can be defined using the [`const`][mdn-const], [`let`][mdn-let] or [`var`][mdn-var] keyword. | ||
|
||
A variable can reference different values over its lifetime when using `let` or `var`. For example, `myFirstVariable` can be defined and redefined many times using the assignment operator `=`: | ||
|
||
```javascript | ||
let myFirstVariable = 1; | ||
myFirstVariable = 'Some string'; | ||
myFirstVariable = new SomeComplexClass(); | ||
``` | ||
|
||
In contrast to `let` and `var`, variables that are defined with `const` can only be assigned once. This is used to define constants in JavaScript. | ||
|
||
```javascript | ||
const MY_FIRST_CONSTANT = 10; | ||
|
||
// Can not be re-assigned. | ||
MY_FIRST_CONSTANT = 20; | ||
// => TypeError: Assignment to constant variable. | ||
``` | ||
|
||
> 💡 In a later Concept Exercise the difference between _constant_ assignment / binding and _constant_ value is explored and explained. | ||
## Function Declarations | ||
|
||
In JavaScript, units of functionality are encapsulated in _functions_, usually grouping functions together in the same file if they belong together. These functions can take parameters (arguments), and can _return_ a value using the `return` keyword. Functions are invoked using `()` syntax. | ||
|
||
```javascript | ||
function add(num1, num2) { | ||
return num1 + num2; | ||
} | ||
|
||
add(1, 3); | ||
// => 4 | ||
``` | ||
|
||
> 💡 In JavaScript there are _many_ different ways to declare a function. These other ways look different than using the `function` keyword. The track tries to gradually introduce them, but if you already know about them, feel free to use any of them. In most cases, using one or the other isn't better or worse. | ||
## Exposing to Other Files | ||
|
||
To make a `function`, a constant, or a variable available in _other files_, they need to be [exported][mdn-export] using the `export` keyword. Another file may then [import][mdn-import] these using the `import` keyword. This is also known as the module system. A great example is how all the tests work. Each exercise has at least one file, for example `lasagna.js`, which contains the _implementation_. Additionally there is at least one other file, for example `lasagna.spec.js`, that contains the _tests_. This file _imports_ the public (i.e. exported) entities in order to test the implementation: | ||
|
||
```javascript | ||
// file.js | ||
export const MY_VALUE = 10; | ||
|
||
export function add(num1, num2) { | ||
return num1 + num2; | ||
} | ||
|
||
// file.spec.js | ||
import { MY_VALUE, add } from './file'; | ||
|
||
add(MY_VALUE, 5); | ||
// => 15 | ||
``` | ||
|
||
[mdn-const]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const | ||
[mdn-export]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export | ||
[mdn-import]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import | ||
[mdn-let]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let | ||
[mdn-var]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var | ||
[wiki-camel-case]: https://en.wikipedia.org/wiki/Camel_case | ||
[wiki-snake-case]: https://en.wikipedia.org/wiki/Snake_case |
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,27 @@ | ||
{ | ||
"authors": ["SleeplessByte"], | ||
"files": { | ||
"solution": [ | ||
"lasagna.ts" | ||
], | ||
"test": [ | ||
"__typetests__/lasagna.tst.ts", | ||
"lasagna.test.ts" | ||
], | ||
"exemplar": [ | ||
".meta/exemplar.ts" | ||
] | ||
}, | ||
"forked_from": [ | ||
"javascript/lasagna" | ||
], | ||
"blurb": "Learn the basics of TypeScript cooking a brilliant lasagna from your favorite cooking book.", | ||
"custom": { | ||
"version.tests.compatibility": "jest-29", | ||
"flag.tests.task-per-describe": true, | ||
"flag.tests.may-run-long": false, | ||
"flag.tests.includes-optional": false, | ||
"flag.tests.jest": true, | ||
"flag.tests.tstyche": true | ||
} | ||
} |
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,38 @@ | ||
# Design | ||
|
||
## Learning objectives | ||
|
||
- Know what a variable is. | ||
- Know what a constant variable is. | ||
- Know how to define a variable. | ||
- Know how to export a variable | ||
- Know how to return a value from a function (explicit return). | ||
- Know how to annotate a function parameter | ||
- Know how to annotate a function return type | ||
|
||
## Out of scope | ||
|
||
This exercise is really just to introduce the bare minimum a student needs to know to solve a very basic exercise on Exercism. | ||
Details about the primitive data types, different ways to define functions etc. will all be properly introduced in the later concept exercises. | ||
|
||
We don't even explicitly teach the basics of numbers and arithmetic operators in the introduction. | ||
Given the general code examples that are provided and some "I will just try that", the student should be fine solving the exercise nevertheless. | ||
|
||
## Concepts | ||
|
||
- `basics` | ||
|
||
## Prerequisites | ||
|
||
There are no prerequisites. | ||
|
||
## Analyzer | ||
|
||
This exercise could benefit from the following rules added to the the [analyzer][analyzer]: | ||
|
||
- Verify that the `remainingMinutesInOven` function uses the `EXPECTED_MINUTES_IN_OVEN` constant. | ||
- Verify that the `preparationTimeInMinutes` function uses the `PREPARATION_MINUTES_PER_LAYER` constant. | ||
- Verify that the `totalTimeInMinutes` function calls the `preparationTimeInMinutes` function. | ||
- Verify that no extra _bookkeeping_ or _intermediate_ variables are declared | ||
|
||
[analyzer]: https://github.com/exercism/typescript-analyzer |
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,45 @@ | ||
/** | ||
* The amount of minutes the lasagna should be in the oven. | ||
*/ | ||
export const EXPECTED_MINUTES_IN_OVEN = 40 | ||
|
||
/** | ||
* The amount of minutes it takes to prepare a single layer. | ||
*/ | ||
const PREPARATION_MINUTES_PER_LAYER = 2 | ||
|
||
/** | ||
* Determines the amount of minutes the lasagna still needs to remain in the | ||
* oven to be properly prepared. | ||
* | ||
* @param actualMinutesInOven | ||
* @returns the number of minutes remaining | ||
*/ | ||
export function remainingMinutesInOven(actualMinutesInOven: number): number { | ||
return EXPECTED_MINUTES_IN_OVEN - actualMinutesInOven | ||
} | ||
|
||
/** | ||
* Given a number of layers, determines the total preparation time. | ||
* | ||
* @param numberOfLayers | ||
* @returns the total preparation time | ||
*/ | ||
export function preparationTimeInMinutes(numberOfLayers: number): number { | ||
return numberOfLayers * PREPARATION_MINUTES_PER_LAYER | ||
} | ||
|
||
/** | ||
* Calculates the total working time. That is, the time to prepare all the layers | ||
* of lasagna, and the time already spent in the oven. | ||
* | ||
* @param numberOfLayers | ||
* @param actualMinutesInOven | ||
* @returns the total working time | ||
*/ | ||
export function totalTimeInMinutes( | ||
numberOfLayers: number, | ||
actualMinutesInOven: number | ||
): number { | ||
return preparationTimeInMinutes(numberOfLayers) + actualMinutesInOven | ||
} |
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,54 @@ | ||
import { execSync } from 'node:child_process' | ||
// Experimental: import config from './config.json' with { type: 'json' } | ||
|
||
import { readFileSync } from 'node:fs' | ||
import { exit } from 'node:process' | ||
|
||
/** @type {import('./config.json') } */ | ||
const config = JSON.parse( | ||
readFileSync(new URL('./config.json', import.meta.url)) | ||
) | ||
|
||
const jest = !config.custom || config.custom['flag.tests.jest'] | ||
const tstyche = config.custom?.['flag.tests.tstyche'] | ||
|
||
console.log( | ||
`[tests] tsc: ✅, tstyche: ${tstyche ? '✅' : '❌'}, jest: ${jest ? '✅' : '❌'}, ` | ||
) | ||
|
||
console.log('[tests] tsc (compile)') | ||
|
||
try { | ||
execSync('corepack yarn lint:types', { | ||
stdio: 'inherit', | ||
cwd: process.cwd(), | ||
}) | ||
} catch { | ||
exit(-1) | ||
} | ||
|
||
if (tstyche) { | ||
console.log('[tests] tstyche (type tests)') | ||
|
||
try { | ||
execSync('corepack yarn test:types', { | ||
stdio: 'inherit', | ||
cwd: process.cwd(), | ||
}) | ||
} catch { | ||
exit(-2) | ||
} | ||
} | ||
|
||
if (jest) { | ||
console.log('[tests] tstyche (implementation tests)') | ||
|
||
try { | ||
execSync('corepack yarn test:implementation', { | ||
stdio: 'inherit', | ||
cwd: process.cwd(), | ||
}) | ||
} catch { | ||
exit(-3) | ||
} | ||
} |
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,7 @@ | ||
{ | ||
"recommendations": [ | ||
"arcanis.vscode-zipfs", | ||
"dbaeumer.vscode-eslint", | ||
"esbenp.prettier-vscode" | ||
] | ||
} |
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,7 @@ | ||
{ | ||
"cSpell.words": ["exercism"], | ||
"search.exclude": { | ||
"**/.yarn": true, | ||
"**/.pnp.*": true | ||
} | ||
} |
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,3 @@ | ||
compressionLevel: mixed | ||
|
||
enableGlobalCache: true |
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,45 @@ | ||
import { describe, expect, test } from 'tstyche' | ||
import { | ||
EXPECTED_MINUTES_IN_OVEN, | ||
remainingMinutesInOven, | ||
preparationTimeInMinutes, | ||
totalTimeInMinutes, | ||
} from '../lasagna.ts' | ||
|
||
describe('EXPECTED_MINUTES_IN_OVEN', () => { | ||
test('constant is defined as a number or a constant number', () => { | ||
expect(EXPECTED_MINUTES_IN_OVEN).type.toBeAssignableTo<number>() | ||
}) | ||
}) | ||
|
||
describe('remainingMinutesInOven', () => { | ||
test('takes one number parameter', () => { | ||
expect<Parameters<typeof remainingMinutesInOven>>().type.toBe<[number]>() | ||
}) | ||
|
||
test('returns a number', () => { | ||
expect<ReturnType<typeof remainingMinutesInOven>>().type.toBe<number>() | ||
}) | ||
}) | ||
|
||
describe('preparationTimeInMinutes', () => { | ||
test('takes one number parameter', () => { | ||
expect<Parameters<typeof preparationTimeInMinutes>>().type.toBe<[number]>() | ||
}) | ||
|
||
test('returns a number', () => { | ||
expect<ReturnType<typeof preparationTimeInMinutes>>().type.toBe<number>() | ||
}) | ||
}) | ||
|
||
describe('totalTimeInMinutes', () => { | ||
test('takes two number parameters', () => { | ||
expect<Parameters<typeof totalTimeInMinutes>>().type.toBe< | ||
[number, number] | ||
>() | ||
}) | ||
|
||
test('returns a number', () => { | ||
expect<ReturnType<typeof totalTimeInMinutes>>().type.toBe<number>() | ||
}) | ||
}) |
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,4 @@ | ||
module.exports = { | ||
presets: [[require('@exercism/babel-preset-typescript'), { corejs: '3.37' }]], | ||
plugins: [], | ||
} |
Oops, something went wrong.