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

Completed Recursion, This, Constructors & Classes.js #75

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
23 changes: 18 additions & 5 deletions classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,30 @@
// problem #1
// convert the Animal constructor function from 'constructors.js' into an ES6 class

class Animal {
constructor(options) {
this.name = options.name;
}
grow() {
return `${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();

18 changes: 12 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() {
return `${this.name} grew larger!`;
}

// problem #2
// setup Cat to inherit from Animal
Expand All @@ -17,17 +20,20 @@ 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);
}
// invoke Animal here with .call

Cat.prototype = Object.create(Animal.prototype);

// connect the prototypes here

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

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

// code here
const countToTen = (num = 1) => {
console.log(num);
if (num === 10) {
return "";
} else {
return countToTen(num + 1);
}
}

// 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 +39,12 @@ console.log(factorial(5));

// write the above function in a recursive way.

const recursiveFactorial = num => {
if (num === 1) {
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());
95 changes: 91 additions & 4 deletions this.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
* 1. Explicit Binding - Using the context of what the keyword should be Call, Apply or Bind

* 2. Implicit Binding - Invoking the object left of the dot

* 3. New Binding - A given constructor will invoke a brand new Object in alphabetical order. Or when the new function in invoked with new keyword, that keyword inside the function is bond to the new Object being constructed

* 4. Window Binding - Referencing a keyword inside the function, it will default to the 'window' keyword object
*
* write out a code example of each explanation above
*/
Expand All @@ -15,14 +18,98 @@ console.log('hello world!');

// code example for Window Binding

var sayAge = function() {
console.log(this.age);
};
var me = {
age: 25
};

sayAge();
window.age = 35;
sayAge();
//output - undefined
// 35

// Principle 2

// code example for Implicit Binding

const dog = {
name: "Kenzie",
breed: "collie",
color: "black",
voice: function() {
console.log(this.breed);
}
}
dog.breed();
//output "collie"


// Principle 3

// code example for New Binding

var Dog = function(breed, age, color) {
this.breed = breed;
this.age = age;
this.color = color
};

var Kenzie = new Dog('collie', '8', 'black');
console.log(Kenzie);

// output [object Object] {
// age: “8”,
// breed: “collie”,
// color: “black”

// Principle 4

// code example for Explicit Binding

// CALL
var sayName = function(lang1, lang2, lang3) {
console.log('My name is ' + this.name + ' and I know ' + lang1 + ', ' + lang2 + ', and ' + lang3);
};

var amanda = {
name: 'Amanda',
age: 18,
};

var languages = ['Javascript', 'Ruby', 'Flexbox'];

sayName.call(amanda, languages[0], languages[1], languages[2]);
//output “My name is Amanda and I know Javascript, Ruby, and Flexbox”

// APPLY
var sayName = function(lang1, lang2, lang3) {
console.log('My name is ' + this.name + ' and I know ' + lang1 + ', ' + lang2 + ', and ' + lang3);
};
var amanda = {
name: 'Amanda',
age: 18,
};
var languages = ['Javascript', 'Ruby', 'Flexbox'];

sayName.apply(amanda, languages)
//output “My name is Amanda and I know Javascript, Ruby, and Flexbox”


// BIND
var sayName = function(lang1, lang2, lang3) {
console.log('My name is ' + this.name + ' and I know ' + lang1 + ', ' + lang2 + ', and ' + lang3);
};
var amanda = {
name: 'Amanda',
age: 18,
};
var languages = ['Javascript', 'Ruby', 'Flexbox'];

var newFunction = sayName.bind(amanda, languages[0], languages[1], languages[2])
console.log("NOW");
newFunction();
//output "NOW"
//output "My name is Amanda and I know Javascript, Ruby, and Flexbox"