Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions number-46/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>
class Shape {
constructor(length, width) {
this.length = length;
this.width = width;
}

get area() {
return this.length * this.width;
}

get name() {
return "Default Shape";
}
}

class Square extends Shape {
constructor(length) {
super(length, length);
}

get name() {
return "Square";
}
}

class Rectangle extends Shape {
constructor(length, width) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this constructor is exactly identical as Shape's constructor, it is not necessary. It will automatically take everything inside the Shape class.

super(length, width);
}

get name() {
return "Rectangle";
}
}

class Triangle extends Shape {
constructor(length, width) {
super(length, width / 2);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 , is it necessary to divide the width by 2? If I gave you this triangle and told you the width was 10cm and the height was 6cm. Would you expect the width to be 5?

}

get name() {
return "Triangle";
}
}
class Circle extends Shape {
constructor(radius) {
super(radius * radius, Math.PI);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually an interesting one! Since a circle doesn't really have a length or height, but what you did is clever since it would still leverage the area function 👍. One thing to note is that it's not always necessary to call super.

}

get name() {
return "Circle";
}
}
class Cube extends Shape {
constructor(length, width, height) {
super(length * width, height);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one might actually be better as something like this. You would also need to override the area function meaning you need to create a new one in this class.

constructor(length, width, height) {
  this.height = height;
  super(length, width);
}

}

get name() {
return "Cube";
}
}

const shapey = new Cube(2, 2, 2);
console.log(shapey.area);
console.log(shapey.name);

</script>
</body>
</html>