Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jeffrey flynn #76

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
24 changes: 17 additions & 7 deletions classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,28 @@

// 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(catOptions) {
super(catOptions);
}
}

// 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();
16 changes: 10 additions & 6 deletions constructors.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ function Animal(options) {
}

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

// problem #2
// setup Cat to inherit from Animal
Expand All @@ -17,17 +20,18 @@ function Animal(options) {
// instances of Cat should also have access to the 'grow' method

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

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

// 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();

36 changes: 31 additions & 5 deletions recursion.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,28 @@ while (n <= 10) {

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

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



// when you code is ready, un-comment the next line and run the file
// console.log(countToTen());
/* ================ Next Problem ================= */

// Problem 2:
Expand All @@ -29,5 +47,13 @@ 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 factorial = n => {
//base case
if (n === 0) {
return 1;
}
//return function
return n * factorial(n - 1);
};

console.log(factorial(5));
66 changes: 55 additions & 11 deletions this.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,72 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
*
* write out a code example of each explanation above
* 1. Implicit binding - when a function containing the THIS keyword is invoked,
look to the left of the . (period) to see what object THIS is referring to

* 2. Explicit binding - explicitly state what the THIS keyword refers to by using .call / .apply / .bind
.call arguments must be passed one-by-one or via spread syntax (...argumentsArray)
.apply allows us to pass an array as a list argument (array)
.bind creates a new function for future use

* 3. new Binding - utilizes the NEW keyword to invoke a function which creates a new THIS object

* 4. window Binding - defaults the THIS keyword to the window object

*/

console.log('hello world!');
/************************************************/

// Principle 1

// code example for Window Binding
const heyThere = function() {
console.log('hey, ' + this.name);
}
const me = {
name: 'jeffrey',
age: 24
};

heyThere(); //returns 'hey, '
window.name = 'jeffrey';
heyThere(); //returns 'hey, jeffrey'

/************************************************/

// Principle 2
// code example fo Implicit Binding
const Person = function(name, age) {
return {
name: name,
age: age,
sayHi: function() {
console.log('hey, ' + name);
}
}
};
const me = Person('jeffrey', 24);
me.sayHi();

// code example for Implicit Binding
/************************************************/

// Principle 3

// code example for New Binding
const Person = function(name, age) {
this.name = name;
this.age = age;
};

// Principle 4
const me = new Person('jeffrey', 24);

/************************************************/

// Principle 4
// code example for Explicit Binding
const heyThere = function() {
console.log('hey, ' + this.name);
}
const me = {
name: 'jeffrey',
age: 24
};
heyThere.call(me);