과제1. 20점
위 코드는 학생의 수업들을 입력받아 졸업평점을 계산하는 코드다. 이 학교는 특이하게도 과목에 따라 가중치가 있어, 이 가중치에 따라 졸업평점을 계산한다. 그런데 이 코드는 흉측할 뿐만 아니라 결과도 이상하다.
9/10장의 어떤 리팩터링 기법
이 필요한지 찾아
그 이름을 쓰고
,
리팩터링하시오.
class Student {
constructor() {
this._courses = [];
this._grade = new Grade(0);
this._weights = 0;
}
addCourse(course) {
this._courses.push(course);
// 9.4 참조를 값으로 바꾸기
this._grade = new Grade(this._grade.value + course.grade * course.weight);
}
// 9.3 파생 변수를 질의 합수로 바꾸기
get grade() {
return this._grade.value / this._courses.length;
}
}
class Grade {
constructor(data) {
this._value = data;
}
get value() {
return this._value;
}
}
/////////// 여기까지 과제2 문제 영역
let student = new Student();
student.addCourse({grade:4.0, weight:1.0})
student.addCourse({grade:3.7, weight:0.5})
student.addCourse({grade:3.0, weight:1.0})
student.addCourse({grade:4.0, weight:0.5})
console.log(`졸업 평점은 ${student.grade} 입니다`)
class Student {
constructor() {
this._courses = [];
this._grade = 0;
this._weights = 0;
}
addCourse(course) {
this._courses.push(course);
}
get weights() {
return this._courses.reduce((a,b) => a+b.weight, 0);
}
get grade() {
return this._courses.reduce((a,b)=> a + (b.grade * b.weight), 0) / this.weights;
}
}
과제2. 30점
function shouldJab (healthworker, critical, patient, date) {
if(isHealthWorker(healthworker)) {
if(critical != true) { return true; }
if(critical == true) { return false;}
}
// 10.3 중첩 조건문을 보호 구문으로 바꾸기
if(isNotHeathWorker(healthworker)) {
if(critical == true) { return true; }
if(isAgeOverForty(patient, date)) {return true;}
if(isAgeYouth(patient)) {return false;}
if(date > 1000) {return true;}
}
return false;
}
// 10.2 조건식 통합하기
function isAgeOverForty(patient, date) {
return (isAgeSeniorHigh(patient) || isAgeSenior(patient, date) || isAgeMiddleAged(patient, date));
}
//10.1 조건문 분해하기
function isHealthWorker(healthworker) {
return healthworker === true;
}
//10.1 조건문 분해하기
function isNotHeathWorker(healthworker) {
return healthworker !== true;
}
//10.1 조건문 분해하기
function isCritical(critical) {
return critical === true;
}
//10.1 조건문 분해하기
function isAgeSeniorHigh(patient) {
return patient.age > 75;
}
function isAgeSenior(patient, date) {
return patient.age > 60 && date > 430;
}
function isAgeMiddleAged(patient, date) {
return patient.age > 40 && date > 700;
}
function isAgeYouth(patient) {
patient.age < 30 && patient.sex === 'female';
}
function shouldJab (healthworker, critical, patient = {}, date = 0) {
if(healthworker) {
return treatHealthworker(healthworker, critical);
}
if (isYoungWoman(patient)) {
return false;
}
if (nhReadyToJab(critical, patient.age, date)) {
return true;
}
return false;
}
function nhReadyToJab(critical, age, date) {
return critical ||
age > 75 ||
age > 60 && date > 430 ||
age> 40 && date > 700 ||
date > 1000
}
function treatHealthworker(healthworker, critical) {
if (healthworker && !critical) {
return true;
}
if (healthworker && critical) {
return false;
}
}
function isYoungWoman(patient) {
return patient.age < 30 && patient.sex === 'female'
}