Skip to content
Merged
Show file tree
Hide file tree
Changes from 50 commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
5f5dee3
Create captains-log.js
quintuple-mallard Jun 18, 2025
784ab06
Create captains-log.spec.js
quintuple-mallard Jun 18, 2025
839ff5d
Create config.json
quintuple-mallard Jun 18, 2025
9660cf4
Rename config.json to config.json
quintuple-mallard Jun 18, 2025
aeaa04e
Create design.md
quintuple-mallard Jun 18, 2025
fe7da9a
Create exemplar.js
quintuple-mallard Jun 18, 2025
365d8cc
Update exemplar.js
quintuple-mallard Jun 18, 2025
580e1a9
Update exemplar.js
quintuple-mallard Jun 18, 2025
77af0d8
Update exemplar.js
quintuple-mallard Jun 18, 2025
f2a7ef8
Create hints.md
quintuple-mallard Jun 18, 2025
91e048f
Create instructions.md
quintuple-mallard Jun 18, 2025
5ba9ac2
Create introduction.md
quintuple-mallard Jun 18, 2025
1de010d
Modified tests in captains-log.spec.js
quintuple-mallard Jun 18, 2025
c334b74
Update captains-log.spec.js
quintuple-mallard Jun 18, 2025
b9d179f
Update config.json
quintuple-mallard Jun 18, 2025
06a5d05
Update captains-log.spec.js
quintuple-mallard Jun 18, 2025
87ef708
Removed extra parenthesis
quintuple-mallard Jun 18, 2025
a84402b
Added captains-log to config.json
quintuple-mallard Jun 18, 2025
a249a06
Update captains-log.spec.js
quintuple-mallard Jun 18, 2025
ef7aa46
Update config.json
quintuple-mallard Jun 18, 2025
5fbe8cf
Update captains-log.spec.js
quintuple-mallard Jun 18, 2025
9a07643
Added randomness to concepts list
quintuple-mallard Jun 18, 2025
eac6873
Rename docs to .docs
quintuple-mallard Jun 18, 2025
d9adf15
Rename instructions.md to instructions.md
quintuple-mallard Jun 18, 2025
b068bfc
Rename introduction.md to introduction.md
quintuple-mallard Jun 18, 2025
fb945a5
Merge branch 'exercism:main' into main
quintuple-mallard Jun 19, 2025
f03e1e7
Update config.json
quintuple-mallard Jun 19, 2025
47f5e27
Update config.json
quintuple-mallard Jun 19, 2025
b3cd46f
Create config.json in concepts
quintuple-mallard Jun 19, 2025
d2436ae
Rename concepts/randomness/config.json to concepts/randomness/.meta/c…
quintuple-mallard Jun 19, 2025
6d25bfb
Create about.md
quintuple-mallard Jun 19, 2025
9c2a849
Create introduction.md
quintuple-mallard Jun 19, 2025
3779474
Update config.json
quintuple-mallard Jun 19, 2025
855ccd2
Create links.json
quintuple-mallard Jun 19, 2025
fe381c7
Update config.json
quintuple-mallard Jun 19, 2025
173333d
Update links.json
quintuple-mallard Jun 19, 2025
f946ae5
Update exercises/concept/captains-log/captains-log.spec.js
quintuple-mallard Jun 19, 2025
0cdd9ac
Fix tests
SleeplessByte Jun 19, 2025
ac1d8da
Format 12 files
quintuple-mallard Jun 19, 2025
2d17cba
Fix broken test
quintuple-mallard Jun 19, 2025
88e02f2
Merge branch 'exercism:main' into main
quintuple-mallard Jun 20, 2025
8c7d877
Create .gitignore
quintuple-mallard Jun 20, 2025
4d943eb
Create .npmrc
quintuple-mallard Jun 20, 2025
8bdd28b
Create LICENSE
quintuple-mallard Jun 20, 2025
65b9982
Create babel.config.js
quintuple-mallard Jun 20, 2025
a786e47
Create eslint.config.mjs
quintuple-mallard Jun 20, 2025
e15e862
Create jest.config.js
quintuple-mallard Jun 20, 2025
8e45315
Create package.json
quintuple-mallard Jun 20, 2025
10f181e
Add line break in exercises/concept/captains-log/.docs/instructions.md
quintuple-mallard Jun 20, 2025
4c3e747
Added line breaks to instructions.md
quintuple-mallard Jun 20, 2025
1fd3f14
Apply suggestions from code review
SleeplessByte Jun 20, 2025
eebe50a
Format
SleeplessByte Jun 20, 2025
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
5 changes: 5 additions & 0 deletions concepts/randomness/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"blurb": "Random number generation using Math.random()",
"authors": ["SneakyMallard"],
"contributors":[]
}
41 changes: 41 additions & 0 deletions concepts/randomness/about.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Introduction

Many programs need (apparently) random values to simulate real-world events.

Common, familiar examples include:

- A coin toss: a random value from ('H', 'T').
- The roll of a die: a random integer from 1 to 6.
- Shuffling a deck of cards: a random ordering of a card list.
- The creation of trees and bushes in a 3-D graphics simulation.

