I need help. Is my code right? Thank you. #123553
-
Body//create two classes: an Animal class and a Mamal class. class Animal {
}; class Mamal extends Animal { const animal1 = new Mamal("Shawn", "Cow", "White"); Guidelines
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Code Review and ExplanationLet's review and discuss the code you provided: // Create two classes: an Animal class and a Mammal class.
// Create a cow that accepts a name, type, and color and has a sound method that moos her name, type, and color.
class Animal {
constructor(name, type, color) {
this.name = name;
this.type = type;
this.color = color;
}
sound() {
console.log(`Mooooo my name is ${this.name}, and I'm a ${this.type}.
My color is ${this.color}`);
}
}
class Mammal extends Animal {
constructor(name, type, color) {
super(name, type, color);
}
}
const animal1 = new Mammal("Shawn", "Cow", "White"); Code Explanation:
Code Review:
Suggestions:
By following these suggestions, you can further enhance the code's robustness and ensure it meets your requirements. Mark as Correct Answer ✅If you found this review helpful, please mark me as the correct answer. Happy coding! Note: Remember to test your code thoroughly to ensure it behaves as expected in different scenarios. |
Beta Was this translation helpful? Give feedback.
-
Hi @Xaeon23 , Yes your code overall is fine, the only thing I would add to it is a call to the animal1.sound(); Result:
|
Beta Was this translation helpful? Give feedback.
Hi @Xaeon23 ,
Yes your code overall is fine, the only thing I would add to it is a call to the
sound()
method on theanimal1
instance, which prints out the sound of the animal along with its name, type, and color:Result:
Mooooo my name is Shawn, and I'm a Cow. My color is White