I just started an internship and we have a little task to do. It should follow the SOLID principles and I have a slight idea, but I'm not exactly sure what I'm looking for and if you can tell me how to improve my code you will help me a lot. Also, why is the forEach not working and I have to make a For loop?
This is the task: Imagine a medical app for registering patients. As medical worker I want to input 3 patients in the system(first name, last name, date of birth) in random order. After the 3rd patient when I press Enter a list of the patients is returned in sorted, alphabetical order. You have the lecture, keep in mind the S.O.L.I.D principles, code reusability etc.
P.S. There are always 3 patients.
// let list = prompt('Enter list of patients.');
let list = 'John, Doe, 18/05/1995;Jane, Doe, 13/12/1988;Brett, Cooper, 05/11/1991;';
list = list.split(';');
class Patient {
constructor(fname, lname, date) {
this.fname = fname;
this.lname = lname;
this.date = date;
}
loader(patients) {
for(let i = 0; i < 3; i++) {
const tempArr = list[i].split(',');
// tempArr.forEach(e => e = e.trim());
for(let i = 0; i < tempArr.length; i++) {
tempArr[i] = tempArr[i].trim();
}
patients.push(new Patient(tempArr[0], tempArr[1], tempArr[2]));
}
};
}
function objSort(arr) {
arr.sort((a,b) => (a.fname > b.fname) ? 1 : ((b.fname > a.fname) ? -1 : 0));
}
let patients = [];
let patient = new Patient();
patient.loader(patients);
objSort(patients);
let listForPrint = '';
patients.forEach(patient => {
listForPrint += `${patient.fname} ${patient.lname} ${patient.date}\n`;
});
console.log(listForPrint);
Code Review
. Fixing broken code changes the code. Changing code necessarily changes its evaluation in the context of SOLID principles. \$\endgroup\$