From 92c43bdaa7a4252e42930740d0a1c7b7e4c64cca Mon Sep 17 00:00:00 2001 From: Christopher-Martinez-422 <107010274+Christopher-Martinez-422@users.noreply.github.com> Date: Fri, 2 Sep 2022 11:34:47 -0700 Subject: [PATCH] first commit --- index.js | 75 ++++++++++++++++++++++++++++++++++++++++------- package-lock.json | 4 +-- 2 files changed, 67 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index 43db2abe..cf5d4771 100644 --- a/index.js +++ b/index.js @@ -15,9 +15,36 @@ + It should return a string with `name` and `age`. Example: "Mary, 50" */ -function Person() { +function Person(name, age) { + this.name = name; + this.age = age; + this.stomach = []; + } -} + Person.prototype.eat = function(edible){ + if(this.stomach.length <= 10){ + this.stomach.push(edible); + } + } + + Person.prototype.poop = function(){ + this.stomach = []; + } + + Person.prototype.toString = function(){ + return `${this.name}, ${this.age}`; + } + + + const Christopher = new Person ('Christopher', 30); + + console.log(Christopher.eat('tacos')); + console.log(Christopher.eat('gyros')); + console.log(Christopher.eat('sandwiches')); + console.log(Christopher.stomach); + + Christopher.poop(); + console.log(Christopher.stomach); /* @@ -36,8 +63,25 @@ function Person() { + The `drive` method should return a string "I ran out of fuel at x miles!" x being `odometer`. */ -function Car() { - +function Car(model, milesPerGallon) { + this.model = model; + this.milesPerGallon = milesPerGallon; + this.tank = 0; + this.odometer = 0; +} +Car.prototype.fill = function(gallons){ + this.tank = this.tank + gallons; +} +Car.prototype.drive = function(distance){ + const drivableMiles = this.tank * this.milesPerGallon; + if (distance <= drivableMiles){ + this.odometer = this.odometer + distance; + this.tank = this.tank - (distance/this.milesPerGallon) + }else{ + this.odometer = this.odometer + drivableMiles; + this.tank = 0; + return `I ran out of fuel at ${this.odometer} miles!` + } } @@ -49,18 +93,29 @@ function Car() { + Should return a string "Playing with x", x being the favorite toy. */ -function Baby() { +function Baby(name, age, favoriteToy) { + Person.call(this, age, name) + this.name = name; + this.age = age; + this.favoriteToy = favoriteToy; + Baby.prototype.play = function() { + return `Playing with ${favoriteToy}.`; + } -} + Person.call(this, name, age ) + } + + + Baby.prototype = Object.create(Person.prototype) /* TASK 4 In your own words explain the four principles for the "this" keyword below: - 1. - 2. - 3. - 4. + 1. Window/Global Object Binding: in the global scope the value of this is the whole window. + 2. Implicit Binding: when you use .this the object before the . is this + 3. New Binding: when using a constructor function this refers to the object that is created by the constructor function + 4. Explicit Binding: when you use call or apply, this is explicitly defined. */ ///////// END OF CHALLENGE ///////// diff --git a/package-lock.json b/package-lock.json index d758c657..4a96a12c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "codegraded-project-js", - "version": "0.0.8", + "version": "0.0.9", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "codegraded-project-js", - "version": "0.0.8", + "version": "0.0.9", "devDependencies": { "@babel/core": "7.17.8", "@babel/plugin-transform-runtime": "7.17.0",