10. Systems Register JS Fundamentals
Judge ми връща 60/100.
Предполагам, че е заради сортировката, макар че не намирам грешка в нея.
Кодът ми : https://pastebin.com/4YRx3F1n
Judge ми връща 60/100.
Предполагам, че е заради сортировката, макар че не намирам грешка в нея.
Кодът ми : https://pastebin.com/4YRx3F1n
Everything is fine (including the sorting :-) ), you were just missing out on the third sorting-validation:
"The Components must be ordered by amount of Subcomponents, in descending order"
Just change your final for-loop to and judge gives 100%:
for (let system of systems) {
console.log(system.systemName);
let components = system.components;
for (let component of components.sort((a, b) => b.subcomponents.length - a.subcomponents.length)) {
console.log(`|||${component.name}`)
let subcomponents = component.subcomponents;
for (const subcomponent of subcomponents) {
console.log(`||||||${subcomponent}`);
}
}
}
Or, alternative with Object.values():
function components(array) {
class System {
constructor(name) {
this.systemName = name;
this.components = [];
}
addComponent(component) {
this.components.push(component);
}
get getComponents() {
return this.components;
}
}
class Component {
constructor(name) {
this.name = name;
this.subcomponents = [];
}
addSubComponent(subcomponent) {
this.subcomponents.push(subcomponent);
}
}
let systems = [];
for (let i = 0; i < array.length; i++) {
let elements = array[i].split(' | ');
let containsInSystems = (systemName) => {
let system = null;
systems.forEach(el => el.systemName === systemName ? system = el : null);
return system;
}
if (containsInSystems(elements[0]) === null) {
let system = new System(elements[0]);
let component = new Component(elements[1]);
component.addSubComponent(elements[2]);
system.addComponent(component);
systems.push(system);
} else {
let system = containsInSystems(elements[0]);
let component = system.getComponents.find(el => el.name === elements[1]);
if (component !== undefined) {
component.addSubComponent(elements[2]);
} else {
let newComponent = new Component(elements[1]);
newComponent.addSubComponent(elements[2]);
system.addComponent(newComponent);
}
}
}
// systems = systems.sort(function (a, b) {
// if (a.components.length > b.components.length) { return -1; }
// if (a.components.length < b.components.length) { return 1; }
// if (a.systemName > b.systemName) { return 1; }
// if (a.systemName < b.systemName) { return -1; }
// else { return 0; }
// });
systems = Object.values(systems).sort((a, b) => {
return b.components.length - a.components.length || a.systemName.localeCompare(b.systemName);
});
// for (let system of systems) {
// console.log(system.systemName);
// let components = system.components;
// for (let component of components.sort((a, b) => b.subcomponents.length - a.subcomponents.length)) {
// console.log(`|||${component.name}`)
// let subcomponents = component.subcomponents;
// for (const subcomponent of subcomponents) {
// console.log(`||||||${subcomponent}`);
// }
// }
// }
for (let system of Object.values(systems)) {
console.log(system.systemName);
let components = system.components;
for (let component of components.sort((a, b) => b.subcomponents.length - a.subcomponents.length)) {
console.log(`|||${component.name}`)
let subcomponents = component.subcomponents;
for (const subcomponent of subcomponents) {
console.log(`||||||${subcomponent}`);
}
}
}
}
Best,
great job! i l love it! couples therapy san diego
Thank you!!! : )