Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,28 @@
// for a potential password that will be compared to the `password` property.
// Return true if the potential password matches the `password` property. Otherwise return false.


// code here
class User {
constructor(options) {
this.email = options.email;
this.password = options.password;

}

comparePasswords(pass) {

if(this.password == pass) return true;
else{ return false;}

}
}

const User1 = new User({
password: 'password',

})
User1.comparePasswords('password');
// Part 2
// Create a class called `Animal` and a class called `Cat` using ES6 classes.
// `Cat` should extend the `Animal` class.
Expand All @@ -20,7 +40,41 @@
// property set on the Cat instance.

// code here
// code here
class Animal {
constructor(options) {
this.age = options.age;


}
growOlder() {
return this.age +=1;
}
}




class Cat extends Animal {
constructor(options) {
super(options);
this.name = options.name;
}
meow() {

return this.name + ' meowed!'
}
}

const fifi = new Cat ({
name: 'fifi',
age: 10,
})

let catSound = fifi.meow();
let catAge = fifi.growOlder();

console.log('The cats sound is ' + catSound + 'The cats age is ' + catAge);
/* eslint-disable no-undef */

module.exports = {
Expand Down
82 changes: 68 additions & 14 deletions src/prototype.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,76 @@
GameObject
createdAt
dimensions
destroy() // prototype method -> returns the string 'Game object was removed from the game.'

NPC
hp
name
takeDamage() // prototype method -> returns the string '<object name> took damage.'
// should inherit destroy() from GameObject's prototype

Humanoid
faction
weapons
language
greet() // prototype method -> returns the string '<object name> offers a greeting in <object language>.'
destroy() // prototype method -> returns the string 'Game object was removed from the game.'*/
function GameObject(gameobject) {
this.createdAt = gameobject.createdAt;
this.dimensions = gameobject.dimensions;
this.name = gameobject.name;

}

GameObject.prototype.destroy = function() { // this is a function for
console.log(this.name + ' was removed from the game!');
}

const gameobject = {
name: this.GameObject,
}



const pawn = new GameObject ({ // creates a new object
name: 'pawn',
})

pawn.destroy(); //calls destroy on the pawn object

function NPC(NPCgameobjects) {
GameObject.call(this, NPCgameobjects);
this.hp = NPCgameobjects.hp;
this.name = NPCgameobjects.name;
}

NPC.prototype = Object.create(GameObject.prototype);

NPC.prototype.takeDamage = function() {
console.log(this.name + ' took damage');
}

const Enemy = new NPC ({
hp: 100,
name: 'The Destroyer!',
})

function Humanoid(options) {
NPC.call(this, options)
this.faction = options.faction;
this.weapons = options.weapons;
this.language = options.language;
}

Humanoid.prototype = Object.create(NPC.prototype);

Humanoid.prototype.greet = function() {
console.log(this.name + " offers a greeting in " + this.language);
}

const Player = new Humanoid ({
hp: 100,
name: 'Player1',
faction: 'Blue Team',
weapons: 'Swords',
language: 'Japanese',
})

Enemy.takeDamage();
Player.takeDamage();
Player.greet();

// prototype method -> returns the string '<object name> offers a greeting in <object language>.'
// should inherit destroy() from GameObject through NPC
// should inherit takeDamage() from NPC

/*
Inheritance chain: Humanoid -> NPC -> GameObject
Instances of Humanoid should have all of the same properties as NPC and GameObject.
Instances of NPC should have all of the same properties as GameObject.
Expand Down
22 changes: 21 additions & 1 deletion src/recursion.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,32 @@

const nFibonacci = (n) => {
// fibonacci sequence: 1 1 2 3 5 8 13 ...
// return the nth number in the sequence

if(n <= 2) { // this is my base case
return n;
}
else {
console.log(n);
return (nFibonacci(n - 1) + nFibonacci(n - 2));

}
// return the nth number in the sequence
return n;

};

const nFactorial = (n) => {
// factorial example: !5 = 5 * 4 * 3 * 2 * 1
// return the factorial of `n`

// write the above function in a recursive way.

if (n === 0) { // this is my base case
return 1
}
else { return n*factorial2(n - 1)
}

};

/* Extra Credit */
Expand Down
23 changes: 22 additions & 1 deletion src/this.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,26 @@
/* part 1 */

class User {
constructor(options) {
constructor(username, password) {
// set a username and password property on the user object that is created
this.username = username;
this.password = password;

this.checkPassword = function(str) {
console.log('I am here!')

if(this.password === str) {
return true;
}
return false;

}

}
// create a method on the User class called `checkPassword`
// this method should take in a string and compare it to the object's password property
// return `true` if they match, otherwise return `false`

}

const me = new User({
Expand All @@ -20,7 +34,10 @@ const me = new User({

const result = me.checkPassword('correcthorsebatterystaple'); // should return `true`

console.log(result);

/* part 2 */
const args = [5, 6 ,7 ,8 ];

const checkPassword = function comparePasswords(passwordToCompare) {
// recreate the `checkPassword` method that you made on the `User` class
Expand All @@ -33,7 +50,11 @@ const checkPassword = function comparePasswords(passwordToCompare) {
// use .call, .apply, and .bind

// .call
checkPassword.call(User, passwordToCompare);

// .apply
checkPassword.apply(User, passwordToCompare, args);

// .bind
let test = checkPassword.bind(User. passwordToCompare); // what is wrong with this?
let test('this is bind?');