Exercise Solutions: Modules

Export Finished Modules

  1. In averages.js, add code to export all of the functions within an object.

    1
    2
    3
    4
    module.exports = {
       averageForStudent: averageForStudent,
       averageForTest: averageForTest
    };
    

Code and Export a New Module

  1. Add code to complete the randomFromArray function. It should take an array as an argument and then return a randomly selected element from that array.

    1
    2
    3
    4
    function randomFromArray(arr){
       let index = Math.floor(Math.random()*arr.length);
       return arr[index];
    }
    

Import Required Modules

  1. Assign readline-sync to the input variable.

    const input = require('readline-sync');
    
  1. Assign the printAll function from display.js to the printAll variable.

    const printAll = require('./display.js');
    

Finish the Project

  1. Line 21 - Call printAll to display all of the tests and student scores. Be sure to pass in the correct arguments.

    21
    printAll(astronauts, testTitles, scores);
    
  1. Line 29 - Call averageForStudent (with the proper arguments) to print each astronaut's average score.

    29
    let avg = averages.averageForStudent(j, scores);