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
73 changes: 60 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
* trimProperties({ name: ' jane ' }) // returns a new object { name: 'jane' }
*/
function trimProperties(obj) {
// ✨ implement
const copiedObj = {
...obj.trim()
}
return copiedObj
}

/**
Expand All @@ -19,7 +22,7 @@ function trimProperties(obj) {
* trimPropertiesMutation({ name: ' jane ' }) // returns the object mutated in place { name: 'jane' }
*/
function trimPropertiesMutation(obj) {
// ✨ implement
return obj.trim()
}

/**
Expand All @@ -31,7 +34,13 @@ function trimPropertiesMutation(obj) {
* findLargestInteger([{ integer: 1 }, { integer: 3 }, { integer: 2 }]) // returns 3
*/
function findLargestInteger(integers) {
// ✨ implement
const largest = 0
integers.forEach(int => {
if (int.integer > largest) {
largest = int.integer
}
})
return largest
}

class Counter {
Expand All @@ -40,7 +49,8 @@ class Counter {
* @param {number} initialNumber - the initial state of the count
*/
constructor(initialNumber) {
// ✨ initialize whatever properties are needed
this.initialNumber = initialNumber
this.counter = initialNumber
}

/**
Expand All @@ -55,8 +65,16 @@ class Counter {
* counter.countDown() // returns 0
* counter.countDown() // returns 0
*/
countDown() {
// ✨ implement
countDown(initialNumber) {
const initialInvocation = true
if (initialInvocation) {
initialInvocation = false
return initialNumber
} else if (initialNumber > 0) {
return initialNumber -= 1
} else {
return 0
}
}
}

Expand All @@ -65,7 +83,7 @@ class Seasons {
* [Exercise 5A] Seasons creates a seasons object
*/
constructor() {
// ✨ initialize whatever properties are needed
this.seasons = {}
}

/**
Expand All @@ -81,7 +99,19 @@ class Seasons {
* seasons.next() // returns "summer"
*/
next() {
// ✨ implement
if (this.seasons === {} || this.seasons === 'spring') {
this.seasons = 'summer'
return 'summer'
} else if (this.seasons === 'summer') {
this.seasons = 'fall'
return 'fall'
} else if (this.seasons === 'fall') {
this.seasons = 'winter'
return 'winter'
} else if (this.seasons === 'winter') {
this.seasons = 'spring'
return 'spring'
}
}
}

Expand All @@ -95,7 +125,9 @@ class Car {
constructor(name, tankSize, mpg) {
this.odometer = 0 // car initilizes with zero miles
this.tank = tankSize // car initiazes full of gas
// ✨ initialize whatever other properties are needed
this.name = name
this.mpg = mpg
this.driveableDistance = this.mpg * this.tank
}

/**
Expand All @@ -112,7 +144,14 @@ class Car {
* focus.drive(200) // returns 600 (ran out of gas after 100 miles)
*/
drive(distance) {
// ✨ implement
if (this.driveableDistance > 0) {
this.tank -= distance/mpg
this.odometer += distance
} else {
this.odometer += this.driveableDistance
this.tank = 0
}
return this.odometer
}

/**
Expand All @@ -127,7 +166,12 @@ class Car {
* focus.refuel(99) // returns 600 (tank only holds 20)
*/
refuel(gallons) {
// ✨ implement
if (gallons < this.tank) {
this.tank += gallons
} else {
this.tank = this.tankSize
}
return this.tank * this.mpg
}
}

Expand All @@ -150,8 +194,11 @@ class Car {
* // error.message is "number must be a number"
* })
*/
function isEvenNumberAsync(number) {
// ✨ implement
async function isEvenNumberAsync(number) {
if (typeof number !== 'number' || isNaN(number)) {
throw new Error('number must be a number')
}
return number % 2 === 0 ? true : false
}

module.exports = {
Expand Down
139 changes: 117 additions & 22 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,55 +8,150 @@ describe('[Exercise 1] trimProperties', () => {
const actual = utils.trimProperties(input)
expect(actual).toEqual(expected)
})
test.todo('[2] returns a copy, leaving the original object intact')
test('[2] returns a copy, leaving the original object intact', () => {
const input = { foo: ' foo ', bar: 'bar ', baz: ' baz' }
const expected = { foo: 'foo', bar: 'bar', baz: 'baz' }
const actual = utils.trimProperties(input)
expect(actual).toEqual(expected)
expect(expected).not.toBe(actual).trimProperties()
})
})

describe('[Exercise 2] trimPropertiesMutation', () => {
test.todo('[3] returns an object with the properties trimmed')
test.todo('[4] the object returned is the exact same one we passed in')
test('[3] returns an object with the properties trimmed', () => {
const input = { foo: ' foo ', bar: 'bar ', baz: ' baz' }
const expected = { foo: 'foo', bar: 'bar', baz: 'baz' }
const actual = utils.trimProperties(input)
expect(actual).toEqual(expected)
})
test('[4] the object returned is the exact same one we passed in', () => {
const input = { foo: ' foo ', bar: 'bar ', baz: ' baz' }
const expected = { foo: 'foo', bar: 'bar', baz: 'baz' }
const actual = utils.trimProperties(input)
expect(actual).toBe(input.trimProperties())
})
})

describe('[Exercise 3] findLargestInteger', () => {
test.todo('[5] returns the largest number in an array of objects { integer: 2 }')
test('[5] returns the largest number in an array of objects { integer: 2 }', () => {
const input = [{ integer: 2 }, { integer: 5 }, { integer: 3 }]
const actual = utils.findLargestInteger(input)
expect(actual).toBe(5)
})
})

describe('[Exercise 4] Counter', () => {
let counter
beforeEach(() => {
counter = new utils.Counter(3) // each test must start with a fresh couter
})
test.todo('[6] the FIRST CALL of counter.countDown returns the initial count')
test.todo('[7] the SECOND CALL of counter.countDown returns the initial count minus one')
test.todo('[8] the count eventually reaches zero but does not go below zero')
test('[6] the FIRST CALL of counter.countDown returns the initial count', () => {
expect(counter.countDown()).toBe(3)
})
test('[7] the SECOND CALL of counter.countDown returns the initial count minus one', () => {
counter.countDown()
expect(counter.countDown()).toBe(2)
})
test('[8] the count eventually reaches zero but does not go below zero', () => {
counter.countDown()
counter.countDown()
counter.countDown()
counter.countDown()
expect(counter.countDown()).toBe(0)
})
})

describe('[Exercise 5] Seasons', () => {
let seasons
beforeEach(() => {
seasons = new utils.Seasons() // each test must start with fresh seasons
})
test.todo('[9] the FIRST call of seasons.next returns "summer"')
test.todo('[10] the SECOND call of seasons.next returns "fall"')
test.todo('[11] the THIRD call of seasons.next returns "winter"')
test.todo('[12] the FOURTH call of seasons.next returns "spring"')
test.todo('[13] the FIFTH call of seasons.next returns again "summer"')
test.todo('[14] the 40th call of seasons.next returns "spring"')
test('[9] the FIRST call of seasons.next returns "summer"', () => {
expect(seasons.next()).toBe('summer')
})
test('[10] the SECOND call of seasons.next returns "fall"', () => {
seasons.next()
expect(seasons.next()).toBe('fall')
})
test('[11] the THIRD call of seasons.next returns "winter"', () => {
seasons.next()
seasons.next()
expect(seasons.next()).toBe('winter')
})
test('[12] the FOURTH call of seasons.next returns "spring"', () => {
seasons.next()
seasons.next()
seasons.next()
expect(seasons.next()).toBe('spring')
})
test('[13] the FIFTH call of seasons.next returns again "summer"', () => {
seasons.next()
seasons.next()
seasons.next()
seasons.next()
expect(seasons.next()).toBe('summer')

})
test('[14] the 40th call of seasons.next returns "spring"', () => {
for (let i = 0; i < 39; i++) {
seasons.next()
}
expect(seasons.next()).toBe('spring')
})
})

describe('[Exercise 6] Car', () => {
let focus
beforeEach(() => {
focus = new utils.Car('focus', 20, 30) // each test must start with a fresh car
})
test.todo('[15] driving the car returns the updated odometer')
test.todo('[16] driving the car uses gas')
test.todo('[17] refueling allows to keep driving')
test.todo('[18] adding fuel to a full tank has no effect')
test('[15] driving the car returns the updated odometer', () => {
expect(focus.drive(100)).toHaveProperty('odometer', 100)
})
test('[16] driving the car uses gas', () => {
focus.drive(600)
expect(focus.drive(1)).toBe(600)
expect(focus.tank).toBe(0)
})
test('[17] refueling allows to keep driving', () => {
focus.drive(600)
focus.refuel(0)
focus.drive(600)
expect(focus.odometer).toBe(600)
focus.refuel(50)
focus.drive(600)
expect(focus.odometer).toBe(1200)
})
test('[18] adding fuel to a full tank has no effect', () => {
focus.drive(600)
focus.refuel(100)
focus.drive(700)
expect(focus.odometer).toBe(1200)
})
})

describe('[Exercise 7] isEvenNumberAsync', () => {
test.todo('[19] resolves true if passed an even number')
test.todo('[20] resolves false if passed an odd number')
test.todo('[21] rejects an error with the message "number must be a number" if passed a non-number type')
test.todo('[22] rejects an error with the message "number must be a number" if passed NaN')
})
test('[19] resolves true if passed an even number', async () => {
const result = await utils.isEvenNumberAsync(2)
expect(result).toReturn(true)

})
test('[20] resolves false if passed an odd number', async () => {
const result = await utils.isEvenNumberAsync(3)
expect(result).toReturn(false)
})
test('[21] rejects an error with the message "number must be a number" if passed a non-number type', async () => {
try {
await utils.isEvenNumberAsync('foo')
} catch (error) {
expect(error.message).toMatch(/number must be a number/i)
}
})
test('[22] rejects an error with the message "number must be a number" if passed NaN', async () => {
try {
await utils.isEvenNumberAsync(NaN)
} catch (error) {
expect(error.message).toMatch(/number must be a number/i)
}
})
})