forked from bloominstituteoftechnology/node-testing1-project
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
196 lines (185 loc) · 5.38 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
* [Exercise 1] trimProperties copies an object trimming its properties
* @param {object} obj - an object with properties that are strings
* @returns {object} - a copy of the object with strings trimmed
*
* EXAMPLE
* trimProperties({ name: ' jane ' }) // returns a new object { name: 'jane' }
*/
function trimProperties(obj) {
const result = {}
for (let i in obj) {
result[i] = obj[i].trim()
}
return result
}
/**
* [Exercise 2] trimPropertiesMutation trims in place the properties of an object
* @param {object} obj - an object with properties that are strings
* @returns {object} - the same object with strings trimmed
*
* EXAMPLE
* trimPropertiesMutation({ name: ' jane ' }) // returns the object mutated in place { name: 'jane' }
*/
function trimPropertiesMutation(obj) {
for (let i in obj) {
obj[i] = obj[i].trim()
}
return obj
}
/**
* [Exercise 3] findLargestInteger finds the largest integer in an array of objects { integer: 1 }
* @param {object[]} integers - an array of objects
* @returns {number} - the largest integer
*
* EXAMPLE
* findLargestInteger([{ integer: 1 }, { integer: 3 }, { integer: 2 }]) // returns 3
*/
function findLargestInteger(integers) {
let result = integers[0].integer
for (let i = 1; i < integers.length; i++) {
if (integers[i].integer > result) {
result = integers[i].integer
}
}
return result
}
class Counter {
/**
* [Exercise 4A] Counter creates a counter
* @param {number} initialNumber - the initial state of the count
*/
constructor(initialNumber) {
this.count = initialNumber
}
/**
* [Exercise 4B] Counter.prototype.countDown counts down to zero
* @returns {number} - the next count, does not go below zero
*
* EXAMPLE
* const counter = new Counter(3)
* counter.countDown() // returns 3
* counter.countDown() // returns 2
* counter.countDown() // returns 1
* counter.countDown() // returns 0
* counter.countDown() // returns 0
*/
countDown() {
if (this.count > 0) {
return this.count--
}
return this.count
}
}
class Seasons {
/**
* [Exercise 5A] Seasons creates a seasons object
*/
constructor() {
this.seasons =['summer', 'fall', 'winter', 'spring']
this.currentSeason = 0
}
/**
* [Exercise 5B] Seasons.prototype.next returns the next season
* @returns {string} - the next season starting with "summer"
*
* EXAMPLE
* const seasons = new Seasons()
* seasons.next() // returns "summer"
* seasons.next() // returns "fall"
* seasons.next() // returns "winter"
* seasons.next() // returns "spring"
* seasons.next() // returns "summer"
*/
next() {
const result = this.seasons[this.currentSeason]
this.currentSeason === 3 ? this.currentSeason = 0 : this.currentSeason++
return result // ✨ implement
}
}
class Car {
/**
* [Exercise 6A] Car creates a car object
* @param {string} name - the name of the car
* @param {number} tankSize - capacity of the gas tank in gallons
* @param {number} mpg - miles the car can drive per gallon of gas
*/
constructor(name, tankSize, mpg) {
this.odometer = 0 // car initilizes with zero miles
this.tank = tankSize // car initiazes full of gas
this.tankSize = tankSize
this.mpg = mpg
}
/**
* [Exercise 6B] Car.prototype.drive adds miles to the odometer and consumes fuel according to mpg
* @param {string} distance - the distance we want the car to drive
* @returns {number} - the updated odometer value
*
* EXAMPLE
* const focus = new Car('focus', 20, 30)
* focus.drive(100) // returns 100
* focus.drive(100) // returns 200
* focus.drive(100) // returns 300
* focus.drive(200) // returns 500
* focus.drive(200) // returns 600 (ran out of gas after 100 miles)
*/
drive(distance) {
const milesCanDrive = this.tank * this.mpg
if (distance < milesCanDrive) {
this.odometer = this.odometer + distance
this.tank = this.tank - (distance / this.mpg)
} else {
this.odometer = this.odometer + milesCanDrive
this.tank = 0
}
return this.odometer // ✨ implement
}
/**
* [Exercise 6C] Adds gallons to the tank
* @param {number} gallons - the gallons of fuel we want to put in the tank
* @returns {number} - the miles that can be driven after refueling
*
* EXAMPLE
* const focus = new Car('focus', 20, 30)
* focus.drive(600) // returns 600
* focus.drive(1) // returns 600 (no distance driven as tank is empty)
* focus.refuel(99) // returns 600 (tank only holds 20)
*/
refuel(gallons) {
const gallonsThatFit = this.tankSize - this.tank
if ( gallons <= gallonsThatFit) {
this.tank = this.tank +gallons
} else {
this.tank = this.tankSize
}
return this.tank * this.mpg
}
}
/**
* [Exercise 7] Asynchronously resolves whether a number is even
* @param {number} number - the number to test for evenness
* @returns {promise} - resolves true if number even, false otherwise
*
* EXAMPLE
* isEvenNumberAsync(2).then(result => {
* // result is true
* })
* isEvenNumberAsync(3).then(result => {
* // result is false
* })
*/
function isEvenNumberAsync(number) {
if ( number %2 ===0 ) {
return Promise.resolve (true)
}
return Promise.resolve (false)
}
module.exports = {
trimProperties,
trimPropertiesMutation,
findLargestInteger,
isEvenNumberAsync,
Counter,
Seasons,
Car,
}