Generating truly random values with a computer is a [surprisingly difficult technical challenge][why-randomness-is-hard], so you may see these results referred to as "pseudorandom".

## Generating random numbers

In Javascript, you can generate psuedorandom numbers using the [`Math.random()`][Math.random] function.
It will return a psuedorandom floating-point number between 0 (inclusive), and 1 (exclusive).

To get a random number between _min_ (inclusive) and _max_ (exclusive) you can use a function something like this:

```javascript
function getRandomInRange(min, max) {
return min + Math.random() * (max - min);
}
getRandomInRange(4, 10);
// => 5.72
```

## Generating random integers

To generate a random integer, you can use `Math.floor()` or `Math.ceil()` to turn a randomly generated number into an integer.

```exercism/caution

The `Math.random()` function should NOT be used for security and cryptographic applications!!

Instead, you can use the Web Crypto API, which provides various cryptographic functions.
```

[why-randomness-is-hard]: https://www.malwarebytes.com/blog/news/2013/09/in-computers-are-random-numbers-really-random
[Math.random]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random
41 changes: 41 additions & 0 deletions concepts/randomness/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Introduction

Many programs need (apparently) random values to simulate real-world events.

Common, familiar examples include:

- A coin toss: a random value from ('H', 'T').
- The roll of a die: a random integer from 1 to 6.
- Shuffling a deck of cards: a random ordering of a card list.
- The creation of trees and bushes in a 3-D graphics simulation.

Generating truly random values with a computer is a [surprisingly difficult technical challenge][why-randomness-is-hard], so you may see these results referred to as "pseudorandom".

## Generating random numbers

In Javascript, you can generate psuedorandom numbers using the [`Math.random()`][Math.random] function.
It will return a psuedorandom floating-point number between 0 (inclusive), and 1 (exclusive).

To get a random number between _min_ (inclusive) and _max_ (exclusive) you can use a function something like this:

```javascript
function getRandomInRange(min, max) {
return min + Math.random() * (max - min);
}
getRandomInRange(4, 10);
// => 5.72
```

## Generating random integers

To generate a random integer, you can use `Math.floor()` or `Math.ceil()` to turn a randomly generated number into an integer.

```exercism/caution

The `Math.random()` function should NOT be used for security and cryptographic applications!!

Instead, you can use the Web Crypto API, which provides various cryptographic functions.
```

[why-randomness-is-hard]: https://www.malwarebytes.com/blog/news/2013/09/in-computers-are-random-numbers-really-random
[Math.random]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random
6 changes: 6 additions & 0 deletions concepts/randomness/links.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"url": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random",
"description": "MDN: The Math.random() function"
}
]
17 changes: 17 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,18 @@
"type-conversion"
],
"status": "beta"
},
{
"slug": "captains-log",
"name": "Captain's Log",
"uuid": "65cf28ab-243c-41cb-a720-f324f2cabe28",
"concepts": [
"randomness"
],
"prerequisites": [
"numbers",
"arithmetic-operators"
]
}
],
"practice": [
Expand Down Expand Up @@ -2851,6 +2863,11 @@
"uuid": "4e68e39a-e36c-4d2d-8714-eb6482e31ff5",
"slug": "while-loops",
"name": "While Loops"
},
{
"uuid": "ca322d6f-0f7e-4a2d-a058-e98a59cdae93",
"slug": "randomness",
"name": "Randomness"
}
],
"key_features": [
Expand Down
16 changes: 16 additions & 0 deletions exercises/concept/captains-log/.docs/hints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Hints

## 1. Generate a random starship registry number

- To generate a random number in the range _min_ (inclusive) to _max_ (exclusive) you can use the snippet `min + Math.random()*(max - min)`.
- There is a [built in function][floor] for turning a floating point number into an integer.

## 2.Generate a random stardate

- To generate a random number in the range _min_ (inclusive) to _max_ (exclusive) you can use the snippet `min + Math.random()*(max - min)`.

## 3. Generate a random planet

- You can use a randomly generated integer as an array index.

[floor]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor
52 changes: 52 additions & 0 deletions exercises/concept/captains-log/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
## Instructions

Mary is a big fan of the TV series Star Trek: The Next Generation.
She often plays pen-and-paper role playing games, where she and her friends pretend to be the crew of the Starship Enterprise.
Mary's character is Captain Picard, which means she has to keep the captain's log.
She loves the creative part of the game, but doesn't like to generate random data on the spot.

Help Mary by creating random generators for data commonly appearing in the captain's log.

### 1. Generate a random starship registry number

Enterprise (registry number NCC-1701) is not the only starship flying around!
When it rendezvous with another starship, Mary needs to log the registry number of that starship.

Registry numbers start with the prefix "NCC-" and then use a number from 1000 to 9999 (both inclusive).

Implement the randomShipRegistryNumber() function that returns a random starship registry number.

```javascript
randomShipRegistryNumber();
// => "NCC-1947"
```

### 2. Generate a random stardate

What's the use of a log if it doesn't include dates?

A stardate is a floating point number.
The adventures of the Starship Enterprise from the first season of The Next Generation take place between the stardates 41000.0 and 42000.0.
The "4" stands for the 24th century, the "1" for the first season.

Implement the function randomStardate that returns a floating point number between 41000.0 (inclusive) and 42000.0 (exclusive).

```javascript
randomStardate();
// => 41458.15721310934
```

### 3. Generate a random planet

The Starship Enterprise encounters many planets in its travels.
Planets in the Star Trek universe are split into categories based on their properties.
For example, Earth is a class M planet.
All possible planetary classes are: D, H, J, K, L, M, N, R, T, and Y.

Implement the randomPlanetClassfunction.
It should return one of the planetary classes at random.

```javascript
randomPlanetClass();
// => "K"
```
41 changes: 41 additions & 0 deletions exercises/concept/captains-log/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Introduction

Many programs need (apparently) random values to simulate real-world events.

Common, familiar examples include:

- A coin toss: a random value from ('H', 'T').
- The roll of a die: a random integer from 1 to 6.
- Shuffling a deck of cards: a random ordering of a card list.
- The creation of trees and bushes in a 3-D graphics simulation.

Generating truly random values with a computer is a [surprisingly difficult technical challenge][why-randomness-is-hard], so you may see these results referred to as "pseudorandom".

## Generating random numbers

In Javascript, you can generate psuedorandom numbers using the [`Math.random()`][Math.random] function.
It will return a psuedorandom floating-point number between 0 (inclusive), and 1 (exclusive).

To get a random number between _min_ (inclusive) and _max_ (exclusive) you can use a function something like this:

```javascript
function getRandomInRange(min, max) {
return min + Math.random() * (max - min);
}
getRandomInRange(4, 10);
// => 5.72
```

## Generating random integers

To generate a random integer, you can use `Math.floor()` or `Math.ceil()` to turn a randomly generated number into an integer.

```exercism/caution

The `Math.random()` function should NOT be used for security and cryptographic applications!!

Instead, you can use the Web Crypto API, which provides various cryptographic functions.
```

[why-randomness-is-hard]: https://www.malwarebytes.com/blog/news/2013/09/in-computers-are-random-numbers-really-random
[Math.random]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random
5 changes: 5 additions & 0 deletions exercises/concept/captains-log/.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/concept/captains-log/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"authors": [
"SneakyMallard"
],
"files": {
"solution": [
"captains-log.js"
],
"test": [
"captains-log.spec.js"
],
"exemplar": [
".meta/exemplar.js"
]
},
"blurb": "Learn about randomness and the Math.random() function while helping Mary generate stardates and starship registry numbers for her Star Trek roleplay."
}
26 changes: 26 additions & 0 deletions exercises/concept/captains-log/.meta/design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Design

