Advertisement
divanov94

Untitled

Dec 9th, 2020
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solveClasses() {
  2.     class Person {
  3.         constructor(firstName, lastName) {
  4.             this.firstName = firstName;
  5.             this.lastName = lastName;
  6.             this.problems = [];
  7.         }
  8.  
  9.         toString() {
  10.             return `${this.firstName} ${this.lastName} is part of SoftUni community now!`
  11.         }
  12.     }
  13.  
  14.     class Teacher extends Person {
  15.         constructor(firstName, lastName) {
  16.             super(firstName, lastName);
  17.         }
  18.  
  19.         createProblem(id, difficulty) {
  20.             let newProblem = {
  21.                 id,
  22.                 difficulty
  23.             }
  24.             this.problems.push(newProblem);
  25.             return this.problems;
  26.         }
  27.  
  28.         getProblem() {
  29.             return this.problems;
  30.         }
  31.  
  32.         showProblemSolution(id) {
  33.             let check = this.problems.find(p => p.id === id);
  34.             if (!check) {
  35.                 throw new Error(`Problem with id ${id} not found.`)
  36.  
  37.             } else {
  38.                 check.difficulty -= 1;
  39.                 return check;
  40.             }
  41.         }
  42.     }
  43.  
  44.     class Student extends Person {
  45.         constructor(firstName, lastName, graduationCredits, problems) {
  46.             super(firstName, lastName);
  47.             this.graduationCredits = graduationCredits;
  48.             this.myCredits = 0;
  49.             this.solvedProblems = [];
  50.             this.problems=problems;
  51.         }
  52.  
  53.         solveProblem(id) {
  54.             let check = this.problems.find(p => p.id === id);
  55.             if (!check) {
  56.                 throw new Error(`Problem with id ${id} not found.`)
  57.             } else if (!this.solvedProblems.includes(check)) {
  58.                 this.solvedProblems.push(check);
  59.                 this.myCredits += check.difficulty;
  60.             }
  61.  
  62.             return this.myCredits;
  63.         }
  64.  
  65.         graduate() {
  66.             if (this.myCredits >= this.graduationCredits) {
  67.                 return `${this.firstName} ${this.lastName} has graduated succesfully.`
  68.             } else {
  69.                 return `${this.firstName} ${this.lastName}, you need ${this.graduationCredits - this.myCredits} credits to graduate.`
  70.             }
  71.         }
  72.     }
  73.     return {
  74.         Person,
  75.         Teacher,
  76.         Student
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement