diff --git a/index.js b/index.js index 3df06ec1..51d8d9b1 100644 --- a/index.js +++ b/index.js @@ -7,7 +7,11 @@ * trimProperties({ name: ' jane ' }) // returns a new object { name: 'jane' } */ function trimProperties(obj) { - // ✨ implement + const result = {} + for (let prop in obj) { + result[prop] = obj[prop].trim() + } + return result } /** @@ -19,7 +23,10 @@ function trimProperties(obj) { * trimPropertiesMutation({ name: ' jane ' }) // returns the object mutated in place { name: 'jane' } */ function trimPropertiesMutation(obj) { - // ✨ implement + for (let prop in obj) { + obj[prop] = obj[prop].trim() + } + return obj } /** @@ -31,7 +38,13 @@ function trimPropertiesMutation(obj) { * findLargestInteger([{ integer: 1 }, { integer: 3 }, { integer: 2 }]) // returns 3 */ function findLargestInteger(integers) { - // ✨ implement + let result = integers[0].integer + for (let idx = 1; idx < integers.length; idx++) { + if (integers[idx].integer > result) { + result = integers[idx].integer + } + } + return result } class Counter { @@ -40,7 +53,7 @@ class Counter { * @param {number} initialNumber - the initial state of the count */ constructor(initialNumber) { - // ✨ initialize whatever properties are needed + this.count = initialNumber } /** @@ -56,7 +69,10 @@ class Counter { * counter.countDown() // returns 0 */ countDown() { - // ✨ implement + if (this.count > 0) { + return this.count-- + } + return this.count } } @@ -65,7 +81,8 @@ class Seasons { * [Exercise 5A] Seasons creates a seasons object */ constructor() { - // ✨ initialize whatever properties are needed + this.seasons = ['summer', 'fall', 'winter', 'spring'] + this.currentSeason = 0 } /** @@ -81,7 +98,13 @@ class Seasons { * seasons.next() // returns "summer" */ next() { - // ✨ implement + const result = this.seasons[this.currentSeason] + if (this.currentSeason === 3) { + this.currentSeason = 0 + } else { + ++this.currentSeason + } + return result } } diff --git a/index.test.js b/index.test.js index cfb0a0b3..93322d38 100644 --- a/index.test.js +++ b/index.test.js @@ -8,16 +8,33 @@ 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', () => { - // 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', () => { + 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', () => { - // 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 }', () => { + const input = [{ integer: 1 }, { integer: 3 }, { integer: 2 }] + const actual = utils.findLargestInteger(input) + expect(actual).toBe(3) + }) }) describe('[Exercise 4] Counter', () => { @@ -25,9 +42,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()).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 +63,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 < 39; i++) { + seasons.next() + } + expect(seasons.next()).toBe('spring') + }) }) describe('[Exercise 6] Car', () => { @@ -48,13 +101,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', () => {}) })