From 3f92b80600c30ddc12228e0f7ef70e3cfe8873d6 Mon Sep 17 00:00:00 2001 From: joey-bertschler Date: Wed, 19 Jan 2022 08:28:42 +0400 Subject: [PATCH 1/7] trim --- index.js | 6 ++++++ index.test.js | 6 +++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 3df06ec1..0fca60c5 100644 --- a/index.js +++ b/index.js @@ -8,8 +8,14 @@ */ function trimProperties(obj) { // ✨ implement + const result = {} + for (let key in obj) { + result[key] = obj[key].trim(); //not sure if call key or prop + } + return result; } + /** * [Exercise 2] trimPropertiesMutation trims in place the properties of an object * @param {object} obj - an object with properties that are strings diff --git a/index.test.js b/index.test.js index cfb0a0b3..0a1eb8f9 100644 --- a/index.test.js +++ b/index.test.js @@ -8,7 +8,11 @@ describe('[Exercise 1] trimProperties', () => { const actual = utils.trimProperties(input) expect(actual).toEqual(expected) }) - // test('[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' } + utils.trimProperties(input) + expect(input).toEqual({ foo: ' foo ', bar: 'bar ', baz: ' baz' }) + }) }) describe('[Exercise 2] trimPropertiesMutation', () => { From d9e40459a8f0990da5384f54392425fbd7072f68 Mon Sep 17 00:00:00 2001 From: joey-bertschler Date: Wed, 19 Jan 2022 08:39:03 +0400 Subject: [PATCH 2/7] trimMutation --- index.js | 4 ++++ index.test.js | 15 +++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 0fca60c5..730235aa 100644 --- a/index.js +++ b/index.js @@ -26,6 +26,10 @@ function trimProperties(obj) { */ function trimPropertiesMutation(obj) { // ✨ implement + for (let key in obj) { + obj[key] = obj[key].trim(); //not sure if call key or prop + } + return obj; } /** diff --git a/index.test.js b/index.test.js index 0a1eb8f9..037342e9 100644 --- a/index.test.js +++ b/index.test.js @@ -16,8 +16,19 @@ describe('[Exercise 1] trimProperties', () => { }) describe('[Exercise 2] trimPropertiesMutation', () => { - // test('[3] returns an object with the properties trimmed', () => {}) - // test('[4] the object returned is the exact same one we passed in', () => {}) + test('[3] returns an object with the properties trimmed', () => { + // EXAMPLE + 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 actual = utils.trimPropertiesMutation(input) + expect(actual).toBe(input) + }) }) describe('[Exercise 3] findLargestInteger', () => { From d118c6f05057da45dcef13475c88a5858f483f3a Mon Sep 17 00:00:00 2001 From: joey-bertschler Date: Wed, 19 Jan 2022 09:25:01 +0400 Subject: [PATCH 3/7] countdown --- index.js | 13 +++++++++++++ index.test.js | 49 +++++++++++++++++++++++++++++++++---------------- 2 files changed, 46 insertions(+), 16 deletions(-) diff --git a/index.js b/index.js index 730235aa..d3854dee 100644 --- a/index.js +++ b/index.js @@ -42,6 +42,13 @@ function trimPropertiesMutation(obj) { */ function findLargestInteger(integers) { // ✨ implement + let largest = 0; + for (let i = 0; i < integers.length; i++) { + if (integers[i].integer > largest) { + largest = integers[i].integer; + } + } + return largest; } class Counter { @@ -51,6 +58,7 @@ class Counter { */ constructor(initialNumber) { // ✨ initialize whatever properties are needed + this.count = initialNumber; } /** @@ -67,6 +75,11 @@ class Counter { */ countDown() { // ✨ implement + if (this.count > 0) { + return this.count--; + } + return this.count; + } } diff --git a/index.test.js b/index.test.js index 037342e9..bcf5f1ba 100644 --- a/index.test.js +++ b/index.test.js @@ -32,7 +32,13 @@ describe('[Exercise 2] trimPropertiesMutation', () => { }) describe('[Exercise 3] findLargestInteger', () => { - // test('[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 }', () => { + // EXAMPLE + const input = [{ integer: 1 }, { integer: 3 }, { integer: 2 }] + const expected = 3 + const actual = utils.findLargestInteger(input) + expect(actual).toEqual(expected) + }) }) describe('[Exercise 4] Counter', () => { @@ -40,9 +46,20 @@ describe('[Exercise 4] Counter', () => { beforeEach(() => { counter = new utils.Counter(3) // each test must start with a fresh couter }) - // test('[6] the FIRST CALL of counter.countDown returns the initial count', () => {}) - // test('[7] the SECOND CALL of counter.countDown returns the initial count minus one', () => {}) - // test('[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()).toEqual(3) + }) + test('[7] the SECOND CALL of counter.countDown returns the initial count minus one', () => { + counter.countDown() + expect(counter.countDown()).toEqual(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()).toEqual(0) + }) }) describe('[Exercise 5] Seasons', () => { @@ -50,12 +67,12 @@ describe('[Exercise 5] Seasons', () => { beforeEach(() => { seasons = new utils.Seasons() // each test must start with fresh seasons }) - // test('[9] the FIRST call of seasons.next returns "summer"', () => {}) - // test('[10] the SECOND call of seasons.next returns "fall"', () => {}) - // test('[11] the THIRD call of seasons.next returns "winter"', () => {}) - // test('[12] the FOURTH call of seasons.next returns "spring"', () => {}) - // test('[13] the FIFTH call of seasons.next returns again "summer"', () => {}) - // test('[14] the 40th call of seasons.next returns "spring"', () => {}) + test('[9] the FIRST call of seasons.next returns "summer"', () => {}) + test('[10] the SECOND call of seasons.next returns "fall"', () => {}) + test('[11] the THIRD call of seasons.next returns "winter"', () => {}) + test('[12] the FOURTH call of seasons.next returns "spring"', () => {}) + test('[13] the FIFTH call of seasons.next returns again "summer"', () => {}) + test('[14] the 40th call of seasons.next returns "spring"', () => {}) }) describe('[Exercise 6] Car', () => { @@ -63,13 +80,13 @@ describe('[Exercise 6] Car', () => { beforeEach(() => { focus = new utils.Car('focus', 20, 30) // each test must start with a fresh car }) - // test('[15] driving the car returns the updated odometer', () => {}) - // test('[16] driving the car uses gas', () => {}) - // test('[17] refueling allows to keep driving', () => {}) - // test('[18] adding fuel to a full tank has no effect', () => {}) + test('[15] driving the car returns the updated odometer', () => {}) + test('[16] driving the car uses gas', () => {}) + test('[17] refueling allows to keep driving', () => {}) + test('[18] adding fuel to a full tank has no effect', () => {}) }) describe('[Exercise 7] isEvenNumberAsync', () => { - // test('[19] resolves true if passed an even number', () => {}) - // test('[20] resolves false if passed an odd number', () => {}) + test('[19] resolves true if passed an even number', () => {}) + test('[20] resolves false if passed an odd number', () => {}) }) From 75e6b6a749b628bce0b3bbf9c616a27e3c82a43c Mon Sep 17 00:00:00 2001 From: joey-bertschler Date: Wed, 19 Jan 2022 10:39:21 +0400 Subject: [PATCH 4/7] seasons --- index.test.js | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/index.test.js b/index.test.js index bcf5f1ba..cbba090a 100644 --- a/index.test.js +++ b/index.test.js @@ -67,12 +67,37 @@ describe('[Exercise 5] Seasons', () => { beforeEach(() => { seasons = new utils.Seasons() // each test must start with fresh seasons }) - test('[9] the FIRST call of seasons.next returns "summer"', () => {}) - test('[10] the SECOND call of seasons.next returns "fall"', () => {}) - test('[11] the THIRD call of seasons.next returns "winter"', () => {}) - test('[12] the FOURTH call of seasons.next returns "spring"', () => {}) - test('[13] the FIFTH call of seasons.next returns again "summer"', () => {}) - test('[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 < 40; i++) { + seasons.next() + } + expect(seasons.next()).toBe('spring') + }) }) describe('[Exercise 6] Car', () => { From a68b2ef3d2fbcfcf9d93ff7c9031a48a1c8792da Mon Sep 17 00:00:00 2001 From: joey-bertschler Date: Wed, 19 Jan 2022 10:39:25 +0400 Subject: [PATCH 5/7] seasons --- index.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/index.js b/index.js index d3854dee..74732962 100644 --- a/index.js +++ b/index.js @@ -89,6 +89,8 @@ class Seasons { */ constructor() { // ✨ initialize whatever properties are needed + this.seasons = ["summer", "fall", "winter", "spring"]; + this.currentSeason = 0 } /** @@ -105,6 +107,13 @@ class Seasons { */ next() { // ✨ implement + const result = this.seasons[this.currentSeason]; + if (this.currentSeason === 3) { + this.currentSeason = 0 + } else { + this.currentSeason++; + } + return result; } } From 1092f42f472dce162910741ce9055d136dc4bc0e Mon Sep 17 00:00:00 2001 From: joey-bertschler Date: Wed, 19 Jan 2022 11:38:39 +0400 Subject: [PATCH 6/7] tank --- index.js | 24 ++++++++++++++++++++++-- index.test.js | 34 +++++++++++++++++++++++++++++----- 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 74732962..1a968c16 100644 --- a/index.js +++ b/index.js @@ -127,6 +127,9 @@ class Car { constructor(name, tankSize, mpg) { this.odometer = 0 // car initilizes with zero miles this.tank = tankSize // car initiazes full of gas + this.mpg = mpg // car initiazes with mpg + this.name = name // car initiazes with name + this.tankSize = tankSize // ✨ initialize whatever other properties are needed } @@ -145,7 +148,16 @@ class Car { */ drive(distance) { // ✨ implement - } + const milesCanDrive = this.tank * this.mpg; + if (milesCanDrive >= distance) { + this.odometer += distance; + this.tank -= (distance / this.mpg); + } else { + this.odometer += milesCanDrive; + this.tank = 0; + } + return this.odometer; + } /** * [Exercise 6C] Adds gallons to the tank @@ -160,7 +172,15 @@ class Car { */ refuel(gallons) { // ✨ implement + const gallonsCanFit = this.tankSize - this.tank; + if (gallonsCanFit >= gallons) { + this.tank += gallons; + } else { + this.tank = this.tankSize; + } + return this.drive(this.tank * this.mpg); } + } /** @@ -188,4 +208,4 @@ module.exports = { Counter, Seasons, Car, -} +}; diff --git a/index.test.js b/index.test.js index cbba090a..d44deaee 100644 --- a/index.test.js +++ b/index.test.js @@ -93,7 +93,7 @@ describe('[Exercise 5] Seasons', () => { expect(seasons.next()).toBe('summer') }) test('[14] the 40th call of seasons.next returns "spring"', () => { - for (let i = 0; i < 40; i++) { + for (let i = 0; i < 39; i++) { seasons.next() } expect(seasons.next()).toBe('spring') @@ -105,10 +105,34 @@ describe('[Exercise 6] Car', () => { beforeEach(() => { focus = new utils.Car('focus', 20, 30) // each test must start with a fresh car }) - test('[15] driving the car returns the updated odometer', () => {}) - test('[16] driving the car uses gas', () => {}) - test('[17] refueling allows to keep driving', () => {}) - test('[18] adding fuel to a full tank has no effect', () => {}) + test('[15] driving the car returns the updated odometer', () => { + expect(focus.drive(100)).toBe(100) + expect(focus.drive(100)).toBe(200) + expect(focus.drive(100)).toBe(300) + expect(focus.drive(100)).toBe(400) + expect(focus.drive(100)).toBe(500) + expect(focus.drive(100)).toBe(600) + expect(focus.drive(100)).toBe(600) //empty tank + }) + test('[16] driving the car uses gas', () => { + focus.drive(600) + expect(focus.drive(100)).toBe(600) + expect(focus.drive(100)).toBe(600) + expect(focus.tank).toBe(0) + + }) + test('[17] refueling allows to keep driving', () => { + focus.drive(600) + focus.refuel(10) + focus.drive(600) + expect(focus.odometer).toBe(900) + focus.refuel(20) + focus.drive(600) + expect(focus.odometer).toBe(1500) + }) + test('[18] adding fuel to a full tank has no effect', () => { + + }) }) describe('[Exercise 7] isEvenNumberAsync', () => { From 76e9d33e6e3ed2062c4312544706670936294a40 Mon Sep 17 00:00:00 2001 From: joey-bertschler Date: Wed, 19 Jan 2022 11:43:56 +0400 Subject: [PATCH 7/7] is even --- index.js | 6 +++++- index.test.js | 14 +++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 1a968c16..8c94454d 100644 --- a/index.js +++ b/index.js @@ -180,7 +180,6 @@ class Car { } return this.drive(this.tank * this.mpg); } - } /** @@ -198,6 +197,11 @@ class Car { */ function isEvenNumberAsync(number) { // ✨ implement + if (number % 2 === 0) { + return Promise.resolve(true); + } else { + return Promise.resolve(false); + } } module.exports = { diff --git a/index.test.js b/index.test.js index d44deaee..bc75fbf7 100644 --- a/index.test.js +++ b/index.test.js @@ -131,11 +131,19 @@ describe('[Exercise 6] Car', () => { expect(focus.odometer).toBe(1500) }) test('[18] adding fuel to a full tank has no effect', () => { - + focus.refuel(200000) + focus.drive(10000) + expect(focus.odometer).toBe(600) }) }) describe('[Exercise 7] isEvenNumberAsync', () => { - test('[19] resolves true if passed an even number', () => {}) - test('[20] resolves false if passed an odd number', () => {}) + test('[19] resolves true if passed an even number', async () => { + const result = await utils.isEvenNumberAsync(2) + expect(result).toBe(true) + }) + test('[20] resolves false if passed an odd number', async () => { + const result = await utils.isEvenNumberAsync(3) + expect(result).toBe(false) + }) })