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 the Recursion.js and this.js files #61

Open
wants to merge 2 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
42 changes: 37 additions & 5 deletions classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,39 @@

// problem #1
// convert the Animal constructor function from 'constructors.js' into an ES6 class
// to test these problems you can run 'node constructors.js' in your terminal
class Animal{
constructor(options) {
this.name = options.name;

}

grow() {

console.log(this.name + ' grew larger');

}
}

const options = { // creates a new object called options
name: 'sally',
}

let Cheetah = new Animal(options); // creates a new object of animal type
Cheetah.grow();

// 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

class Cat extends Animal {
constructor(options) {
super(options)
// invoke Animal here with .call
}
}

// problem #2
// convert the Cat constructor function from 'constructors.js' into an ES6 class
Expand All @@ -11,9 +43,9 @@
// 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();

25 changes: 20 additions & 5 deletions constructors.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,22 @@

function Animal(options) {
this.name = options.name;

}

const options = {
name: 'sally',
}

// add 'grow' to Animal's prototype here
Animal.prototype.grow = function() {

console.log(this.name + ' grew larger');

}

let Cheetah = new Animal(options); // creates a new object
Cheetah.grow();

// problem #2
// setup Cat to inherit from Animal
Expand All @@ -18,16 +31,18 @@ function Animal(options) {

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

33 changes: 30 additions & 3 deletions recursion.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,21 @@ while (n <= 10) {

// code here

// what is your base case?

let count = (num) => {
if (num < 10 ) { // this is my base case
num++;
count(num);
console.log(num);
}
};

// count(0);


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

// Problem 2:
Expand All @@ -26,8 +39,22 @@ const factorial = n => {
};

console.log(factorial(5));

// write the above function in a recursive way.

const factorial2 = (n) => {



if (n === 0) { // this is my base case
return 1

}
else {
return n*factorial2(n - 1)

}

};

// when your code is ready, un-comment the next line and run the file
// console.log(recursiveFactorial());
console.log(factorial2(5));
57 changes: 51 additions & 6 deletions this.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,73 @@
/* 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 binds to the object of the window itself that you are inside of running the code.
* 2. implicet binding is binding to the object that is actually using the this. keyword
* 3. new binding is binding the this. property to the new object being created from the prototype object class or as i call it the object base class.
* 4. Explicit binding is binding from outside the object in question using a function or something else to connect to the object.
*
* write out a code example of each explanation above
*/

console.log('hello world!');

// Principle 1

// code example for Window Binding
this.car = 'convertable'; // binds this to the window itself so the windows.car = 'convertible'


// Principle 2

// code example for Implicit Binding
// create your object.
const car = {
//key: value,
color: 'red',
type: 'convertible',
speed: 0,
};

// use a function to connect to the object and allow your this to be use with the object in question.
car.carsSpeed = (speed) => {
return this.speed = 90;
}
console.log(car.carsSpeed());

// Principle 3

// code example for New Binding
let carss = function(color, type, speed) {
//key: value,
this.color = color;
this.type = type;
this.speed = speed;
};

let corvette = new carss('blue', 'sedan', 1000);

console.log(corvette);

// Principle 4

// code example for Explicit Binding

// use .call .bind .apply
// function.bind(thisArg[,arg1[,arg2[,argN]]]


// create your object.
const carsss = {
//key: value,
color: 'red',
type: 'convertible',
speed: 0,

getSpeed: function() {
return this.speed;
}
};

// use a function to connect to the object and allow your this to be use with the object in question.
const newcars = carsss.getSpeed;

const newCar = newcars.bind(carsss);
console.log(newCar());