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

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


// function Animal(options) {
// this.name = options.name;
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(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',
// });
const ravioli = new Cat({
name: 'Ravioli',
});
//
// foofie.grow();
console.log(ravioli.grow());

22 changes: 15 additions & 7 deletions constructors.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,32 @@ 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
// 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
// Cat.prototype = Object.create(Animal.prototype.); <Don't need a lower inheritance level yet

function Cat(options) {
// invoke Animal here with .call
function Cat(catOptions) {
Animal.call(this, catOptions);
}
// 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 ravioli = new Cat({
name: 'Ravioli',
});

ravioli.grow();

36 changes: 24 additions & 12 deletions recursion.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,42 @@
// 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++;
}
//Note for initial PR

// let n = 1;
// while (n <= 10) {
// console.log('While Loop', n);
// n++;
//}
const countToTen = num => {
if (num > 10) return;
console.log(num);
return countToTen(++num);
};
let num = 1;

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

// code here

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

// Problem 2:

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

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

console.log(factorial(5));

// write the above function in a recursive way.
Expand Down
69 changes: 61 additions & 8 deletions this.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,81 @@
/* The for principles of "this";
/* The 4 principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
* 1. Window Binding will cause the 'this' function to refer to
the applicaiton frame of wherever you are executing the funciton. Not very Useful!
* 2. With Implicit Binding, 'this' from an object's method funciton refers to the
object directly before the '.' when called by a function (ex: me.sayName()).
It can be called with in a nested funciton also: me.mother.sayName().
* 3. New Binding is used to create a bound function that can have new objects
passed in via 'this'. It can be used with constructor functions in object oriented
programming to tie changable variables into a related function. It is defined with
the word 'New' before the bound function.
* 4. Explicit Binding can be used to override constructor objects by calling the
funciton explicitly with .call and .apply. These fucntions are very similar except
that .call uses a (...__) format or passes in variables listed out. The .apply passes in
whole arrays. There is also a .bind that can pass in variables to a funciton like .call
but is used when defining a new funciton (this seems like a different form of the New Bind function).
*
* write out a code example of each explanation above
*/

console.log('hello world!');
// console.log('hello world!');

// Principle 1

// code example for Window Binding

function sayName1(name) {
console.log(this); // will not refer to Natalie!!!
return name;
}
sayName1("Natalie");
// Principle 2

// code example for Implicit Binding

const sayNameFunc = obj => {
obj.sayName = function() {
console.log(`Hello my name is ${this.name}`);
console.log(this);
};
};

const me = { name: 'Natalie' };
const you = { name: 'Kia' };
sayNameFunc(me);
sayNameFunc(you);
// Principle 3

// code example for New Binding
let myinfo = {
name: 'Natalie',
age: 31
};

let sayName = function(hobby1, hobby2) {
console.log('My name is ' + this.name + ', I like to ' + hobby1 + ' and ' + hobby2 + '.');
};
let sayAge = function(hobby1, hobby2) {
console.log('My age is ' + this.age + ', I still like to ' + hobby1 + ' and ' + hobby2 + '.');
};

let hobbies = ['study Biology','Cook'];
let newFunction = sayName.bind(myinfo, ...hobbies);
let ageFunc = sayAge.bind(myinfo, ...hobbies);
newFunction();
ageFunc();

// Principle 4

// code example for Explicit Binding
/*let myinfo2 = {
name: 'Natalie',
age: 31
};*/

let sayName2 = function(hobby1, hobby2) {
console.log('My name is ' + this.name + ', I like to ' + hobby1 + ' and ' + hobby2 + '.');
}

// let hobbies = ['study Biology','Cook'];

sayName2.apply(myinfo, hobbies);