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

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


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

18 changes: 13 additions & 5 deletions constructors.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,33 @@ 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
// the Animal constructor needs to be invoked with the 'options' argument
// Cat should have its prototype inherit from Animal
// 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);
Cat.prototype.constructor = Cat;


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

18 changes: 15 additions & 3 deletions recursion.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,17 @@ while (n <= 10) {

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

// code here
function countToTen(num = 1) {
if (num > 10) {
return;
}

console.log(num);
return countToTen(num + 1);
}

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

// Problem 2:
Expand All @@ -28,6 +35,11 @@ const factorial = n => {
console.log(factorial(5));

// write the above function in a recursive way.
function recursiveFactorial(num) {
if (num == 0) return 1;

return num * recursiveFactorial(num - 1);
}

// when your code is ready, un-comment the next line and run the file
// console.log(recursiveFactorial());
console.log(recursiveFactorial(5));
29 changes: 24 additions & 5 deletions this.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,39 @@
* write out a code example of each explanation above
*/

console.log('hello world!');
function helloWorld(args) {
console.log(`this is ${this} with args ${args}`);
}

// Principle 1
myObj = {
helloWorld: helloWorld,
};

function Obj() {
this.name = 'Constructor';
this.myMethod = function() {
console.log(`I am a ${this.name}`);
};
}

// Principle 1
// code example for Window Binding
helloWorld(1);

// Principle 2

// code example for Implicit Binding
myObj.helloWorld(1);

// Principle 3

// code example for New Binding
myNewObj = new Obj;
myNewObj.myMethod();

// Principle 4

// Principle 4
// code example for Explicit Binding
const boundFunction = helloWorld.bind(myObj, 4);
boundFunction();

helloWorld.call(myObj, 2);
helloWorld.apply(myObj, [3]);