Exercise Solutions: Modules

Export Finished Modules

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

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

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.

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

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.

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

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