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
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() {
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',
// });
//
// foofie.grow();
const foofie = new Cat({
name: 'foofie',
});

console.log(foofie.grow());

19 changes: 14 additions & 5 deletions constructors.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ function Animal(options) {
}

// add 'grow' to Animal's prototype here
Animal.prototype.grow = function() {
return `${this.name} grew larger!`
}

// Person.prototype.speak = function () {
// return `Hello, my name is ${this.name}`;
// };

// problem #2
// setup Cat to inherit from Animal
Expand All @@ -18,16 +25,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',
});

console.log(foofie.grow());

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

// code here
let m = 1;
let countToTen = function(m) {
if (m === 10) return console.log('While Loop II', m);
console.log('While Loop II', m);
m++;
return countToTen(m);
}

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

// Problem 2:

const factorial = n => {
let result = 1;
for (let i = 2; i <= n; i++) {
result *= i;
result *= i; // 1 * 2, 2 * 3, 6 * 4, 24 * 5
}
return result;
};

console.log(factorial(5));

// write the above function in a recursive way.
let fresult = 1;
let recursiveFactorial = (k) => {
if (k === 1) return console.log(fresult);
fresult *= k;
// console.log("K is: " + k + "fresult: " +fresult);
k--;
recursiveFactorial(k);

}
// factorialT(5);



// when your code is ready, un-comment the next line and run the file
// console.log(recursiveFactorial());
console.log(recursiveFactorial(5));
27 changes: 26 additions & 1 deletion this.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,46 @@
*
* write out a code example of each explanation above
*/
1. Implicit Binding. When you call a function within an object (called a method), the method's this
statement uses properties attached to the object. In the following, hello is the object and myFunc()
is a method. Example: hello.myFunc() .

2. Explicit Binding. Is when you want to pass data that's not attched to an object or function to
do a task on that object or function. If the object or function is not connected to some variable
that has data that you want to use on the object or function, you use EXPLICIT BINDING. Such as
.call(), .apply() , or .bind(). In the following hello is a function and arVar is a variable passed
to the hello function. Example: hello.call(argVar) .

3. New Binding. It's used to create a new object from an object constructor, ES5 and ES6 constructor
function (class in ES6) look different, but the syntax to create new object is the same. When you create
a new object with the "New" keyword (let objname = New ClassConstruct(prop1, prop2, prop3, etc.), the
object's constructors this.prop are bound to the new object created. So, the .this references the new object.
Example: let newvar = New Hello("prop1","prop2", "etc of new object") .


4. Window Binding. When you don't have data for a task, such as a function doing a task with data, and the function didn't receive the
data by other methods (Implicit binding, Explicit binding, or New binding), than the function will look for
data in the container, or window. The following doesn't have anything specfic to console.log, and it will
log the windows's (or container's) this references. Example: console.log(this)

console.log('hello world!');

// Principle 1

// code example for Window Binding
console.log(this)

// Principle 2

// code example for Implicit Binding
hello.myFunc()

// Principle 3

// code example for New Binding
let newvar = New Hello("prop1","prop2", "etc of new object")

// Principle 4

// code example for Explicit Binding

hello.call(argVar)
32 changes: 32 additions & 0 deletions yarn-error.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
Arguments:
/home/linuxbrew/.linuxbrew/Cellar/node/9.6.1/bin/node /usr/share/yarn/bin/yarn.js run

PATH:
/home/linuxbrew/.linuxbrew/bin:/home/linuxbrew/.linuxbrew/sbin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/home/abueno/.local/bin:/home/abueno/bin

Yarn version:
1.5.1

Node version:
9.6.1

Platform:
linux x64

npm manifest:
No manifest

yarn manifest:
No manifest

Lockfile:
No lockfile

Trace:
Error: Couldn't find a package.json file in "/home/abueno/LambdaSchool/JavaScript-II-Mini"
at new MessageError (/usr/share/yarn/lib/cli.js:186:110)
at /usr/share/yarn/lib/cli.js:40048:15
at Generator.next (<anonymous>)
at step (/usr/share/yarn/lib/cli.js:98:30)
at /usr/share/yarn/lib/cli.js:109:13
at <anonymous>