|
1 | | -//Declare a class called CrewCandidate with a constructor that takes three parameters—name, mass, and scores. Note that scores will be an array of test results. |
2 | 1 | class CrewCandidate { |
3 | 2 | constructor(name, mass, scores) { |
4 | 3 | this.name = name; |
5 | | - this.mass = name; |
| 4 | + this.mass = mass; |
6 | 5 | this.scores = scores; |
7 | 6 | } |
| 7 | + |
| 8 | + addScore(newScore) { |
| 9 | + this.scores.push(newScore); |
| 10 | + } |
| 11 | + |
| 12 | + average() { |
| 13 | + let sum = 0; |
| 14 | + for (let i = 0; i < this.scores.length; i++) { |
| 15 | + sum += this.scores[i]; |
| 16 | + } |
| 17 | + const avg = sum / this.scores.length; |
| 18 | + return Math.round(avg * 10) / 10; |
| 19 | + } |
| 20 | + |
| 21 | + status() { |
| 22 | + let avgScore = this.average(); |
| 23 | + if (avgScore >= 90) { |
| 24 | + return "Accepted"; |
| 25 | + } else if (avgScore >= 80) { |
| 26 | + return "Reserve"; |
| 27 | + } else if (avgScore >= 70) { |
| 28 | + return "Probationary"; |
| 29 | + } else { |
| 30 | + return "Rejected"; |
| 31 | + } |
| 32 | + } |
8 | 33 | } |
| 34 | + |
9 | 35 | let bubbaBear = new CrewCandidate("Bubba Bear", 135, [88, 85, 90]); |
10 | 36 | let merryMaltese = new CrewCandidate("Merry Maltese", 1.5, [93, 88, 97]); |
11 | 37 | let gladGator = new CrewCandidate("Glad Gator", 225, [75, 78, 62]); |
12 | 38 |
|
13 | | -console.log("Bubba Bear:", bubbaBear); |
14 | | -console.log("Merry Maltese:", merryMaltese); |
15 | | -console.log("Glad Gator:", gladGator); |
| 39 | +function calculateTestsForStatusUpgrade(candidate, upgradeStatus) { |
| 40 | + let testsAdded = 0; |
| 41 | + while (candidate.status() !== upgradeStatus) { |
| 42 | + candidate.addScore(100); |
| 43 | + testsAdded++; |
| 44 | + if (candidate.status() === upgradeStatus) break; |
| 45 | + } |
| 46 | + return testsAdded; |
| 47 | +} |
| 48 | + |
| 49 | +let testsForReserve = calculateTestsForStatusUpgrade(gladGator, "Reserve"); |
| 50 | +let gladGatorReserveStatus = gladGator.status(); |
| 51 | +let testsForAccepted = calculateTestsForStatusUpgrade(gladGator, "Accepted"); |
16 | 52 |
|
17 | | -//Add methods for adding scores, averaging scores and determining candidate status as described in the studio activity. |
| 53 | +console.log(`Tests needed for Glad Gator to reach Reserve: ${testsForReserve}`); |
| 54 | +console.log( |
| 55 | + `Additional tests needed for Glad Gator to reach Accepted: ${testsForAccepted}` |
| 56 | +); |
18 | 57 |
|
19 | | -//Part 4 - Use the methods to boost Glad Gator’s status to Reserve or higher. How many tests will it take to reach Reserve status? How many to reach Accepted? Remember, scores cannot exceed 100%. |
| 58 | +[bubbaBear, merryMaltese, gladGator].forEach((candidate) => { |
| 59 | + console.log( |
| 60 | + `${ |
| 61 | + candidate.name |
| 62 | + } earned an average test score of ${candidate.average()}% and has a status of ${candidate.status()}.` |
| 63 | + ); |
| 64 | +}); |
0 commit comments