From 9d8398e2ece0ffff3a524955971786ce3d656982 Mon Sep 17 00:00:00 2001 From: Daniel Brannon Date: Thu, 20 May 2021 07:41:48 -0500 Subject: [PATCH 1/2] hope i got index.js right --- index.js | 67 ++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 55 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index 28090f12..9377b501 100644 --- a/index.js +++ b/index.js @@ -7,7 +7,10 @@ * trimProperties({ name: ' jane ' }) // returns a new object { name: 'jane' } */ function trimProperties(obj) { - // ✨ implement + const copiedObj = { + ...obj.trim() + } + return copiedObj } /** @@ -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() } /** @@ -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 { @@ -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 } /** @@ -56,7 +66,11 @@ class Counter { * counter.countDown() // returns 0 */ countDown() { - // ✨ implement + if (this.counter > 0) { + return this.counter -= 1 + } else { + return 0 + } } } @@ -65,7 +79,7 @@ class Seasons { * [Exercise 5A] Seasons creates a seasons object */ constructor() { - // ✨ initialize whatever properties are needed + this.seasons = {} } /** @@ -81,7 +95,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' + } } } @@ -95,7 +121,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 } /** @@ -112,7 +140,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 } /** @@ -127,7 +162,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 } } @@ -150,8 +190,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 = { From 79eb1b492b1552123bff15fac6ab98b7d7d23628 Mon Sep 17 00:00:00 2001 From: Daniel Brannon Date: Thu, 20 May 2021 08:14:36 -0500 Subject: [PATCH 2/2] finished --- index.js | 16 +++--- index.test.js | 139 ++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 127 insertions(+), 28 deletions(-) diff --git a/index.js b/index.js index 9377b501..ebb7e9da 100644 --- a/index.js +++ b/index.js @@ -65,12 +65,16 @@ class Counter { * counter.countDown() // returns 0 * counter.countDown() // returns 0 */ - countDown() { - if (this.counter > 0) { - return this.counter -= 1 - } else { - return 0 - } + countDown(initialNumber) { + const initialInvocation = true + if (initialInvocation) { + initialInvocation = false + return initialNumber + } else if (initialNumber > 0) { + return initialNumber -= 1 + } else { + return 0 + } } } diff --git a/index.test.js b/index.test.js index b9d30ded..632bc0cd 100644 --- a/index.test.js +++ b/index.test.js @@ -8,16 +8,36 @@ 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', () => { @@ -25,9 +45,20 @@ describe('[Exercise 4] 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', () => { @@ -35,12 +66,38 @@ describe('[Exercise 5] 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', () => { @@ -48,15 +105,53 @@ describe('[Exercise 6] Car', () => { 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) + } + }) +}) \ No newline at end of file