## Goal

The goal of this exercise is to teach the student how to generate psuedorandom numbers in JavaScript.

## Learning objectives

- Know how to generate a random number with `Math.random()`
- Know how to generate a random number in a range.
- Know how to generate a random integer.

## Out of scope

- Details of pseudorandom number generation in general.
- Different algorithms for pseudorandom number generation.

## Concepts

The Concepts this exercise unlocks are:

- `randomness`: Know of the `Math.random()` function and know how to use it to generate random numbers.

## Prerequisites

- `numbers`: Know how numbers work in JavaScript. Know some number methods.
30 changes: 30 additions & 0 deletions exercises/concept/captains-log/.meta/exemplar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/// <reference path="./global.d.ts" />
// @ts-check

/**
* Generates a random starship registry number.
*
* @returns {string} the generated registry number.
*/
export function randomShipRegistryNumber() {
return 'NCC-' + Math.floor(1000 + Math.random() * 9000);
}

/**
* Generates a random stardate.
*
* @returns {number} a stardate between 41000 (inclusive) and 42000 (exclusive).
*/
export function randomStardate() {
return 41000 + Math.random() * 1000;
}

/**
* Generates a random planet class.
*
* @returns {string} a one-letter planet class.
*/
export function randomPlanetClass() {
const planetClasses = ['D', 'H', 'J', 'K', 'L', 'M', 'N', 'R', 'T', 'Y'];
return planetClasses[Math.floor(Math.random() * 10)];
}
1 change: 1 addition & 0 deletions exercises/concept/captains-log/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
audit=false
21 changes: 21 additions & 0 deletions exercises/concept/captains-log/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/concept/captains-log/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: [],
};
35 changes: 35 additions & 0 deletions exercises/concept/captains-log/captains-log.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/// <reference path="./global.d.ts" />
// @ts-check

/**
* Generates a random starship registry number.
*
* @returns {string} the generated registry number.
*/
export function randomShipRegistryNumber() {
throw new Error(
'Please remove this line and implement the randomShipRegistryNumber() function',
);
}

/**
* Generates a random stardate.
*
* @returns {number} a stardate between 41000 (inclusive) and 42000 (exclusive).
*/
export function randomStardate() {
throw new Error(
'Please remove this line and implement the randomStardate() function',
);
}

/**
* Generates a random planet class.
*
* @returns {string} a one-letter planet class.
*/
export function randomPlanetClass() {
throw new Error(
'Please remove this line and implement the randomStardate() function',
);
}
Loading