Advertisement
ralichka

Untitled

Dec 9th, 2020
518
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 problem = this.problems.find(x => x.id === id);
  21.  
  22.             if (!problem) {
  23.                 problem = {
  24.                     id,
  25.                     difficulty: Number(difficulty)
  26.                 }
  27.                 this.problems.push(problem);
  28.  
  29.             }
  30.             return this.problems;
  31.         }
  32.  
  33.         getProblems() {
  34.             return this.problems;
  35.         }
  36.  
  37.         showProblemSolution(id) {
  38.             let problem = this.problems.find(x => x.id === id);
  39.  
  40.             if (!problem) {
  41.                 throw new Error(`Problem with id ${id} not found.`);
  42.             } else {
  43.                 problem.difficulty--;
  44.                 return problem;
  45.             }
  46.  
  47.         }
  48.  
  49.     }
  50.  
  51.     class Student extends Person{
  52.         constructor( firstName, lastName, graduationCredits, problems ){
  53.             super(firstName,lastName);
  54.             this.graduationCredits = Number(graduationCredits);
  55.             this.myCredits = 0;
  56.             this.solvedProblems = [];
  57.             this.problems = problems;
  58.         }
  59.  
  60.         solveProblem(id){
  61.             let problem = this.problems.find(x => x.id === id);
  62.  
  63.             if(!problem){
  64.                 throw new Error(`Problem with id ${ id } not found.`);
  65.             } else{
  66.                 if(!this.solvedProblems.includes(problem)){
  67.                     this.myCredits += problem.difficulty;
  68.                     this.solvedProblems.push(problem);
  69.                 }
  70.             }
  71.             return this.myCredits;
  72.         }
  73.  
  74.         graduate(){
  75.             if(this.myCredits >= this.graduationCredits){
  76.                 return `${this.firstName} ${this.lastName} has graduated succesfully.`;
  77.             } else{
  78.                 let neededCredits = this.graduationCredits - this.myCredits;
  79.                 return `${this.firstName} ${this.lastName}, you need ${ neededCredits } credits to graduate.`;
  80.             }
  81.         }
  82.     }
  83.  
  84.     return {
  85.         Person,
  86.         Teacher,
  87.         Student
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement