Challenge: Solution Review
This lesson will explain the solution to the problem from the previous coding challenge.
We'll cover the following...
Solution #
Press + to interact
//Componentclass Directory {constructor(name, lastModified, size) {this.name = namethis.lastModified = lastModifiedthis.size = size}getLastmodified() { }getSize() { }getName() { }}//Leaf subclassclass File extends Directory {constructor(name, lastModified, size) {super(name, lastModified, size)}getLastmodified() {return this.lastModified}getSize() {return this.size}getName() {return this.name}}//Composite subclassclass Folder extends Directory {constructor(name) {super(name)this.lastModified = 0this.size = 0this.files = []}addFile(file) {this.files.push(file)this.size += file.getSize();}removeFile(file) {for (var i = 0; i < this.files.length; i++) {if (this.files[i] == file) {this.files.splice(i, 1)this.size -= file.getSize();}}}getLastmodified() {var times = []if (this.size == 0) {return this.lastModified}else{for (var i = 0; i < this.files.length; i++) {times[i] = this.files[i].lastModified}this.lastModified = Math.min(...times)return this.lastModified}}getSize() {return this.size}getName() {var names = []for (var i = 0; i < this.files.length; i++) {names[i] = this.files[i].name}return names}}const file1 = new File("dogo.png", 2, 45)const file2 = new File("catty.png", 4, 32)const folder = new Folder("Pictures")folder.addFile(file1)folder.addFile(file2)console.log(folder.getLastmodified())console.log(folder.getSize())console.log(folder.getName())folder.removeFile(file2)console.log(folder.getName())console.log(folder.getSize())const file = new File("penguiny.png", 6, 12)console.log(file.getName())
Explanation
The illustration below provides a visualization of the hierarchy of the classes in the code above:
From the ...