Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
86 lines (72 loc) · 2.52 KB
/
Copy pathscript.js
File metadata and controls
86 lines (72 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
GRADEBOOK APP
Step 1 = Identifying the building blocks
getAverage
getGrade
hasPassingGrade
studentMsg
Think of each function as a lone worker
then they'll need to ebe combined together to make the app work
*/
function getAverage(scores) {
let total = 0; // need a variable that can store the total value
if(scores.length == 0){
return 0;
}
for(let i = 0; i < scores.length; i++) { // the for loop is the choice
total += scores[i];
// need to accumulate to find the sum of all values
// and the scores[i] (calculated each iteration)
}
let average = total / scores.length;
// total = sum of all values
// scores.length = number of the values
return average; // return of the average score
};
// getGrade - the order of the grades matters
// from the A case to the F case
function getGrade(score) {
// decision making - the if/else statement
if(score == 100) {
return "A+";
} else if(score >= 90 && score <= 99) {
return "A";
} else if(score >= 80 && score <= 89) {
return "B";
} else if(score >= 70 && score <= 79) {
return "C";
} else if(score >= 60 && score <= 69) {
return "D";
} else { // because everything else has being calculated
// there's no need of a condition here
return "F";
}
};
function hasPassingGrade(score) {
// a passing grade is anything different from F
// so instead of asking if they passed, I'll ask if they failed
if(getGrade(score) != "F"){
return true;
} else {
return false;
}
}; // it uses getGrade to find its value
// because the system already gives me the grade
// I can rely on the outputs only (and not comparing them)
function studentMsg(scores, studentScore) {
// scores = all the class scores
// studentScore = the individual student score
let average = getAverage(scores);
// the average variable is defined in getAverage but it exists only in that function
// so in this way I'm defyining it here too
let grade = getGrade(studentScore); // similar to the previous
// I'm reusing the function rather than re-calculating/comparing values
if(!hasPassingGrade(studentScore)){
return `Class average: ${average}.
Your grade: ${grade}. You failed the course.`
} else {
return `Class average: ${average}.
Your grade: ${grade}. You passed the course.`
}; // I've tried string concatenation but this way the code looks cleaner
}
console.log(studentMsg([92, 88, 12, 77, 57, 100, 67, 38, 97, 89], 37));