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

15 changes: 10 additions & 5 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 @@ -18,16 +21,18 @@ function Animal(options) {

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

// connect the prototypes here
Cat.prototype = 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();

15 changes: 13 additions & 2 deletions recursion.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@ while (n <= 10) {
// write a recursive - function called countToTen that mimics the while loop above.

// code here
let count = 1;
function countToTen() {
if (count > 10) return;
console.log('recursive', count++);
return countToTen();
}

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

// Problem 2:
Expand All @@ -29,5 +35,10 @@ console.log(factorial(5));

// write the above function in a recursive way.

const recursiveFactorial = (n) => {
if (n <= 1 || !n) return 1;
return n * recursiveFactorial(n - 1);
}

// when your code is ready, un-comment the next line and run the file
// console.log(recursiveFactorial());
console.log(recursiveFactorial(5));
30 changes: 26 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 Binding - 'this' is bound to the global object. In browser it can be window or module exports in node.
* 2. Implicit Binding - 'this' refers to object on the left side of the dot, usually object calling the method (if its nested object, it will be the one closest to function)
* 3. New Binding - When creating new 'instance' of an object with keyword NEW, 'this' bounds to the new empty object in the moment of creation.
* 4. Explicit Binding - We can use .call, .apply or .bind to explicitly bind 'this' by passing first argument which will point to what 'this' should bind to.
*
* write out a code example of each explanation above
*/
Expand All @@ -14,15 +14,37 @@ console.log('hello world!');
// Principle 1

// code example for Window Binding
this.name = "John";
console.log(`hello ${this.name}`);

// Principle 2

// code example for Implicit Binding

const person = {name: "Black", greet: function() {return console.log(`hello ${this.name}`)}};
person.greet();



// Principle 3

// code example for New Binding
const Person = function(name) {
this.name = name,
this.greet = function() {
console.log(`hello ${this.name}`)
}
}

const newPerson = new Person("Mark");
newPerson.greet();

// Principle 4

// code example for Explicit Binding

function clap() {
return console.log(`${this.name} clapped his hands`);
}

clap.call(newPerson);