Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

JavaScript constructor function exercises.

NotificationsYou must be signed in to change notification settings

devleague/js-constructors

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

  1. Navigate to this project in your terminal.
  2. Runlive-server.
  3. 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:

  1. Saveconstructors.js.
  2. Reloadhttp://localhost:8080 in Chrome.
  3. Check if the test passes.
  4. 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;};

Spell(name, cost, 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.

getDetails()

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.

DamageSpell(name, cost, damage, 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.prototypea 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.

Spellcaster(name, health, mana)

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.

inflictDamage(damage)

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

spendMana(cost)

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.

invoke(spell, target)

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.

Releases

No releases published

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp