Day 12: Inheritance hackerrank solution javascript 1

Day 12: Inheritance hackerrank solution javascript

// Day 12: Inheritance hackerrank solution javascript
class Student extends Person {
  /*
   *   Class Constructor
   *
   *   @param firstName - A string denoting the Person's first name.
   *   @param lastName - A string denoting the Person's last name.
   *   @param id - An integer denoting the Person's ID number.
   *   @param scores - An array of integers denoting the Person's test scores.
   */
  // Write your constructor here
  constructor(firstName, lastName, id, scores) {
    super(firstName, lastName, id);
    this.scores = scores;
  }
  /*
   *   Method Name: calculate
   *   @return A character denoting the grade.
   */
  // Write your method here

  calculate() {
    let sum = this.scores.reduce((a, b) => a + b);
    let average = sum / this.scores.length;

    if (average >= 90) {
      return "O";
    } else if (average >= 80) {
      return "E";
    } else if (average >= 70) {
      return "A";
    } else if (average >= 55) {
      return "P";
    } else if (average >= 40) {
      return "D";
    } else {
      return "T";
    }
  }
}

Here is what the above code is Doing:
1. We create a class called Student that extends the Person class.
2. We create a constructor that takes in 4 parameters: firstName, lastName, id, and scores.
3. We call the super constructor on the first 3 parameters to get the Person properties.
4. We set the scores property to the scores parameter.
5. We create a method called calculate that returns a character denoting the grade.
6. We create a variable called sum that sums up all the scores.
7. We create a variable called average that divides the sum by the number of scores.
8. We return the appropriate grade based on the average.

Similar Posts