-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
116 lines (98 loc) · 3.39 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
EXAMPLE TASK:
- Write an Airplane constructor that initializes `name` from an argument.
- All airplanes built with Airplane should initialize with an `isFlying` of false.
- Give airplanes the ability to `.takeOff()` and `.land()`:
+ If a plane takes off, its `isFlying` property is set to true.
+ If a plane lands, its `isFlying` property is set to false.
*/
// EXAMPLE SOLUTION CODE:
function Airplane(name) {
this.name = name;
this.isFlying = false;
}
Airplane.prototype.takeOff = function () {
this.isFlying = true;
};
Airplane.prototype.land = function () {
this.isFlying = false;
};
/*
// 👇 COMPLETE YOUR WORK BELOW 👇
// 👇 COMPLETE YOUR WORK BELOW 👇
// 👇 COMPLETE YOUR WORK BELOW 👇
*/
/*
TASK 1
- Write a Person Constructor that initializes `name` and `age` from arguments.
- All instances of Person should initialize with an empty `stomach` array.
- Give instances of Person the ability to `.eat("someFood")`:
+ When eating an edible, it should be pushed into the `stomach`.
+ The `eat` method should have no effect if there are 10 items in the `stomach`.
- Give instances of Person the ability to `.poop()`:
+ When an instance poops, its `stomach` should empty.
- Give instances of Person a method `.toString()`:
+ It should return a string with `name` and `age`. Example: "Mary, 50"
*/
function Person(name, age) {
this.name = name;
this.age = age;
this.stomach = [];
}
Person.prototype.eat = function(food) {
if (this.stomach.length < 10) {
this.stomach.push(food);
}
}
Person.prototype.poop = function() {
this.stomach = [];
}
Person.prototype.toString = function () {
return this.name + ", " + this.age
}
/*
TASK 2
- Write a Car constructor that initializes `model` and `milesPerGallon` from arguments.
- All instances built with Car:
+ should initialize with an `tank` at 0
+ should initialize with an `odometer` at 0
- Give cars the ability to get fueled with a `.fill(gallons)` method. Add the gallons to `tank`.
- STRETCH: Give cars ability to `.drive(distance)`. The distance driven:
+ Should cause the `odometer` to go up.
+ Should cause the the `tank` to go down taking `milesPerGallon` into account.
- STRETCH: A car which runs out of `fuel` while driving can't drive any more distance:
+ The `drive` method should return a string "I ran out of fuel at x miles!" x being `odometer`.
*/
function Car(model, milesPerGallon) {
this.model = model;
this.milesPerGallon = milesPerGallon;
this.tank.odometer =[0];
Car.prototype.
}
/*
TASK 3
- Write a Baby constructor subclassing Person.
- Besides `name` and `age`, Baby takes a third argument to initialize `favoriteToy`.
- Besides the methods on Person.prototype, babies have the ability to `.play()`:
+ Should return a string "Playing with x", x being the favorite toy.
*/
function Baby() {
}
/*
TASK 4
In your own words explain the four principles for the "this" keyword below:
1.
2.
3.
4.
*/
///////// END OF CHALLENGE /////////
///////// END OF CHALLENGE /////////
///////// END OF CHALLENGE /////////
if (typeof exports !== 'undefined') {
module.exports = module.exports || {}
if (Airplane) { module.exports.Airplane = Airplane }
if (Person) { module.exports.Person = Person }
if (Car) { module.exports.Car = Car }
if (Baby) { module.exports.Baby = Baby }
}