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

Mathias Bakken #64

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

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

20 changes: 11 additions & 9 deletions constructors.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

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

// 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
// the Animal constructor needs to be invoked with the 'options' argument
Expand All @@ -18,16 +20,16 @@ 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();
16 changes: 13 additions & 3 deletions recursion.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ while (n <= 10) {
// write a recursive - function called countToTen that mimics the while loop above.

// code here

let countToTen = function(num) {
if (num > 10) return num; // base
console.log('recursive', num);
countToTen(++num); // in this case, return isn't necessary.
};
// when you code is ready, un-comment the next line and run the file
// console.log(countToTen());
countToTen(1);
/* ================ Next Problem ================= */

// Problem 2:
Expand All @@ -28,6 +32,12 @@ const factorial = n => {
console.log(factorial(5));

// write the above function in a recursive way.
const recursiveFactorial = (n) => {
let result = 1;
if (n === 1 || n === 0) return n;
result *= n * recursiveFactorial(n - 1);
return result;
};

// when your code is ready, un-comment the next line and run the file
// console.log(recursiveFactorial());
console.log(recursiveFactorial(5));
32 changes: 25 additions & 7 deletions this.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* The for principles of "this";
/* The four principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
* 1. The global scope of this is calling on properties of the window object.
* 2. Explicit - calling from another function to a specified global object
* 3. Implicit - calling from within object
* 4. new - building a new object using another objects parameters for creating new objects properties
*
* write out a code example of each explanation above
*/
Expand All @@ -14,15 +14,33 @@ console.log('hello world!');
// Principle 1

// code example for Window Binding

console.log(this);
// Principle 2

// code example for Implicit Binding

const me = {
name: 'Matthias',
hello: function() {
console.log(`Hi, ${this.name}`)
}
};
// Principle 3

// code example for New Binding
const Animal = function(color, name, eats) {
this.color = color;
this.name = name;
this.eats = eats;
};

const giraf = new Animal('Spotted', 'Frankie', 'Babies');
// Principle 4

// code example for Explicit Binding
let sayName = function (skill1, skill2, skill3) {
console.log(`My name is ${this.name} and I like to program with ${skill1}, ${skill2} and ${skill3}.`);
};

let skills = ['HTML', 'CSS', 'JS']

sayName.apply(me, skills);