Skip to content

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

Open
wants to merge 14 commits into
base: main
Choose a base branch
from

Conversation

quintuple-mallard
Copy link

This PR adds hints.md,introduction.md, instructions.md, config.json, design.md, exemplar.js, captains-log.js, and captains-log.spec.js to a new exercise on the JavaScript track.

A couple of questions to a reviewer:

  • The other exercises have these files:
    .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?
  • How do I generate a UUID?

Copy link
Contributor

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.

@github-actions github-actions bot closed this Jun 18, 2025
@Cool-Katt Cool-Katt reopened this Jun 18, 2025
@Cool-Katt
Copy link
Contributor

Cool-Katt commented Jun 18, 2025

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);
Copy link
Contributor

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);
Copy link
Contributor

@Cool-Katt Cool-Katt Jun 18, 2025

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.

Copy link
Member

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

@quintuple-mallard
Copy link
Author

Errors in tests fixed, and for loops now used.

Just a question - is there a toInclude method?

Copy link
Member

@SleeplessByte SleeplessByte left a 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);
Copy link
Member

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

@SleeplessByte
Copy link
Member

Just a question - is there a toInclude method?

Yes: toContain(item)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants