In this practice, you will create and use classes, inheritance, and commonJS module importing/exporting!
Clone the starter from the Download link at the bottom of this page.
Run npm install
to install any dependencies.
Let's create a class to represent a Dragon! ๐ฒ
Your goal is to pass all the test specs in the test/dragon-spec.js file. Run the Mocha test specs in that file with the following command:
npm test test/dragon-spec.js
Implement the following in the classes/dragon.js file.
-
Define a class named
Dragon
containing a constructor method that accepts two arguments which should initialize the following properties:- name - the name of the dragon (like "Puff" or "Toothless")
- color - the color of the dragon ( like "green" or "black")
-
Define an instance method to the
Dragon
class. The method should be namedbreathesFire
and return a string that uses thename
property value formatted as${name} breathes fire everywhere! BURN!!!!
. -
Define a static method to the
Dragon
class. The method should be namedgetDragons
and accept any number of instances of theDragon
class that are passed into it as separate arguments. ThegetDragons
static method should return an array of all the dragon names from the instances that were passed into it as arguments.
In addition to Mocha, you should test your code manually using Node.js. After
the Dragon
class definition, you can insert the examples below and use the
command, node classes/dragon.js
to run it.
Examples:
const puff = new Dragon("Puff", "green");
console.log(puff);
console.log(puff.breathesFire());
This should print to the terminal:
Dragon { name: 'Puff', color: 'green' }
Puff breathes fire everywhere! BURN!!!!
const toothless = new Dragon("Toothless", "black");
console.log(toothless);
console.log(toothless.breathesFire());
This should print to the terminal:
Dragon { name: 'Toothless', color: 'black' }
Toothless breathes fire everywhere! BURN!!!!
const puff = new Dragon("Puff", "green");
const toothless = new Dragon("Toothless", "black");
console.log(Dragon.getDragons(puff, toothless));
This should print to the terminal:
[ 'Puff', 'Toothless' ]
Let's create a class to represent a FriendlyDragon! ๐ฒ
Your goal is to pass all the test specs in the test/friendly-dragon-spec.js file. Run the Mocha test specs in that file with the following command:
npm test test/friendly-dragon-spec.js
Implement the following in the classes/friendly-dragon.js file.
-
Require with CommonJS the
Dragon
class from the classes/dragon.js file. -
Define a class named
FriendlyDragon
that inherits from theDragon
parent class you required. TheFriendlyDragon
class should contain a constructor method that accepts four arguments which should initialize the following properties:name
- the name of the dragon (should be inherited)color
- the color of the dragon (should be inherited)lifeGoals
- an array of skills the Friendly Dragon is good atfriend
- the Friendly Dragon's best buddy (they all have one!)
-
Export the
FriendlyDragon
class so that other modules can require it. NOTE: IF YOU DO NOT CORRECTLY EXPORT THEFriendlyDragon
CLASS, THE MOCHA TESTS WILL THROW ATypeError
AND NOT RUN -
Additionally, add two(2) instance methods to the
FriendlyDragon
class.hasLifeGoals()
- this method shouldconsole.log
each of theFriendlyDragon
's life goals separately, in the format${name} likes to ${lifeGoal}
helpsPeople()
- this method should return a string using thename
andfriend
property values formatted as${name} helps their friend ${friend}
.
In addition to Mocha, you should test your code manually using Node.js. After
the FriendlyDragon
class definition, you can insert the examples below and use
the command, node classes/friendly-dragon.js
to run it.
Examples:
const puff = new FriendlyDragon(
"Puff",
"green",
[
"live by the sea",
"frolick in the autumn mist",
"help small children"
],
"Jackie Paper"
);
console.log(puff);
console.log(puff.breathesFire()); //Friendly dragons can still breathe fire!
puff.hasLifeGoals();
console.log(puff.helpsPeople());
This should print to the terminal:
FriendlyDragon {
name: 'Puff',
color: 'green',
lifeGoals: [
'live by the sea',
'frolick in the autumn mist',
'help small children'
],
friend: 'Jackie Paper'
}
Puff breathes fire everywhere! BURN!!!!
Puff likes to live by the sea
Puff likes to frolick in the autumn mist
Puff likes to help small children
Puff helps their friend Jackie Paper
const toothless = new FriendlyDragon(
"Toothless",
"black",
[
"save the town of Burke",
"fly with a kid on his back",
"hang out with Vikings"
],
"Hiccup"
);
console.log(toothless); toothless.hasLifeGoals();
console.log(toothless.helpsPeople());
console.log(toothless.breathesFire());
This should print to the terminal:
FriendlyDragon {
name: 'Toothless',
color: 'black',
lifeGoals: [
'save the town of Burke',
'fly with a kid on his back',
'hang out with Vikings'
],
friend: 'Hiccup'
}
Toothless likes to save the town of Burke
Toothless likes to fly with a kid on his back
Toothless likes to hang out with Vikings
Toothless helps their friend Hiccup
Toothless breathes fire everywhere! BURN!!!!
Let's create a class to represent an EvilDragon! ๐
Your goal is to pass all the test specs in the test/evil-dragon-spec.js file. Run the Mocha test specs in that file with the following command:
npm test test/evil-dragon-spec.js
Implement the following in the classes/evil-dragon.js file.
-
Require with CommonJS the
Dragon
class from the classes/dragon.js file. -
Define a class named
EvilDragon
that inherits from theDragon
parent class you imported. TheEvilDragon
class should contain a constructor method that accepts four arguments that initialize the following properties:name
- the name of the dragon (should be inherited)color
- the color of the dragon (should be inherited)evilDoings
- an array of bad things the Evil Dragon doesnemesis
- the Evil Dragon's arch-nemesis.
-
Export the
EvilDragon
class so that other modules can require it. NOTE: IF YOU DO NOT CORRECTLY EXPORT THEFriendlyDragon
CLASS, THE MOCHA TESTS WILL THROW A 'TypeError` AND NOT RUN -
Additionally, add two(2) instance methods to the
EvilDragon
class.dontInviteThemOverForDinner()
- this method shouldconsole.log
out each of the Evil Dragon's evil doings separately, in the format${name} will ${evilDoing}
burnsNemesis()
- this method should return a string usingname
andnemesis
property values formatted as${name} destroys ${nemesis} with fire! WHOOOSH!!!
.
In addition to Mocha, you should test your code manually using Node.js. After
the EvilDragon
class definition, you can insert the examples below and use
the command, node classes/evil-dragon.js
to run it.
Examples:
const enderDragon = new EvilDragon(
"Ender Dragon",
"black", [
"spit a fireball at Steve",
"rule over all the Endermen",
"destroy all blocks in its way"
],
"Steve"
);
console.log(enderDragon);
enderDragon.dontInviteThemOverForDinner();
console.log(enderDragon.breathesFire());
console.log(enderDragon.burnsNemesis());
This should print to the terminal:
EvilDragon {
name: 'Ender Dragon',
color: 'black',
evilDoings: [
'spit a fireball at Steve',
'rule over all the Endermen',
'destroy all blocks in its way'
],
nemesis: 'Steve'
}
Ender Dragon will spit a fireball at Steve
Ender Dragon will rule over all the Endermen
Ender Dragon will destroy all blocks in its way
Ender Dragon breathes fire everywhere! BURN!!!!
Ender Dragon destroys Steve with fire! WHOOOSH!!!
const drogon = new EvilDragon(
"Drogon",
"black-red",
[
"eat all your livestock",
"burn down your castle in Westeros",
"burn your father and brother"
],
"Night King"
);
console.log(drogon);
drogon.dontInviteThemOverForDinner();
console.log(drogon.breathesFire());
console.log(drogon.burnsNemesis());
This should print to the terminal:
EvilDragon {
name: 'Drogon',
color: 'black-red',
evilDoings: [
'eat all your livestock',
'burn down your castle in Westeros',
'burn your father and brother'
],
nemesis: 'Night King'
}
Drogon will eat all your livestock
Drogon will burn down your castle in Westeros
Drogon will burn your father and brother
Drogon breathes fire everywhere! BURN!!!!
Drogon destroys Night King with fire! WHOOOSH!!!
Let's make instances of both Friendly and Evil Dragons! ๐ฒ ๐ ๐ฒ
Your goal is to pass all the test specs in the test/all-dragons-spec.js file. Run the Mocha test specs in that file with the following command:
npm test test/all-dragons-spec.js
Implement the following in the all-dragons.js file.
-
Require the
FriendlyDragon
class from the ./classes/friendly-dragon.js module. -
Require the
EvilDragon
class from the ./classes/evil-dragon.js module. -
Create 2 new instances, 1 of the
FriendlyDragon
Class and the other of theEvilDragon
Class- A
FriendlyDragon
named "Falkor" with the color "white", the life goals of "save Atreyu from the swamp", "save Atreyu from the Nothing", and "scare the local bullies into a dumpster" and has the friend "Bastian". Assign this new instance to the variablefalkor
- An
EvilDragon
named "Smaug" with the color "black", the evilDoings of "take over your mountain kingdom", "steal all your dwarven gold", and "burn down your floating village" and has the nemesis of "Dwarf King". Assign this new instance to the variable named "smaug"
- A
-
Call the static
getDragons
method on theDragon
Class with yourfalkor
andsmaug
instances as arguments. Save this method call to a variable namedallDragons
and add it to theexport
statement that already exists. Note, there is a comment that cautions not to edit the export statement; it is required to modify it for this step.
In addition to Mocha, you should test your code manually using Node.js. After
you implemented the steps above, you can insert the examples below and use
the command, node all-dragons.js
to run it.
Examples:
console.log(falkor);
falkor.hasLifeGoals();
console.log(falkor.breathesFire());
console.log(falkor.helpsPeople());
FriendlyDragon {
name: 'Falkor',
color: 'white',
lifeGoals: [
'save Atreyu from the swamp',
'save Atreyu from the Nothing',
'scare the local bullies into a dumpster'
],
friend: 'Bastian'
}
Falkor likes to save Atreyu from the swamp
Falkor likes to save Atreyu from the Nothing
Falkor likes to scare the local bullies into a dumpster
Falkor breathes fire everywhere! BURN!!!!
Falkor helps their friend Bastian
console.log(smaug);
smaug.dontInviteThemOverForDinner();
console.log(smaug.breathesFire());
console.log(smaug.burnsNemesis());
EvilDragon {
name: 'Smaug',
color: 'black',
evilDoings: [
'take over your mountain kingdom',
'steal all your dwarven gold',
'burn down your floating village'
],
nemesis: 'Dwarf King'
}
Smaug will take over your mountain kingdom
Smaug will steal all your dwarven gold
Smaug will burn down your floating village
Smaug breathes fire everywhere! BURN!!!!
Smaug destroys Dwarf King with fire! WHOOOSH!!!
The static method from step 4, when called and printed to the terminal:
[ 'Falkor', 'Smaug' ]