diff --git a/index.js b/index.js index 3df06ec1..07ef4662 100644 --- a/index.js +++ b/index.js @@ -7,8 +7,16 @@ * trimProperties({ name: ' jane ' }) // returns a new object { name: 'jane' } */ function trimProperties(obj) { - // ✨ implement -} + const trimmedObj = {}; + for (let prop in obj) { + if (typeof obj[prop] === "string") { + trimmedObj[prop] = obj[prop].trim(); +} else { trimmedObj[prop] = obj[prop]; +}} +return trimmedObj; + } + + /** * [Exercise 2] trimPropertiesMutation trims in place the properties of an object @@ -19,7 +27,9 @@ 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 +41,10 @@ function trimPropertiesMutation(obj) { * findLargestInteger([{ integer: 1 }, { integer: 3 }, { integer: 2 }]) // returns 3 */ 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 { @@ -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 } /** @@ -55,8 +68,7 @@ class Counter { * counter.countDown() // returns 0 * counter.countDown() // returns 0 */ - countDown() { - // ✨ implement + countDown() { return this.count > 0 ? this.count-- : 0; } } @@ -65,7 +77,8 @@ class Seasons { * [Exercise 5A] Seasons creates a seasons object */ constructor() { - // ✨ initialize whatever properties are needed + this.seasons = ['winter','notspring','summer','fall'] + this.currentSeason = 0 } /** @@ -81,8 +94,14 @@ class Seasons { * seasons.next() // returns "summer" */ next() { - // ✨ implement + let thecurrentSeason = this.seasons[this.currentSeason] + if(this.currentSeason === 3 ){ + this.currentSeason = 0 } + else{ ++this.currentSeason} + return thecurrentSeason + } + } class Car { @@ -95,7 +114,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.tankMax = tankSize; } /** @@ -112,7 +133,13 @@ class Car { * focus.drive(200) // returns 600 (ran out of gas after 100 miles) */ drive(distance) { - // ✨ implement + let maxRange = this.tank * this.mpg + if(distance <= maxRange){ this.odometer = this.odometer + distance; + this.tank = this.tank - distance / this.mpg + } + else{this.tank = 0 + this.odometer = this.odometer + maxRange} + return this.odometer } /** @@ -127,7 +154,10 @@ class Car { * focus.refuel(99) // returns 600 (tank only holds 20) */ refuel(gallons) { - // ✨ implement + if (this.tank + gallons > this.tankMax) {this.tank = this.tankMax + return Car} + else{this.tank += gallons} + return Car } } @@ -144,9 +174,14 @@ 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..d1604ced 100644 --- a/index.test.js +++ b/index.test.js @@ -8,26 +8,55 @@ 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: 'jane'} + const expected = {name: 'jane'} + 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', () => {}) +describe('[Exercise 2] trimPropertiesMutation', () => { const input = {name:'james'} + const expected = {name:'james'} + test('[3] returns an object with the properties trimmed', () => { + + expect(utils.trimPropertiesMutation(input)).toEqual(expected) + }) + test('[4] the object returned is the exact same one we passed in', () => { + expect(utils.trimPropertiesMutation(input)).toEqual(expected) + }) }) -describe('[Exercise 3] findLargestInteger', () => { - // test('[5] returns the largest number in an array of objects { integer: 2 }', () => {}) +describe('[Exercise 3] findLargestInteger', () => {const integers = [{integer:1, integer:2, integer:3}] + test('[5] returns the largest number in an array of objects { integer: 3 }', () => { + + expect(utils.findLargestInteger(integers)).toBe(3) + }) }) describe('[Exercise 4] Counter', () => { let counter beforeEach(() => { - counter = new utils.Counter(3) // each test must start with a fresh couter + counter = new utils.Counter(4) // each test must start with a fresh couter + }) + test('[6] the FIRST CALL of counter.countDown returns the initial count', () => { + expect(counter.countDown()).toBe(4) }) - // 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('[7] the SECOND CALL of counter.countDown returns the initial count minus one', () => { counter.countDown() + expect(counter.countDown()).toEqual(3) + }) + + +test('[8] the count eventually reaches zero but does not go below zero', () => { + /*4*/counter.countDown(); + /*3*/counter.countDown(); + /*2*/counter.countDown(); + /*1*/counter.countDown(); + expect(counter.countDown()).toBe(0) +}) + }) describe('[Exercise 5] Seasons', () => { @@ -35,12 +64,35 @@ 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 ""', () => { + expect(seasons.next()).toBe('winter') + }) + test('[10] the SECOND call of seasons.next returns ""', () => { + seasons.next() + expect(seasons.next()).toBe('notspring') + }) + test('[11] the THIRD call of seasons.next returns "summer"', () => { + seasons.next() + seasons.next() + expect(seasons.next()).toBe('summer') + }) + test('[12] the FOURTH call of seasons.next returns "fall"', () => { + seasons.next() + seasons.next() + seasons.next() + expect(seasons.next()).toBe('fall') + }) + test('[13] the FIFTH call of seasons.next returns again "winter"', () => { + seasons.next() + seasons.next() + seasons.next() + seasons.next() + expect(seasons.next()).toBe('winter') + }) + test('[14] the 40th call of seasons.next returns "fall"', () => { + for(let i = 0; i < 39; i++){seasons.next()} + expect(seasons.next()).toBe('fall') + }) }) describe('[Exercise 6] Car', () => { @@ -48,13 +100,38 @@ 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', () => { focus.drive(150) + expect(focus.drive(100)).toBe(250) + + }) + test('[16] driving the car uses gas', () => { + focus.drive(600) + expect(focus.tank).toBe(0) + }) + test('[17] refueling allows to keep driving', () => { + focus.drive(300) + expect(focus.tank).toBe(10) + focus.refuel(5) + expect(focus.tank).toBe(15) + focus.drive(300) + expect(focus.tank).toBe(5) + }) + test('[18] adding fuel to a full tank has no effect', () => { + focus.drive(300) + expect(focus.tank).toBe(10) + focus.refuel(150) + expect(focus.tank).toBe(20) + }) }) 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) + }) })