- Notifications
You must be signed in to change notification settings - Fork184
devleague/js-constructors
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
- Navigate to this project in your terminal.
- Run
live-server
. - Openhttp://localhost:8080 in Chrome.
You should see failing tests.
Create 3 constructor functions by following the comments inconstructors.js
.Write all of the constructor functions below inconstructors.js
After you complete a test:
- Save
constructors.js
. - Reloadhttp://localhost:8080 in Chrome.
- Check if the test passes.
- If it passes,commit your work.
Some comments meant to be nested inside of the constructor function, be sure your code follows the same structure.
Example (Before)
/** * An example constructor. *@class Represents a constructor function. *@param {string} description – The example's description. *//** * Returns the example's description. *@return {string} description – This example's description. */
Example (After)
/** * An example constructor. *@class Represents a constructor function. *@param {string} description – The example's description. */functionExample(description){this.description=description;}/** * Returns the example's description. *@return {string} description – This example's description. */Example.prototype.getDescription=function(){returnthis.description;};
Creates a generic spell that can be cast.
Parameters
name: string, The name of the spell.
cost: number, The amount needed to cast this spell.
description: string, A short description of the spell.
Returns a string of all of the spell's details.The format doesn't matter, as long as it contains the spell name, cost, and description.
A spell that deals damage.We want to keep this code DRY (Don't Repeat Yourself).
So you should useSpell.call()
to assign the spell name, cost, and description.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call
In addition, you will also want to assignDamageSpell.prototype
a value so that it inherits fromSpell
.Make sure to call this OUTSIDE of the function declaration.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/prototype
Parameters
name: string, The name of the spell.
cost: number, The amount needed to cast this spell.
damage: number, The amount of damage this spell deals.
description: string, A short description of the spell.
Now that you've created some spells, let's createSpellcaster
objects that can use them!
Parameters
name: string, The spellcaster's name.
health: number, The spellcaster's health points.
mana: number, The spellcaster's mana points, used for casting spells.
The spellcaster loses health equal todamage
.Health should never be negative.If the spellcaster's health drops to 0,itsisAlive
property should be set tofalse
.
Parameters
damage: number, Amount of damage to deal to the spellcaster
Reduces the spellcaster's mana bycost
.Mana should only be reduced only if there is enough mana to spend.
Parameters
cost: number, The amount of mana to spend.
Returns: boolean, Whether mana was successfully spent.
Allows the spellcaster to cast spells.The first parameter should either be aSpell
orDamageSpell
.If it is aDamageSpell
, the second parameter should be aSpellcaster
.The function should returnfalse
if neither of the conditions are not satisfied.
You should useinstanceof
to check for these conditions.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof
Next check if the spellcaster has enough mana to cast the spell.If it can cast a spell, it should lose mana equal to the spell's cost.If there is not enough mana, returnfalse
.
If there is enough mana to cast the spell, returntrue
.In addition, if it is aDamageSpell
reduce the target's health by the spell's damage value.
Use functions you've previously created: (inflictDamage
,spendMana
)to help you with this.
Parameters
spell: Spell | DamageSpell, The spell to be cast.
target: Spellcaster, The spell target to be inflicted.
Returns: boolean, Whether the spell was successfully cast.