Tracks
/
JavaScript
JavaScript
/
Exercises
/
Captain's Log
Captain's Log

Captain's Log

Learning Exercise

Introduction

Many programs need (pseudo-)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, which is why there are also "pseudorandom" generators.

Caution

The Math.random() function should NOT be used for security and cryptographic applications! Finish the learning exercise(s) about this concept to learn more

Generating random numbers

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

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 meets 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.

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).

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 randomPlanetClass() function. It should return one of the planetary classes at random.

randomPlanetClass();
// => "K"
Edit via GitHub The link opens in a new window or tab
JavaScript Exercism

Ready to start Captain's Log?

Sign up to Exercism to learn and master JavaScript with 37 concepts159 exercises, and real human mentoring, all for free.