-
-
Notifications
You must be signed in to change notification settings - Fork 639
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
base: main
Are you sure you want to change the base?
Conversation
Stub for new Concept exercise
Still need an uuid, no idea how to generate one
Added stub file's comments for task exercism#1
Added stub file's comments for task exercism#3
Hello. Thanks for opening a PR on Exercism 🙂 We ask that all changes to Exercism are discussed on our Community Forum before being opened on GitHub. To enforce this, we automatically close all PRs that are submitted. That doesn't mean your PR is rejected but that we want the initial discussion about it to happen on our forum where a wide range of key contributors across the Exercism ecosystem can weigh in. You can use this link to copy this into a new topic on the forum. If we decide the PR is appropriate, we'll reopen it and continue with it, so please don't delete your local branch. If you're interested in learning more about this auto-responder, please read this blog post. Note: If this PR has been pre-approved, please link back to this PR on the forum thread and a maintainer or staff member will reopen it. |
If you've used configlet, which is our tool for managing tracks and things like generating exercices, then all the needed files should be auto-generated and you shouldn't need to manually attach a UUID, but in case you do configlet can also do that. Also worth checking out our (slightly outdated) CONTRIBUTING guide. |
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You probably want to use toMatch here.
test('stardates are valid',() => { | ||
expect(41000<=randomStardate()<42000).toBe(true); | ||
expect(41000<=randomStardate()<42000).toBe(true); | ||
expect(41000<=randomStardate()<42000).toBe(true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would have liked to see repeated testing like this in a for
loop or something similar, so you can avoid repetition and we can test an arbitrary amount of times, because randomness is tricky.
Additionally, this - (41000<=randomStardate()<42000)
is not valid JS syntax (technically it is but it doesn't work like in Python), because chained comparison isn't supported like that. You'll need something like (41000 <= stardate && stardate < 42000)
instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this will work:
function loadDie(...values) {
const originalRandom = Math.random()
Math.random = function loadedDie {
if (values.length === 0) {
return originalRandom();
}
return values.shift();
}
return () => {
Math.random = originalRandom;
}
}
describe('randomStardate',() => {
test('stardate is between 41000 and 42000',() => {
const restore = loadDie(
0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1,
0.5, 0.5, 0.5, 0.5, 0.5, 0.5
);
// 6 * 0, 6 * 1, 6 * 0.5 and everything else random
for (let i = 0; i < 10_000; i++) {
const starDate = randomStardate()
expect(starDate).toBeGreaterThanOrEqual(41_000);
expect(starDate).toBeLessThan(42_000);
}
restore();
}
}
This should run in ~ 0.1 ms
Errors in tests fixed, and for loops now used. Just a question - is there a toInclude method? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(I wrote an entire thesis on how to calculate the expected number of rolls to get all the values in the planet class thing, but when I tried to submit it I pressed back accidentally and then lost it all. Will write it again soon...)
test('stardates are valid',() => { | ||
expect(41000<=randomStardate()<42000).toBe(true); | ||
expect(41000<=randomStardate()<42000).toBe(true); | ||
expect(41000<=randomStardate()<42000).toBe(true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this will work:
function loadDie(...values) {
const originalRandom = Math.random()
Math.random = function loadedDie {
if (values.length === 0) {
return originalRandom();
}
return values.shift();
}
return () => {
Math.random = originalRandom;
}
}
describe('randomStardate',() => {
test('stardate is between 41000 and 42000',() => {
const restore = loadDie(
0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1,
0.5, 0.5, 0.5, 0.5, 0.5, 0.5
);
// 6 * 0, 6 * 1, 6 * 0.5 and everything else random
for (let i = 0; i < 10_000; i++) {
const starDate = randomStardate()
expect(starDate).toBeGreaterThanOrEqual(41_000);
expect(starDate).toBeLessThan(42_000);
}
restore();
}
}
This should run in ~ 0.1 ms
|
This PR adds
hints.md
,introduction.md
,instructions.md
,config.json
,design.md
,exemplar.js
,captains-log.js
, andcaptains-log.spec.js
to a new exercise on the JavaScript track.A couple of questions to a reviewer:
.gitignore, .npmrc, LICENSE, package.json, jest.config.js, global.d.ts, eslint.config.mjs, and babel.config.js. These are not mentioned in the docs though. Should I add these, or does that happen automatically somehow?