a02 Functions and modules

DUE: 2022-02-11T00:00:00-5:00

GitHub Classroom Assignment Invite: https://classroom.github.com/a/4U2bhooL

Issues: https://github.com/comp426-2022-spring/a02/issues

NOTE: As of 2022-02-03, the autograder for this assignment is not fullly configured. It will tell you this when you push. Give the staff a couple of days to finish configuring it. You don’t have to do anything other than push to your assignment repo. When the grader is ready, it will update and spit out feedback.

Write some functions and make a module in Node.js

In this assignment, you are going to write some functions that emulate a simple random coin flip and then do things with the resulting information. The focus of the assignment is not necessarily on the coin flipping, but on how to work with functions to manipulate information into usable formats.

The instructions for this are very sparse because I want you to be creative in how you approach this. There are a few things that you will have to do very specifically in order to make this work, but beyond that, HOW you do this is really up to you.

Set up your environment

As usual, we’re starting with a kind of empty repository.

I’ve given you a LICENSE file, a README, and a script called flip.js.

I have also given you a directory modules with one file in it.

Run npm init to initialize your repository as a Node package.

Fill in the information as usual and make sure that the license matches the one in the repository.

Once you have done that, you should have a package.json file that looks something like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
{
  "name": "coinflip",
  "version": "0.0.1",
  "description": "This is a simple package that emulates coin flips in several different ways.",
  "main": "flip.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "John D. Martin III",
  "license": "GPL-3.0-or-later"
}

Because of some peculiarities in the way that Node handles modules, exports, and imports, we need to add a line to package.json to make it work. So, let’s do that right now.

Add "type": "module" somewhere. It doesn’t matter where: just make sure that you keep the JSON syntax intact (mind the commas).

After that, we want to add a few scripts, because you are going to be creating several script files. This way we can use NPM to invoke them if we want.

Add the following under "scripts": {:

    "flip": "node flip.js",
	"flips": "node flips.js",
	"guess-flip": "node guess-flip.js", 

You can edit the test script line later if you want to develop a test for your scripts (you should do this).

Your file should now look like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
{
  "name": "coinflip",
  "version": "0.0.1",
  "description": "This is a simple package that emulates coin flips in several different ways.",
  "main": "flip.js",
  "scripts": {
    "flip": "node flip.js",
    "flips": "node flips.js",
    "guess": "node guess-flip.js", 
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "John D. Martin III",
  "license": "GPL-3.0-or-later",
  "type": "module"
}

Write some functions

Open up ./modules/coin.mjs

This file has the .mjs extension to identify it as a module, but it could also just be .js. Both work in this case. You just have to make sure when you are referring to a module file that your reference matches the actual name of the file. See this for more information about module extensions: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules#aside_%E2%80%94_.mjs_versus_.js

Inside, you should see some named functions and some comments with instructions. Your task is to follow the instructions and fill in the function definitions with expressions/statements that take the specified parameters and return the speficied returns.

You will also need to export all of the functions in that file. There are several ways to do this. See the following and choose a method: https://developer.mozilla.org/en-US/docs/web/javascript/reference/statements/export

Write a script

I’ve given you a template for one script. It is pretty straightforward.

When you run node flip.js, in a terminal, you should see either “heads” or “tails” in STDOUT.

So, how do we do that?

First, we have to import one of the functions that we just wrote.

Again, there are a number of ways to do this, so start here and identify one that will work for you: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import

Once your function is imported, use console.log() to get the returned output from your coinFlip() function into STDOUT.

Et voila!

Test your script

Since we set npm up to be able to run our script, you should be able to run npm flip and see the output in STDOUT as either “heads” or “tails.”

Write some more scripts

To complete the assignment, you will need two more scripts, detailed below:

Flip many coins

Create another script file called flips.js.

This script should do the following:

  1. Take one argument on the command line named --number.
  2. If no argument is given, it should flip once by default.
  3. The result on STDOUT should be an array of all of the flips in sequence followed by an object summarizing the flips.

So the script should run like this and have similar output

$ node flips.js --number=10
[
  'heads', 'heads',
  'heads', 'tails',
  'heads', 'tails',
  'tails', 'heads',
  'tails', 'heads'
]
{ tails: 5, heads: 5 }

Try your luck

Create one more script called guess-flip.js.

This script should do the following:

  1. Take one argument on the command line named --call that will only accept .
  2. If no argument is given, or something other than “heads” or “tails” is passed with --call, return an error message (see below for example).
  3. Compare the call with a flip and report the result to STDOUT (see below)

The script should run like this and have similar output.

$ node guess-flip.js 
Error: no input.
Usage: node guess-flip --call=[heads|tails]

$ node guess-flip.js --call=tails
{ call: 'tails', flip: 'heads', result: 'lose' }

$ node guess-flip.js --call=tails
{ call: 'tails', flip: 'tails', result: 'win' }

Laundry list

This is what you should have upon completing the assignment:

  1. A repo set up as a Node package that can handle a module and has three npm helper scripts defined for your scripts.
  2. A module file in ./modules/ that has several functions that emulate coin flips.
  3. Three script files that do the following:
    1. flip.js - flips a coin into STDOUT
    2. flips.js - flips a bunch of coins into STDOUT based on a number input as a command line argument
    3. guess-flip.js - takes an argument calling a coin flip and then testing it against a flip and puts the results into STDOUT

Good luck!

PS

You may run into some errors related to using both ESM and CommonJS methods for importing/requiring dependencies. Check out the links from w03 for help working through those errors.