-
-
Notifications
You must be signed in to change notification settings - Fork 654
Add Concept Exercise for 'randomness': Captain's Log #2683
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 12 commits
Commits
Show all changes
52 commits
Select commit
Hold shift + click to select a range
5f5dee3
Create captains-log.js
quintuple-mallard 784ab06
Create captains-log.spec.js
quintuple-mallard 839ff5d
Create config.json
quintuple-mallard 9660cf4
Rename config.json to config.json
quintuple-mallard aeaa04e
Create design.md
quintuple-mallard fe7da9a
Create exemplar.js
quintuple-mallard 365d8cc
Update exemplar.js
quintuple-mallard 580e1a9
Update exemplar.js
quintuple-mallard 77af0d8
Update exemplar.js
quintuple-mallard f2a7ef8
Create hints.md
quintuple-mallard 91e048f
Create instructions.md
quintuple-mallard 5ba9ac2
Create introduction.md
quintuple-mallard 1de010d
Modified tests in captains-log.spec.js
quintuple-mallard c334b74
Update captains-log.spec.js
quintuple-mallard b9d179f
Update config.json
quintuple-mallard 06a5d05
Update captains-log.spec.js
quintuple-mallard 87ef708
Removed extra parenthesis
quintuple-mallard a84402b
Added captains-log to config.json
quintuple-mallard a249a06
Update captains-log.spec.js
quintuple-mallard ef7aa46
Update config.json
quintuple-mallard 5fbe8cf
Update captains-log.spec.js
quintuple-mallard 9a07643
Added randomness to concepts list
quintuple-mallard eac6873
Rename docs to .docs
quintuple-mallard d9adf15
Rename instructions.md to instructions.md
quintuple-mallard b068bfc
Rename introduction.md to introduction.md
quintuple-mallard fb945a5
Merge branch 'exercism:main' into main
quintuple-mallard f03e1e7
Update config.json
quintuple-mallard 47f5e27
Update config.json
quintuple-mallard b3cd46f
Create config.json in concepts
quintuple-mallard d2436ae
Rename concepts/randomness/config.json to concepts/randomness/.meta/c…
quintuple-mallard 6d25bfb
Create about.md
quintuple-mallard 9c2a849
Create introduction.md
quintuple-mallard 3779474
Update config.json
quintuple-mallard 855ccd2
Create links.json
quintuple-mallard fe381c7
Update config.json
quintuple-mallard 173333d
Update links.json
quintuple-mallard f946ae5
Update exercises/concept/captains-log/captains-log.spec.js
quintuple-mallard 0cdd9ac
Fix tests
SleeplessByte ac1d8da
Format 12 files
quintuple-mallard 2d17cba
Fix broken test
quintuple-mallard 88e02f2
Merge branch 'exercism:main' into main
quintuple-mallard 8c7d877
Create .gitignore
quintuple-mallard 4d943eb
Create .npmrc
quintuple-mallard 8bdd28b
Create LICENSE
quintuple-mallard 65b9982
Create babel.config.js
quintuple-mallard a786e47
Create eslint.config.mjs
quintuple-mallard e15e862
Create jest.config.js
quintuple-mallard 8e45315
Create package.json
quintuple-mallard 10f181e
Add line break in exercises/concept/captains-log/.docs/instructions.md
quintuple-mallard 4c3e747
Added line breaks to instructions.md
quintuple-mallard 1fd3f14
Apply suggestions from code review
SleeplessByte eebe50a
Format
SleeplessByte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,22 @@ | ||
| { | ||
| "authors": [ | ||
| "SneakyMallard" | ||
| ], | ||
| "contributors": [], | ||
| "forked-from": ["java/captains-log"], | ||
| "concept": [ | ||
| { | ||
| "uuid": "need-to-generate", | ||
| "slug": "captains-log", | ||
| "name": "Captain's Log", | ||
| "concepts": ["randomness"], | ||
| "prerequisites": ["numbers", "arithmetic-operators"] | ||
| } | ||
| ], | ||
| "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." | ||
| } | ||
This file contains hidden or 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 @@ | ||
| # 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. |
This file contains hidden or 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 @@ | ||
| /// <reference path="./global.d.ts" /> | ||
SleeplessByte marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // @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)] | ||
| } | ||
This file contains hidden or 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 @@ | ||
| /// <reference path="./global.d.ts" /> | ||
SleeplessByte marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // @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"); | ||
| } | ||
This file contains hidden or 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,33 @@ | ||
| import { describe, expect, test } from '@jest/globals'; | ||
| import { randomShipRegistryNumber, randomStardate, randomPlanetClass } from './captains-log'; | ||
| describe('randomShipRegistryNumber',() => { | ||
| test('registry numbers are valid',() => { | ||
| expect(randomShipRegistryNumber().test(/NCC-[1-9][0-9]{3}/)).toBe(true); | ||
| expect(randomShipRegistryNumber().test(/NCC-[1-9][0-9]{3}/)).toBe(true); | ||
| expect(randomShipRegistryNumber().test(/NCC-[1-9][0-9]{3}/)).toBe(true); | ||
SleeplessByte marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }); | ||
| test('returns a random registry number',() => { | ||
| expect(randomShipRegistryNumber()).not.toEqual(randomShipRegistryNumber()) | ||
| }); | ||
| }); | ||
| describe('randomStardate',() => { | ||
| test('stardates are valid',() => { | ||
| expect(41000<=randomStardate()<42000).toBe(true); | ||
| expect(41000<=randomStardate()<42000).toBe(true); | ||
| expect(41000<=randomStardate()<42000).toBe(true); | ||
SleeplessByte marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }); | ||
| test('returns a random stardate',() => { | ||
| expect(randomStardate()).not.toEqual(randomStardate()) | ||
| }); | ||
| }); | ||
| describe('randomPlanetClass', () => { | ||
| test('planet classes are valid', () => { | ||
| const planetClasses = ['D', 'H', 'J', 'K', 'L', 'M', 'N', 'R', 'T', 'Y']; | ||
| expect(planetClasses.includes(randomPlanetClass())).toBe(true) | ||
| expect(planetClasses.includes(randomPlanetClass())).toBe(true) | ||
| expect(planetClasses.includes(randomPlanetClass())).toBe(true) | ||
| }); | ||
| test('returns a random planet class',() => { | ||
| expect(randomPlanetClass()).not.toEqual(randomPlanetClass()) | ||
| }); | ||
| }); | ||
This file contains hidden or 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,11 @@ | ||
| # 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 |
This file contains hidden or 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,37 @@ | ||
| ## 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" | ||
| ``` |
This file contains hidden or 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,35 @@ | ||
| # 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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.