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
27 changes: 22 additions & 5 deletions classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,35 @@

// problem #1
// convert the Animal constructor function from 'constructors.js' into an ES6 class
class Animal {
constructor(options){
this.name = options.name;
}

grow() {
console.log(`${this.name} grew larger`);
}
}


// problem #2
// convert the Cat constructor function from 'constructors.js' into an ES6 class
class Cat extends Animal {
constructor(options) {
super(options);
}
}

const kitty = new Cat({name: 'kitty'}) ;
kitty.grow()

// if everything is setup properly the code below will print 'Foofie grew larger!'
// uncomment the code below to test your solution

// const foofie = new Cat({
// name: 'foofie',
// });
//
// foofie.grow();
const foofie = new Cat({
name: 'foofie',
});

foofie.grow();


24 changes: 19 additions & 5 deletions constructors.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ function Animal(options) {
}

// add 'grow' to Animal's prototype here
Animal.prototype.grow = function() {
console.log(`${this.name} grew larger`);
};

const ligo = new Animal({name: 'Fido'});

ligo.grow();

// problem #2
// setup Cat to inherit from Animal
Expand All @@ -18,16 +25,23 @@ function Animal(options) {

function Cat(options) {
// invoke Animal here with .call
Animal.call(this, options);
}


// connect the prototypes here
Cat.prototype = Object.create(Animal.prototype);

const kitty = new Cat({name: 'Kitty'});

kitty.grow();

// if everything is setup properly the code below will print 'Foofie grew larger!'
// uncomment the code below to test your solution

// const foofie = new Cat({
// name: 'foofie',
// });
//
// foofie.grow();
const foofie = new Cat({
name: 'foofie',
});

foofie.grow();

24 changes: 20 additions & 4 deletions recursion.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
// to test these problems you can run 'node recursion.js' in your terminal
// Problem 1:

let n = 1;
while (n <= 10) {
console.log('While Loop', n);
n++;
++n;
}

// write a recursive - function called countToTen that mimics the while loop above.

// code here
const countToTen = (n) => {
if (n <= 10) {
console.log(n);
countToTen(++n);
}
};
countToTen(1);

// when you code is ready, un-comment the next line and run the file
// console.log(countToTen());
Expand All @@ -19,7 +25,9 @@ while (n <= 10) {

const factorial = n => {
let result = 1;
for (let i = 2; i <= n; i++) {
for (let i = 2; i <= n; i++)
{
console.log(i);
result *= i;
}
return result;
Expand All @@ -30,4 +38,12 @@ console.log(factorial(5));
// write the above function in a recursive way.

// when your code is ready, un-comment the next line and run the file
// console.log(recursiveFactorial());
const recursiveFactorial = n => {
if( n <= 1){
return 1;
} else{
return n * recursiveFactorial(--n); // make sure that you insert in --n because n-- is the n before decrement
}
}

console.log(recursiveFactorial(5));
35 changes: 31 additions & 4 deletions this.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
* 1. Object binding - refers to global
* 2. Implicit - bind to the obj name on the left of the period
* 3. New Binding - bind this to the constructor func
* 4. Explicit - passes the obj that it has to bind to by using .call /.apply / .bind
*
* write out a code example of each explanation above
*/
Expand All @@ -15,14 +15,41 @@ console.log('hello world!');

// code example for Window Binding

function globalBind (name) {
console.log(name);
console.log(this);
}

// Principle 2

// code example for Implicit Binding

const edward = {
firstName: 'Edward',
lastName: 'Manda',
fullName: function() {
console.log(`${this.firstName} ${this.lastName}`);
},
}

edward.fullName();

// Principle 3

// code example for New Binding
function Person (personObj) {
this.firstName = personObj.firstName;
this.lastName = personObj.lastName;
this.fullName = function() {
console.log(`${this.firstName} ${this.lastName}`)
};
}
const john = new Person({firstName: 'John', lastName: 'Huggett',});
john.fullName();



// Principle 4
edward.fullName.apply({firstName: 'Petter', lastName: 'Huggett',});

// code example for Explicit Binding