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

JS-I-Mini complete. #65

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

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

16 changes: 11 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,19 @@ function Animal(options) {

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

// connect the prototypes here
Cat.prototype.grow = Animal.prototype.grow;

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

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

// code here
const countToTen = count => {
console.log("Recursive " + count);
// baseline
if(count === 10){return count;}
// count
return countToTen(++count);
};


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

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

// write the above function in a recursive way.

const recursiveFactorial = (n, runningNum=1, tracker=2) => {
if (tracker > n) {
return runningNum;
}else{
runningNum*=tracker;
return recursiveFactorial(n, runningNum, ++tracker);
}
}

// when your code is ready, un-comment the next line and run the file
// console.log(recursiveFactorial());
console.log(recursiveFactorial(5));
46 changes: 38 additions & 8 deletions this.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,58 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
* 1. Window/Global Binding: when there is no object to point to, this will point to the console it's running on.
* 2. Implicit Binding: using "this.functionCommand", "this" points to the object on the left.
* 3. New Binding: Makes the constructor key:values point to the object key:values being constructed
* 4. Explicit Binding: When we use "this" in a function to point to the object that is specifically being pointed to via .call() .apply() or .bind()
*
* write out a code example of each explanation above
*/

console.log('hello world!');

// Principle 1

// code example for Window Binding

// Principle 2
function someFunction1 () {
console.log(this);
return;
};
someFunction1();


// Principle 2
// code example for Implicit Binding

// Principle 3
const aboutMe = {
name: "Aaron",
age: "36",
tellMeHappyBirthday: function(name){
console.log('Happy birthday, ' + this.name + '! You are no longer ' + (this.age) + ' years old!');
}
};
aboutMe.tellMeHappyBirthday();


// Principle 3
// code example for New Binding
function terminatorConstructor(type, purpose, faction){
this.type = type;
this.purpose = purpose;
this.faction = faction;
this.declaration = function() {
console.log("I am a " + this.type + ". My purpose is " + this.purpose + ". I serve " + this.faction + ".");
//console.log(this);
}
}
const terminator1 = new terminatorConstructor('T-100', 'to seek out and terminate Sarah Connor', 'SkyNet');
const terminator2 = new terminatorConstructor('reprogrammed T-100', 'to seek out and protect John Connor', 'humans');
const terminator3 = new terminatorConstructor('T-1000', 'to look cool, and to seek out and terminate Sarah Connor', 'SkyNet');

terminator1.declaration();
//terminator2.declaration();
terminator3.declaration();

// Principle 4

// code example for Explicit Binding
terminator1.declaration.call(terminator2);