Skip to content

Commit e4262a0

Browse files
Modules exercises
1 parent a69f851 commit e4262a0

File tree

4 files changed

+32
-19
lines changed

4 files changed

+32
-19
lines changed

modules/exercises/ScoreCalcs/averages.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,7 @@ function averageForTest(testIndex,scores){
1717
}
1818

1919
//TODO: Export all functions within an object.
20+
module.exports = {
21+
averageForStudent: averageForStudent,
22+
averageForTest: averageForTest
23+
};

modules/exercises/display.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,7 @@ function printTestScores(index,test,students,scores){
3434
}
3535
return;
3636
}
37+
38+
module.exports = {
39+
printAll: printAll
40+
};

modules/exercises/index.js

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,37 @@
11
//Import modules:
2-
const input = //Import readline-sync.
3-
const averages = //Import functions from averages.js.
4-
const printAll = //Import function from display.js.
5-
const randomSelect = //Import function from randomSelect.js.
2+
const input = require('readline-sync'); //Import readline-sync.
3+
const averages = require('../exercises/ScoreCalcs/averages.js'); //Import functions from averages.js.
4+
const printAll = require('./display.js'); //Import function from display.js.
5+
const randomSelect = require('./randomSelect.js'); //Import function from randomSelect.js.
66

77
//Candidate data:
8-
let astronauts = ['Fox','Turtle','Cat','Hippo','Dog'];
8+
let astronauts = ['Fox', 'Turtle', 'Cat', 'Hippo', 'Dog'];
99

10-
const testTitles = ['Math','Fitness','Coding','Nav','Communication'];
10+
const testTitles = ['Math', 'Fitness', 'Coding', 'Nav', 'Communication'];
1111

12-
let scores = [[95, 86, 83, 81, 76],[79, 71, 79, 87, 72],[94, 87, 87, 83, 82],[99, 77, 91, 79, 80],[96, 95, 99, 82, 70]];
12+
let scores = [[95, 86, 83, 81, 76], [79, 71, 79, 87, 72], [94, 87, 87, 83, 82], [99, 77, 91, 79, 80], [96, 95, 99, 82, 70]];
1313

1414
//User interface:
15-
let prompts = ['display all scores', 'average the scores for each test', 'average the scores for each astronaut','select the next spacewalker'];
15+
let prompts = ['display all scores', 'average the scores for each test', 'average the scores for each astronaut', 'select the next spacewalker'];
1616

17-
for (let i = 0; i<prompts.length; i++){
17+
for (let i = 0; i < prompts.length; i++) {
1818
let response = input.question(`Would you like to ${prompts[i]}? Y/N: `);
19-
if (response.toLowerCase()==='y'){
20-
if (i===0){
19+
if (response.toLowerCase() === 'y') {
20+
if (i === 0) {
2121
//Call 'printAll' here and pass in all necessary arguments.
22-
} else if (i===1){
23-
for (let j = 0; j<testTitles.length; j++){
24-
let avg = //Call 'averageForTest' here. Pass in j and scores as arguments.
22+
printAll.printAll(astronauts, testTitles, scores);
23+
} else if (i === 1) {
24+
for (let j = 0; j < testTitles.length; j++) {
25+
let avg = averages.averageForTest(j, scores); //Call 'averageForTest' here. Pass in j and scores as arguments.
2526
console.log(`${testTitles[j]} test average = ${avg}%.`);
2627
}
27-
} else if (i===2){
28-
for (let j = 0; j<astronauts.length; j++){
29-
let avg = //Call 'averageForStudent' here. Pass in j and scores as arguments.
28+
} else if (i === 2) {
29+
for (let j = 0; j < astronauts.length; j++) {
30+
let avg = averages.averageForStudent(j, scores); //Call 'averageForStudent' here. Pass in j and scores as arguments.
3031
console.log(`${astronauts[j]}'s test average = ${avg}%.`);
3132
}
3233
} else {
33-
let walker = //Call 'randomSelect' to pick a spacewalker from the astronauts array.
34+
let walker = randomSelect.randomFromArray(astronauts); //Call 'randomSelect' to pick a spacewalker from the astronauts array.
3435
console.log(`${walker} is the next spacewalker.`);
3536
}
3637
} else {

modules/exercises/randomSelect.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
function randomFromArray(arr){
2-
//Your code here to select a random element from the array passed to the function.
2+
let index = Math.floor(Math.random()*arr.length);
3+
return arr[index];
34
}
45

56
//TODO: Export the randomFromArray function.
7+
module.exports = {
8+
randomFromArray: randomFromArray
9+
};

0 commit comments

Comments
 (0)