diff --git a/concepts/randomness/.meta/config.json b/concepts/randomness/.meta/config.json new file mode 100644 index 0000000000..a41a7ca37c --- /dev/null +++ b/concepts/randomness/.meta/config.json @@ -0,0 +1,5 @@ +{ + "blurb": "Random numbers can be generated with the Math.random() method in JavaScript.", + "authors": ["atj-craig"], + "contributors": [] +} \ No newline at end of file diff --git a/concepts/randomness/about.md b/concepts/randomness/about.md new file mode 100644 index 0000000000..ac17245737 --- /dev/null +++ b/concepts/randomness/about.md @@ -0,0 +1 @@ +provide information about the concept for a student who has completed the corresponding concept exercise to learn from and refer back to \ No newline at end of file diff --git a/concepts/randomness/introduction.md b/concepts/randomness/introduction.md new file mode 100644 index 0000000000..b0be8d3d76 --- /dev/null +++ b/concepts/randomness/introduction.md @@ -0,0 +1,26 @@ +# Introduction + +JavaScript provides a method on the Math object called `Math.random()` to create pseudo-random numbers. + +Math.random will return a number between 0 (inclusive) and 1 (exclusive). This means that 0 is a possible result, while 1 is not. To be clear, we can express this range as 0 to 0.999999... + +## How to create psuedo-random numbers + +To demonstrate how to use this method, here is a simple function that employs it: + +```javascript +function randomNumber() { + return Math.random(); +} +// => will return a number between 0 (inclusive) and 1 (exclusive) +``` + +## Creating pseudo-random integers + +## Seeds + +## Cryptographically secure random numbers + +// Links +Why Randomness is hard +What pseudo-random numbers are diff --git a/concepts/randomness/links.json b/concepts/randomness/links.json new file mode 100644 index 0000000000..f373b591da --- /dev/null +++ b/concepts/randomness/links.json @@ -0,0 +1 @@ +provide helpful links that provide more reading or information about a concept \ No newline at end of file diff --git a/config.json b/config.json index 1ea00310b0..45b06485cf 100644 --- a/config.json +++ b/config.json @@ -261,6 +261,14 @@ "concepts": ["classes"], "prerequisites": ["objects", "functions", "conditionals-ternary"], "status": "beta" + }, + { + "slug": "captains-log", + "name": "Captain's Log", + "uuid": "need-to-generate", + "concepts": ["randomness"], + "prerequisites": ["numbers"], + "status": "wip" } ], "practice": [ diff --git a/exercises/concept/captains-log/.docs/hints.md b/exercises/concept/captains-log/.docs/hints.md new file mode 100644 index 0000000000..8531df82b2 --- /dev/null +++ b/exercises/concept/captains-log/.docs/hints.md @@ -0,0 +1 @@ +Still need to start this \ No newline at end of file diff --git a/exercises/concept/captains-log/.docs/instructions.md b/exercises/concept/captains-log/.docs/instructions.md new file mode 100644 index 0000000000..0029cb5a71 --- /dev/null +++ b/exercises/concept/captains-log/.docs/instructions.md @@ -0,0 +1,42 @@ +# 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 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(min, max)` that returns a floating point number between 41000.0 (inclusive) and 42000.0 (exclusive). + +```javascript +randomStardate(); +// => 41458.851814302536 +``` + +## 2. 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 (inclusive). + +Implement the `randomShipRegistryNumber(min, max)` function that returns a random starship registry number. + +```javascript +randomShipRegistryNumber(); +// => "NCC-1947" +``` + +## 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 `randomPlanetClass()` function. It should return one of the planetary classes at random. + +```javascript +randomPlanetClass(); +// => "K" +``` diff --git a/exercises/concept/captains-log/.docs/introduction.md b/exercises/concept/captains-log/.docs/introduction.md new file mode 100644 index 0000000000..3d5c7a381f --- /dev/null +++ b/exercises/concept/captains-log/.docs/introduction.md @@ -0,0 +1 @@ +still need to do this \ No newline at end of file diff --git a/exercises/concept/captains-log/.eslintrc b/exercises/concept/captains-log/.eslintrc new file mode 100644 index 0000000000..1d4446029c --- /dev/null +++ b/exercises/concept/captains-log/.eslintrc @@ -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" + } + ] +} diff --git a/exercises/concept/captains-log/.gitignore b/exercises/concept/captains-log/.gitignore new file mode 100644 index 0000000000..bdb912f98a --- /dev/null +++ b/exercises/concept/captains-log/.gitignore @@ -0,0 +1,3 @@ +node_modules +yarn-error.log + diff --git a/exercises/concept/captains-log/.meta/config.json b/exercises/concept/captains-log/.meta/config.json new file mode 100644 index 0000000000..1485333cc8 --- /dev/null +++ b/exercises/concept/captains-log/.meta/config.json @@ -0,0 +1,11 @@ +{ + "blurb": "Practice randomness with -----", + "authors": ["atj-craig"], + "contributors": [], + "files": { + "solution": ["captains-log.js"], + "test": ["captains-log.spec.js"], + "exemplar": [".meta/exemplar.js"] + }, + "forked_from": [] +} \ No newline at end of file diff --git a/exercises/concept/captains-log/.meta/design.md b/exercises/concept/captains-log/.meta/design.md new file mode 100644 index 0000000000..3d5c7a381f --- /dev/null +++ b/exercises/concept/captains-log/.meta/design.md @@ -0,0 +1 @@ +still need to do this \ No newline at end of file diff --git a/exercises/concept/captains-log/.meta/exemplar.js b/exercises/concept/captains-log/.meta/exemplar.js new file mode 100644 index 0000000000..fae0c49cba --- /dev/null +++ b/exercises/concept/captains-log/.meta/exemplar.js @@ -0,0 +1,33 @@ +// @ts-check + +/** + * Generates a random stardate + * + * @param {number} min + * @param {number} max + * @returns {number} a random number between min (inclusive) and max (exclusive) + */ +export function randomStardate(min, max) { + return Math.random() * (max - min) + min; +} + +/** + * Generates a starship registry number + * + * @param {number} min + * @param {number} max + * @returns {string} starting with "NCC-" followed by a random integer between min (inclusive) and max (inclusive) + */ +export function randomShipRegistryNumber(min, max) { + return "NCC-" + Math.floor(Math.random() * (max - min + 1) + min); +} + +/** + * Generates a random planet class + * + * @param {array} classes an array of strings filled with the classes of planets + * @returns {string} a single random item from the classes parameter + */ +export function randomPlanetClass(classes) { + return classes[Math.floor(Math.random() * classes.length)]; +} diff --git a/exercises/concept/captains-log/.npmrc b/exercises/concept/captains-log/.npmrc new file mode 100644 index 0000000000..d26df800bb --- /dev/null +++ b/exercises/concept/captains-log/.npmrc @@ -0,0 +1 @@ +audit=false diff --git a/exercises/concept/captains-log/LICENSE b/exercises/concept/captains-log/LICENSE new file mode 100644 index 0000000000..90e73be03b --- /dev/null +++ b/exercises/concept/captains-log/LICENSE @@ -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. diff --git a/exercises/concept/captains-log/TO-DO b/exercises/concept/captains-log/TO-DO new file mode 100644 index 0000000000..7295364ee1 --- /dev/null +++ b/exercises/concept/captains-log/TO-DO @@ -0,0 +1,42 @@ +To-do: + /javascript + /concepts + /randomness + /.meta + config.json + - mostly done + about.md + - not started + introduction.md + - started but wondering about the balance between showing students what they need VS getting them to learn in the exercise + links.json + - not started + /exercises + /concept + /captains-log + /.docs + hints.md + - not done + instructions.md + - copied from other exercise + introduction.md + - not done + /.meta + config.json + - semi-done + design.md + - not done + exemplar.js + - done + captains-log.js + - mostly done + captains-log.spec.js + - mostly done + - note: Testing for randomness proved a bit tricky. I went for a simple solution that runs the functions multiple times and tests to see that the results are random by seeing if different outputs would be made. It's not perfect. I had to adjust the number of times the tests are run, otherwise there can be false positive / false negative results. The current settings should be working, but please do test this a few times to verify it. + LICENSE + - standard + package.json + - mostly done + config.json + - generate UUID + - see if additional prereq's are needed \ No newline at end of file diff --git a/exercises/concept/captains-log/babel.config.js b/exercises/concept/captains-log/babel.config.js new file mode 100644 index 0000000000..420ea5973b --- /dev/null +++ b/exercises/concept/captains-log/babel.config.js @@ -0,0 +1,15 @@ +module.exports = { + presets: [ + [ + '@babel/preset-env', + { + targets: { + node: 'current', + }, + useBuiltIns: 'entry', + corejs: '3.19', + }, + ], + ], + plugins: ['@babel/plugin-syntax-bigint'], +}; diff --git a/exercises/concept/captains-log/captains-log.js b/exercises/concept/captains-log/captains-log.js new file mode 100644 index 0000000000..3c9f29c400 --- /dev/null +++ b/exercises/concept/captains-log/captains-log.js @@ -0,0 +1,46 @@ +// @ts-check + +/** + * Generates a random stardate + * + * @param {number} min + * @param {number} max + * @returns {number} a random number between min (inclusive) and max (exclusive) + */ +export function randomStardate(min, max) { + // return Math.random() * (max - min) + min; + return 1000000000000; + // throw new Error('Implement the randomStardate function'); +} +// console.log(randomStardate(1, 2)) + +/** + * Generates a starship registry number + * + * @param {number} min + * @param {number} max + * @returns {string} starting with "NCC-" followed by a random integer between min (inclusive) and max (inclusive) + */ +export function randomShipRegistryNumber(min, max) { + return "NCC-" + Math.floor(Math.random() * (max - min + 1) + min); + // throw new Error('Implement the randomShipRegistryNumber function'); +} + +// integer test +// see if there is a decimal in the result + +// inclusive test +// console.log(randomShipRegistryNumber(0, 1)) +// => will return NCC-0 or NCC-1 if working. +// => will return NCC-0 if not inclusive + +/** + * Generates a random planet class + * + * @param {array} classes an array of strings filled with the classes of planets + * @returns {string} a single random item from the classes parameter + */ +export function randomPlanetClass(classes) { + return classes[Math.floor(Math.random() * classes.length)]; + // throw new Error('Implement the randomPlanetClass function'); +} diff --git a/exercises/concept/captains-log/captains-log.spec.js b/exercises/concept/captains-log/captains-log.spec.js new file mode 100644 index 0000000000..5b6edb117e --- /dev/null +++ b/exercises/concept/captains-log/captains-log.spec.js @@ -0,0 +1,81 @@ +import { randomStardate, randomShipRegistryNumber, randomPlanetClass } from './captains-log'; + +describe('randomStardate', () => { + test('returns a non-integer number', () => { + const min = 1; + const max = 2; + const result = randomStardate(min, max); + expect(typeof result).toBe('number'); + expect(Number.isInteger(result)).toBe(false); + }); + + test('returns a number between 41000 and 42000', () => { + const min = 41000; + const max = 42000; + const result = randomStardate(min, max); + expect(result).toBeGreaterThanOrEqual(min); + expect(result).toBeLessThan(max); + }); + + test('returns a random number', () => { + const min = 1; + const max = 1000; + const resultArray = []; + for (let i = 0; i < 10; i++) { + resultArray.push(randomStardate(min, max)); + } + expect(resultArray).toEqual(expect.not.arrayContaining([randomStardate(min, max)])); + }); +}); + +//type needs to be string +describe('randomShipRegistryNumber', () => { + const min = 1000; + const max = 9999; + const regexNumber = /(?<=NCC\-).+/; + const regexFull = /^(NCC\-)(\d\d\d\d)$/; + + test('returns the correct format of NCC-####', () => { + const result = randomShipRegistryNumber(min, max).match(regexFull); + expect(result).not.toBe(null); + }); + + test('number after NCC- is random', () => { + const resultArray = []; + for (let i = 0; i < 10; i++) { + resultArray.push(randomShipRegistryNumber(min, max).match(regexNumber)[0]) + } + expect(resultArray).toEqual(expect.not.arrayContaining([randomShipRegistryNumber(min, max).match(regexNumber)[0]])) + }); + + test('returns the max value', () => { + const resultArray = []; + for (let i = 0; i < 10; i++) { + resultArray.push(randomShipRegistryNumber(0, 1).match(regexNumber)[0]) + } + expect(resultArray).toEqual(expect.arrayContaining(["1"])) + }); +}); + +describe('randomPlanetClass', () => { + const planetClasses = ["D", "H", "J", "K", "L", "M", "N", "R", "T", "Y"]; + const regex = /^\w$/; + + test('returns a string with a single character', () => { + const result = typeof randomPlanetClass(planetClasses).match(regex)[0]; + expect(result).toBe("string") + }); + + test('when repeated, will return the entire array', () => { + const expected = planetClasses.sort().join(''); + const resultArray = []; + for (let i = 0; i < 100; i++) { + const item = randomPlanetClass(planetClasses).match(regex)[0]; + if (!resultArray.includes(item)) { + resultArray.push(item) + } + } + const result = resultArray.sort().join(''); + expect(result).toEqual(expected); + }); +}); diff --git a/exercises/concept/captains-log/package.json b/exercises/concept/captains-log/package.json new file mode 100644 index 0000000000..d6847461bb --- /dev/null +++ b/exercises/concept/captains-log/package.json @@ -0,0 +1,31 @@ +{ + "name": "@exercism/javascript-randomness", + "description": "Exercism concept exercise on randomness", + "author": "Andrew Craig", + "private": true, + "license": "MIT", + "repository": { + "type": "git", + "url": "https://github.com/exercism/javascript", + "directory": "exercises/concept/captains-log" + }, + "devDependencies": { + "@babel/cli": "^7.16.8", + "@babel/core": "^7.16.7", + "@babel/plugin-syntax-bigint": "^7.8.3", + "@babel/preset-env": "^7.16.8", + "@exercism/eslint-config-javascript": "^0.5.0", + "@types/jest": "^26.0.24", + "@types/node": "^16.11.20", + "babel-jest": "^27.4.6", + "core-js": "^3.20.3", + "eslint": "^7.32.0", + "jest": "^26.6.3" + }, + "dependencies": {}, + "scripts": { + "test": "jest --no-cache ./*", + "watch": "jest --no-cache --watch ./*", + "lint": "eslint ." + } +}