From bc981aed7ee4968895c049fbb1ae71525e9a6571 Mon Sep 17 00:00:00 2001 From: "M. Margo Nikolaisen" Date: Thu, 22 Mar 2018 19:36:35 -0600 Subject: [PATCH] JavaScript-II --- src/class.js | 34 ++++++++++++++++++++++++++++ src/prototype.js | 58 ++++++++++++++++++++++++++++++++++++++++++++++++ src/recursion.js | 10 +++++++++ 3 files changed, 102 insertions(+) diff --git a/src/class.js b/src/class.js index 1ec26ec..e86a1c7 100644 --- a/src/class.js +++ b/src/class.js @@ -9,6 +9,22 @@ // code here +class Use { + constructor(options) { + this.email = options.email; + this.password = options.password; + } + + comparePasswords(param) { + if (param === this.password) { + return true; + } + else { + return false; + } + } +} + // Part 2 // Create a class called `Animal` and a class called `Cat` using ES6 classes. // `Cat` should extend the `Animal` class. @@ -21,6 +37,24 @@ // code here +class Animal { + constructor(options) { + this.age = options.age; + } + growOlder() { + return ++this.age; + } +} + +class Cat extends Animal { + constructor(options) { + super(options); + this.name = options.name; + } + meow() { + return `$(this.name)`; + } +} /* eslint-disable no-undef */ module.exports = { diff --git a/src/prototype.js b/src/prototype.js index e2494a6..028e636 100644 --- a/src/prototype.js +++ b/src/prototype.js @@ -49,6 +49,64 @@ hamsterHuey.destroy(); // returns 'Game object was removed from the game.' */ +class GameObject { + constructor() { + this.createdAt = obj.createdAt; + this.dimensions = obj.dimensions; + } + destroy() { + return 'Game object was removed from the game.'; + } +} + +class NPC extends GameObject { + constructor(obj) { + super(obj); + this.hp = obj.hp; + this.name = obj.name; + } + takeDamage() { + return `${this.name} took damage.'; + } + destroy() { + }; +} + +class Humanoid extends NPC { + constructor(obj) { + super(obj); + this.faction = obj.faction; + this.weapons = obj.weapons; + this.language = obj.language; + } + greet() { + return `${ this.name } offers a greeting in ${ this.language } `; + } + destroy(); + takeDamage(); +} + +const Charlie = new Humanoid({ + createdAt: new Date(), + dimensions: { + length: 2, + width: 1, + height: 1, + }, + hp: 5, + name: 'Charlie', + faction: 'Fierce Doggie Tribe', + weapons: [ + 'passes gas', + ], + language: 'Barkish', +}); + +Charlie.greet(); // return 'Charlie offers a greeting in Barkish' +Charlie.takeDamage(); // return 'Hamster Huey took damage +Charlie.destroy(); // return 'Game object was removed from the game.' + + /* eslint-disable no-undef */ module.exports = { diff --git a/src/recursion.js b/src/recursion.js index a6a6c13..ffb04a7 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -3,11 +3,21 @@ const nFibonacci = (n) => { // fibonacci sequence: 1 1 2 3 5 8 13 ... // return the nth number in the sequence + function fibonacci(num) { + if (num <= 1) return 1; + + return fibonacci(num - 1) + fibonacci(num - 2); + } + }; const nFactorial = (n) => { // factorial example: !5 = 5 * 4 * 3 * 2 * 1 // return the factorial of `n` + if (n === 0) + return 1; + return n * nFactorial(n - 1); + }; /* Extra Credit */