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
113 changes: 95 additions & 18 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,32 +45,109 @@
- When eating an edible, it should be pushed into a "stomach" property which is an array.
- Give persons the ability to poop.
- When pooping, the stomach should empty.
*/

TASK 2
//Person
function Person (personName,personAge) {
this.name = personName;
this.age = personAge;
this.stomach = [];
}

- Build a Car constructor that takes model name and make.
- Give cars the ability to drive a distance.
- By driving a car, the distance driven should be added to an "odometer" property.
- Give cars the ability to crash.
- A crashed car can't be driven any more. Attempts return a string "I crashed at x miles!", x being the miles in the odometer.
- Give cars the ability to be repaired.
- A repaired car can be driven again.
Person.prototype.greet = function () {
return `Hello, I am ${this.name} and I am ${this.age} years old`;
}

TASK 3
Person.prototype.eat = function(eatFood) {
return this.stomach.push(eatFood);
}

Person.prototype.poop = function() {
return this.stomach = [];
}

- Build a Baby constructor that subclasses the Person built earlier.
- Babies of course inherit the ability to greet, which can be strange.
- Babies should have the ability to play, which persons don't.
- By playing, a string is returned with some text of your choosing.

TASK 4
// TASK 2

// - Build a Car constructor that takes model name and make.
// - Give cars the ability to drive a distance.
// - By driving a car, the distance driven should be added to an "odometer" property.
// - Give cars the ability to crash.
// - A crashed car can't be driven any more. Attempts return a string "I crashed at x miles!", x being the miles in the odometer.
// - Give cars the ability to be repaired.
// - A repaired car can be driven again.

//Car
function Car (modelName, carMake) {
this.model = modelName;
this.make = carMake;
this.odometer = 0;
carGood = true;
carCrashed = false;
}

Car.prototype.drive = function(distance) {
if (carGood == true){
return this.odometer += distance;
}
return `I crashed at ${this.odometer} miles!`;
};

Car.prototype.carCrash = function() {
carCrashed = true;
carGood = false;
};

Car.prototype.repairCar = function() {
carGood = true;
carCrashed = false;
};

// TASK 3

// - Build a Baby constructor that subclasses the Person built earlier.
// - Babies of course inherit the ability to greet, which can be strange.
// - Babies should have the ability to play, which persons don't.
// - By playing, a string is returned with some text of your choosing.

//Baby
function Baby(name, age) {
Person.call(this, name, age);
}

Use your imagination and come up with constructors that allow to build objects
With amazing and original capabilities. Build 3 small ones, or a very
complicated one with lots of state. Surprise us!
Baby.prototype = Object.create(Person.prototype);
var baby = new Baby('Sonny', 2 );
Baby.prototype.play = function () {
return `Plays all day`;
}


*/
// TASK 4

// Use your imagination and come up with constructors that allow to build objects
// With amazing and original capabilities. Build 3 small ones, or a very
// complicated one with lots of state. Surprise us!


function DistanceConverter(){
this.result = 0;
};

DistanceConverter.prototype.milesToKilometers = function(fInput,sInput,length) {
if (fInput === 'miles' && sInput === 'kilometers'){
return this.result = length * 1.60934 + ' kilometers';
}
return "Enter 'miles' as first input and 'kilometers' as second input";
};

DistanceConverter.prototype.kilometersToMiles = function(fInput,sInput,length) {
if (fInput === 'kilometers' && sInput === 'miles'){
return this.result = length / 1.60934 + ' miles';
}
return "Enter 'kilometers' as first input and 'miles' as second input";
};


/*

STRETCH TASK
Expand Down
47 changes: 42 additions & 5 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,63 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
* 1. "This" refers to the window object when we do not state specifically what "this" refers to.

* 2. With Implicit Binding, when a method is invoked "this" refers to the object within which that method was used in.

* 3. "This" in new binding refers to the object that is created from a constructor object.

* 4. With Explicit binding, "this" refers to whatever context we choose to use it. We use keywords such as call, apply and bind to determine what exactly "this" refers to.
*
* write out a code example of each explanation above
*/

// Principle 1

// code example for Window Binding
function sayAge() {
return this;
}
sayAge();

// Principle 2

// code example for Implicit Binding

const me = {
firstName: 'David',
lastName: 'Kuseh',
speak: function() {
console.log(this.firstName + ' ' + this.lastName)
}
}
me.speak();

// Principle 3

// code example for New Binding
function FullName(fName,lName) {
this.firstName = fName;
this.lastName = lName;
}

FullName.prototype.logFullName = function() {
console.log(`${this.firstName} ${this.lastName}`)
}

const fullName = new FullName('David','Kuseh');
fullName.logFullName();

// Principle 4

// code example for Explicit Binding
// code example for Explicit Binding

function sayName() {
console.log(this.firstName + ' ' + this.lastName)
}

const anotherFullName = {
firstName: 'David',
lastName: 'Kuseh'
};
sayName.call(anotherFullName);