satyacode
Background
Back to js-conditionals

03-grade-calculator.js

JavaScript
1/**
2 * 📝 Ms. Parker's Report Cards
3 *
4 * Ms. Parker teaches 8th-grade science and needs help converting
5 * percentage scores into letter grades for report cards. She also
6 * rewards students who earned extra credit by adding 5 bonus points
7 * to their score — but the final score can never go above 100.
8 *
9 * Grading Scale:
10 *   - 90–100 → "A"
11 *   - 80–89  → "B"
12 *   - 70–79  → "C"
13 *   - 60–69  → "D"
14 *   - 0–59   → "F"
15 *
16 * Rules:
17 *   - Check validity FIRST: if the original score is less than 0
18 *     or greater than 100, return "INVALID"
19 *   - If hasExtraCredit is true, add 5 points AFTER validation
20 *     (cap the result at 100)
21 *   - Then determine the letter grade from the adjusted score
22 *
23 * @param {number} score - The student's percentage score (0-100)
24 * @param {boolean} hasExtraCredit - Whether the student has extra credit
25 * @returns {string} The letter grade or "INVALID"
26 */
27export function calculateGrade(score, hasExtraCredit) {
28  
29    if(score < 0 || score > 100){
30      return "INVALID"
31    }
32
33  if(hasExtraCredit){
34    score += 5
35  }
36
37  let finalScore;
38  
39
40  if(score > 100){
41    score = 100
42  }
43
44  if( score >= 90){
45    finalScore = "A"
46  }else if(score >= 80) {
47    finalScore = "B"
48  }else if(score >= 70){
49    finalScore = "C"
50  }else if(score >= 60){
51    finalScore = "D"
52  }else {
53    finalScore = "F"
54  }
55 return finalScore
56}
57