diff --git a/index.js b/index.js index 3df06ec1..0fc880ef 100644 --- a/index.js +++ b/index.js @@ -8,6 +8,11 @@ */ function trimProperties(obj) { // ✨ implement + const result = {} + for (let prop in obj) { + result[prop] = obj[prop].trim() + } + return result } /** @@ -20,6 +25,10 @@ function trimProperties(obj) { */ function trimPropertiesMutation(obj) { // ✨ implement + for(let prop in obj){ + obj[prop] = obj[prop].trim() + } + return obj } /** @@ -32,6 +41,13 @@ function trimPropertiesMutation(obj) { */ function findLargestInteger(integers) { // ✨ implement + let result = integers[0].integer + for(let i = 0; i < integers.length; i++){ + if(integers[i].integer > result){ + result = integers[i].integer + } + } + return result } class Counter { @@ -41,6 +57,7 @@ class Counter { */ constructor(initialNumber) { // ✨ initialize whatever properties are needed + this.count = initialNumber } /** @@ -57,6 +74,7 @@ class Counter { */ countDown() { // ✨ implement + return this.count > 0 ? this.count -- : 0 } } @@ -66,6 +84,8 @@ class Seasons { */ constructor() { // ✨ initialize whatever properties are needed + this.seasons = ['summer', 'fall', 'winter', 'spring'] + this.currentSeason = 0 } /** @@ -82,6 +102,13 @@ class Seasons { */ next() { // ✨ implement + const result = this.seasons[this.currentSeason] + if(this.currentSeason === 3){ + this.currentSeason = 0 + }else{ + this.currentSeason++ + } + return result } } @@ -96,6 +123,8 @@ class Car { this.odometer = 0 // car initilizes with zero miles this.tank = tankSize // car initiazes full of gas // ✨ initialize whatever other properties are needed + this.tankSize = tankSize + this.mpg = mpg } /** @@ -113,6 +142,15 @@ class Car { */ drive(distance) { // ✨ implement + const milesDriveable = this.tank * this.mpg + if(distance <= milesDriveable){ + this.odometer = this.odometer + distance + this.tank = this.tank - (distance / this.mpg) + }else{ + this.tank = 0 + this.odometer = this.odometer + milesDriveable + } + return this.odometer } /** @@ -128,6 +166,13 @@ class Car { */ refuel(gallons) { // ✨ implement + const tankGallons = this.tankSize - this.tank + if(gallons <= tankGallons){ + this.tank = this.tank + gallons + }else{ + this.tank = this.tankSize + } + return this.tank * this.mpg } } @@ -144,9 +189,13 @@ class Car { * // result is false * }) */ -function isEvenNumberAsync(number) { - // ✨ implement -} +async function isEvenNumberAsync(number) { + if(!number || typeof number !== "number"){ + return false + } + if(number % 2 === 0) {return true } + else{return false} + } module.exports = { trimProperties, diff --git a/index.test.js b/index.test.js index cfb0a0b3..6e417696 100644 --- a/index.test.js +++ b/index.test.js @@ -8,16 +8,35 @@ 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 = { name: ' cynthia ' } + const expected = { name: 'cynthia' } + expect(utils.trimProperties(input)).toEqual(expected) + expect(utils.trimProperties(input)).not.toBe(input) + }) }) 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 ' } + const actual = utils.trimPropertiesMutation(input) + const expected = { foo: 'foo' } + expect(expected).toEqual(actual) + }) + 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).toEqual(expected) + }) }) 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: 2 }, { integer: 3 }] + const invoked = utils.findLargestInteger(input) + expect(invoked).toBe(3) + }) }) describe('[Exercise 4] Counter', () => { @@ -25,9 +44,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 +65,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 +103,41 @@ 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(200)).toBe(500) + }) + test('[16] driving the car uses gas', () => { + focus.drive(600) + expect(focus.drive(1)).toBe(600) + expect(focus.drive(1)).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', () => { + focus.refuel(2000000) + 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 () => { + let result = await utils.isEvenNumberAsync(4) + expect(result).toBe(true) + }) + test('[20] resolves false if passed an odd number', async () => { + let number = await utils.isEvenNumberAsync(5) + expect(number).toBe(false) + }) }) diff --git a/package-lock.json b/package-lock.json index 880aab90..db97e7ae 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5,7 +5,6 @@ "requires": true, "packages": { "": { - "name": "node-testing1-project", "version": "1.0.0", "devDependencies": { "@types/jest": "^27.0.3",