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 @@ -3,17 +3,34 @@
// 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!`);
}
}

dog = new Animal({name: 'Fido'});
dog.grow();

// problem #2
// convert the Cat constructor function from 'constructors.js' into an ES6 class

class Cat extends Animal {
constructor(options) {
super(options)
}
}

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

20 changes: 14 additions & 6 deletions constructors.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ function Animal(options) {
this.name = options.name;
}

Animal.prototype.grow = function() {
console.log(`${this.name} grew larger!`);
}

dog = new Animal({name: 'Fido'});
dog.grow();

// add 'grow' to Animal's prototype here

// problem #2
Expand All @@ -17,17 +24,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();

40 changes: 25 additions & 15 deletions recursion.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,43 @@
// 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++;
}
// let n = 1;
// while (n <= 10) {
// console.log('While Loop', n);
// n++;
// }

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

// code here
const countToTen = (n = 10) => {
if (n === 0) return 'done';
console.log('Recursion', n);

return countToTen(--n);
}

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

// Problem 2:

const factorial = n => {
let result = 1;
for (let i = 2; i <= n; i++) {
result *= i;
}
return result;
};
// const factorial = n => {
// let result = 1;
// for (let i = 2; i <= n; i++) {
// result *= i;
// }
// return result;
// };

console.log(factorial(5));
// console.log(factorial(5));

// write the above function in a recursive way.
const recursiveFactorial = n => {
if (n === 1) return 1;

return n * recursiveFactorial(--n) ;
}
// when your code is ready, un-comment the next line and run the file
// console.log(recursiveFactorial());
console.log(recursiveFactorial(5));
41 changes: 37 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. Window/Global binding: any object defined in global space bound to window object so this will be window object.
* 2. Implicit binding: function is bound to it's object scope, i.e. left of dot
* 3. New binding: constructor funcitons are bound the the newly created object
* 4. Explicit binding: Set this by using call, apply and bind.
*
* write out a code example of each explanation above
*/
Expand All @@ -15,14 +15,47 @@ console.log('hello world!');

// code example for Window Binding

const myfunc = () => {
console.log(this); // will return window object
}

// Principle 2

// code example for Implicit Binding

const myObj = {
myProp: 'awesomeProp!',
myFunc: function() { console.log(`This is ` + this + ` MyProp is ` + this.myProp) }
}

myObj.myFunc(); // logs This is [object Object] MyProp is awesomeProp!

// Principle 3

// code example for New Binding

const Obj = function(a, b) {
this.a = a;
this.b = b;
}

const myObj2 = new Obj('yo', 'dude');

console.log(`param a is ${myObj2.a} and param b is ${myObj2.b}`); // logs param a is yo and param b is dude



// Principle 4

// code example for Explicit Binding

const theFunc = function() {
console.log(`me.a is ${me.a} and me.b is ${me.b}`);
}

const me = {
a: 'it is a!',
b: 'it is b!'
}

theFunc.apply(me); // logs me.a is it is a! and me.b is it is b!