From 081030e8ec2abc20fea457ab215e3545708df2e9 Mon Sep 17 00:00:00 2001 From: Stan Date: Tue, 10 Aug 2021 18:16:32 -0700 Subject: [PATCH 1/9] project setup --- index.js | 8 +++--- index.test.js | 73 ++++++++++++++++++++++++++++---------------------- jest.config.js | 2 +- 3 files changed, 47 insertions(+), 36 deletions(-) diff --git a/index.js b/index.js index 3df06ec1..6c51fd31 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,4 @@ +/* eslint-disable */ /** * [Exercise 1] trimProperties copies an object trimming its properties * @param {object} obj - an object with properties that are strings @@ -8,6 +9,7 @@ */ function trimProperties(obj) { // ✨ implement + return { obj }; } /** @@ -93,8 +95,8 @@ class Car { * @param {number} mpg - miles the car can drive per gallon of gas */ constructor(name, tankSize, mpg) { - this.odometer = 0 // car initilizes with zero miles - this.tank = tankSize // car initiazes full of gas + this.odometer = 0; // car initilizes with zero miles + this.tank = tankSize; // car initiazes full of gas // ✨ initialize whatever other properties are needed } @@ -156,4 +158,4 @@ module.exports = { Counter, Seasons, Car, -} +}; diff --git a/index.test.js b/index.test.js index cfb0a0b3..a6055a7a 100644 --- a/index.test.js +++ b/index.test.js @@ -1,60 +1,69 @@ -const utils = require('./index') +/* eslint-disable */ +const utils = require("./index"); -describe('[Exercise 1] trimProperties', () => { - test('[1] returns an object with the properties trimmed', () => { +// test("works great", () => { +// //empty test +// }); + +describe("[Exercise 1] trimProperties", () => { + test("[1] 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) - }) + 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('[2] returns a copy, leaving the original object intact', () => {}) -}) +}); -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", () => { + test("[3] returns an object with the properties trimmed", () => { + expect(); + }); + test("[4] the object returned is the exact same one we passed in", () => { + expect(); + }); +}); -describe('[Exercise 3] findLargestInteger', () => { +describe("[Exercise 3] findLargestInteger", () => { // test('[5] returns the largest number in an array of objects { integer: 2 }', () => {}) -}) +}); -describe('[Exercise 4] Counter', () => { - let counter +describe("[Exercise 4] Counter", () => { + let counter; beforeEach(() => { - counter = new utils.Counter(3) // each test must start with a fresh couter - }) + 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', () => {}) -}) +}); -describe('[Exercise 5] Seasons', () => { - let seasons +describe("[Exercise 5] Seasons", () => { + let seasons; beforeEach(() => { - seasons = new utils.Seasons() // each test must start with fresh seasons - }) + 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"', () => {}) -}) +}); -describe('[Exercise 6] Car', () => { - let focus +describe("[Exercise 6] Car", () => { + let focus; beforeEach(() => { - focus = new utils.Car('focus', 20, 30) // each test must start with a fresh car - }) + 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', () => {}) -}) +}); -describe('[Exercise 7] isEvenNumberAsync', () => { +describe("[Exercise 7] isEvenNumberAsync", () => { // test('[19] resolves true if passed an even number', () => {}) // test('[20] resolves false if passed an odd number', () => {}) -}) +}); diff --git a/jest.config.js b/jest.config.js index b150ac59..dab50f78 100644 --- a/jest.config.js +++ b/jest.config.js @@ -11,7 +11,7 @@ module.exports = { // bail: 0, // The directory where Jest should store its cached dependency information - // cacheDirectory: "/private/var/folders/78/wsw4bc497l1b0dlb3f77xh380000gn/T/jest_dx", + // cacheDirectory: "/private/var/folders/4q/91xyqb651fz9_4dwn3l5flyr0000gn/T/jest_dx", // Automatically clear mock calls and instances between every test // clearMocks: false, From 771bb160a6e06a9e9e14b72a79565194997a92ba Mon Sep 17 00:00:00 2001 From: Stan Date: Tue, 10 Aug 2021 19:42:55 -0700 Subject: [PATCH 2/9] testing working-15 --- index.js | 12 +++++++++++- index.test.js | 46 +++++++++++++++++++++++++++++++--------------- 2 files changed, 42 insertions(+), 16 deletions(-) diff --git a/index.js b/index.js index 6c51fd31..cd94ad6b 100644 --- a/index.js +++ b/index.js @@ -9,7 +9,11 @@ */ function trimProperties(obj) { // ✨ implement - return { obj }; + return Object.keys(obj).reduce((acc, curr) => { + acc[curr] = obj[curr].trim(); + // console.log(acc); + return acc; + }, {}); } /** @@ -22,6 +26,11 @@ function trimProperties(obj) { */ function trimPropertiesMutation(obj) { // ✨ implement + // return Object.keys(obj).reduce((acc, curr) => { + // acc[curr] = obj[curr].trim(); + // console.log(acc); + // return acc; + // }, {}); } /** @@ -115,6 +124,7 @@ class Car { */ drive(distance) { // ✨ implement + this.odometer += distance; } /** diff --git a/index.test.js b/index.test.js index a6055a7a..e081aeb0 100644 --- a/index.test.js +++ b/index.test.js @@ -13,30 +13,40 @@ 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", () => { + // // expect(); + // }); }); -describe("[Exercise 2] trimPropertiesMutation", () => { - test("[3] returns an object with the properties trimmed", () => { - expect(); - }); - test("[4] the object returned is the exact same one we passed in", () => { - expect(); - }); -}); +// describe("[Exercise 2] trimPropertiesMutation", () => { +// test("[3] returns an object with the properties trimmed", () => { +// // expect(); +// }); +// test("[4] the object returned is the exact same one we passed in", () => { +// // expect(); +// }); +// }); 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 }", () => { + // expect(); + // }); }); describe("[Exercise 4] Counter", () => { let counter; beforeEach(() => { - counter = new utils.Counter(3); // each test must start with a fresh couter + counter = new utils.Counter(3); // each test must start with a fresh counter }); - // 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(); + // }); + // test("[7] the SECOND CALL of counter.countDown returns the initial count minus one", () => { + // expect(); + // }); + // test("[8] the count eventually reaches zero but does not go below zero", () => { + // expect(); + // }); }); describe("[Exercise 5] Seasons", () => { @@ -57,7 +67,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("[15] driving the car returns the updated odometer", () => { + focus.drive(100); + focus.drive(200); + focus.drive(250); + + expect(focus.odometer).toBe(550); + }); // 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', () => {}) From 99405592c18b31673bfad2310eb56570c4ca066c Mon Sep 17 00:00:00 2001 From: Stan Date: Tue, 10 Aug 2021 20:53:08 -0700 Subject: [PATCH 3/9] test-7 --- index.js | 4 ++-- index.test.js | 32 +++++++++++++++++++++++++------- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index cd94ad6b..231a2a38 100644 --- a/index.js +++ b/index.js @@ -140,6 +140,7 @@ class Car { */ refuel(gallons) { // ✨ implement + this.gallons += gallons; } } @@ -149,8 +150,6 @@ class Car { * @returns {promise} - resolves true if number even, false otherwise * * EXAMPLE - * isEvenNumberAsync(2).then(result => { - * // result is true * }) * isEvenNumberAsync(3).then(result => { * // result is false @@ -158,6 +157,7 @@ class Car { */ function isEvenNumberAsync(number) { // ✨ implement + number = 2; } module.exports = { diff --git a/index.test.js b/index.test.js index e081aeb0..4ef89e18 100644 --- a/index.test.js +++ b/index.test.js @@ -67,19 +67,37 @@ describe("[Exercise 6] Car", () => { beforeEach(() => { focus = new utils.Car("focus", 20, 30); // each test must start with a fresh car }); + test("[B] has an odometer initialized at zero", () => { + expect(focus.odometer).toBe(0); + }); test("[15] driving the car returns the updated odometer", () => { focus.drive(100); focus.drive(200); - focus.drive(250); + focus.drive(300); - expect(focus.odometer).toBe(550); + expect(focus.odometer).toBe(600); + }); + test("[16] driving the car uses gas", () => { + const actualGallons = focus.drive(600); + // const expectedGallons = 10; + expect(actualGallons).toBe(10); + }); + test("[17] refueling allows to keep driving", () => { + expect(); + }); + test("[18] adding fuel to a full tank has no effect", () => { + expect(); }); - // 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", async () => { + const evenNr = await Promise.resolve(true); + expect(evenNr).toBe(true); + }); + + test("[20] resolves false if passed an odd number", async () => { + const oddNr = await Promise.resolve(false); + expect(oddNr).toBe(false); + }); }); From 5c9ccad9bd4c732b2966339267714c6db4cfa5ae Mon Sep 17 00:00:00 2001 From: Stan Date: Tue, 10 Aug 2021 21:16:20 -0700 Subject: [PATCH 4/9] test-1 trimProp --- index.js | 12 ++++++------ index.test.js | 9 ++++++--- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index 231a2a38..55e4fe7c 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,3 @@ -/* eslint-disable */ /** * [Exercise 1] trimProperties copies an object trimming its properties * @param {object} obj - an object with properties that are strings @@ -9,11 +8,12 @@ */ function trimProperties(obj) { // ✨ implement - return Object.keys(obj).reduce((acc, curr) => { - acc[curr] = obj[curr].trim(); - // console.log(acc); - return acc; - }, {}); + const result = {}; + for (let prop in obj) { + result[prop] = obj[prop].trim(); + } + // console.log(acc); + return result; } /** diff --git a/index.test.js b/index.test.js index 4ef89e18..d48268c7 100644 --- a/index.test.js +++ b/index.test.js @@ -13,9 +13,12 @@ describe("[Exercise 1] trimProperties", () => { const actual = utils.trimProperties(input); expect(actual).toEqual(expected); }); - // test("[2] returns a copy, leaving the original object intact", () => { - // // expect(); - // }); + + 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 836c1ba91086c91c27565a866683acd1b68c134b Mon Sep 17 00:00:00 2001 From: Stan Date: Tue, 10 Aug 2021 21:55:52 -0700 Subject: [PATCH 5/9] test 3, 4 --- index.js | 13 ++++++------- index.test.js | 43 ++++++++++++++++++++++++------------------- 2 files changed, 30 insertions(+), 26 deletions(-) diff --git a/index.js b/index.js index 55e4fe7c..acf4031b 100644 --- a/index.js +++ b/index.js @@ -12,7 +12,7 @@ function trimProperties(obj) { for (let prop in obj) { result[prop] = obj[prop].trim(); } - // console.log(acc); + // console.log(result); return result; } @@ -26,11 +26,10 @@ function trimProperties(obj) { */ function trimPropertiesMutation(obj) { // ✨ implement - // return Object.keys(obj).reduce((acc, curr) => { - // acc[curr] = obj[curr].trim(); - // console.log(acc); - // return acc; - // }, {}); + for (let prop in obj) { + obj[prop] = obj[prop].trim(); + } + return obj; } /** @@ -140,7 +139,7 @@ class Car { */ refuel(gallons) { // ✨ implement - this.gallons += gallons; + // this.gallons += gallons; } } diff --git a/index.test.js b/index.test.js index d48268c7..3665cab6 100644 --- a/index.test.js +++ b/index.test.js @@ -21,14 +21,19 @@ describe("[Exercise 1] trimProperties", () => { }); }); -// describe("[Exercise 2] trimPropertiesMutation", () => { -// test("[3] returns an object with the properties trimmed", () => { -// // expect(); -// }); -// test("[4] the object returned is the exact same one we passed in", () => { -// // expect(); -// }); -// }); +describe("[Exercise 2] trimPropertiesMutation", () => { + 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 }", () => { @@ -80,17 +85,17 @@ describe("[Exercise 6] Car", () => { expect(focus.odometer).toBe(600); }); - test("[16] driving the car uses gas", () => { - const actualGallons = focus.drive(600); - // const expectedGallons = 10; - expect(actualGallons).toBe(10); - }); - test("[17] refueling allows to keep driving", () => { - expect(); - }); - test("[18] adding fuel to a full tank has no effect", () => { - expect(); - }); + // test("[16] driving the car uses gas", () => { + // const actualGallons = focus.drive(600); + // // const expectedGallons = 10; + // expect(actualGallons).toBe(10); + // }); + // test("[17] refueling allows to keep driving", () => { + // expect(); + // }); + // test("[18] adding fuel to a full tank has no effect", () => { + // expect(); + // }); }); describe("[Exercise 7] isEvenNumberAsync", () => { From a6fcc960bceb7f16a00ea77037045d9755782a88 Mon Sep 17 00:00:00 2001 From: Stan Date: Tue, 10 Aug 2021 22:19:58 -0700 Subject: [PATCH 6/9] exercise:4 --- index.js | 16 ++++++++++++++-- index.test.js | 38 +++++++++++++++++++++++++------------- 2 files changed, 39 insertions(+), 15 deletions(-) diff --git a/index.js b/index.js index acf4031b..f85d8bd0 100644 --- a/index.js +++ b/index.js @@ -41,7 +41,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 = 0; idx < integers.length; idx++) { + if (integers[idx].integer > result) { + result = integers[idx].integer; + } + } + return result; } class Counter { @@ -51,6 +57,7 @@ class Counter { */ constructor(initialNumber) { // ✨ initialize whatever properties are needed + this.count = initialNumber; } /** @@ -66,7 +73,12 @@ class Counter { * counter.countDown() // returns 0 */ countDown() { - // ✨ implement + return this.count > 0 ? this.count-- : 0; + // or: + // if (this.count > 0) { + // return this.count--; + // } + // return this.count; } } diff --git a/index.test.js b/index.test.js index 3665cab6..9f96138b 100644 --- a/index.test.js +++ b/index.test.js @@ -1,4 +1,3 @@ -/* eslint-disable */ const utils = require("./index"); // test("works great", () => { @@ -36,9 +35,17 @@ describe("[Exercise 2] trimPropertiesMutation", () => { }); describe("[Exercise 3] findLargestInteger", () => { - // test("[5] returns the largest number in an array of objects { integer: 2 }", () => { - // expect(); - // }); + test("[5] returns the largest number in an array of objects { integer: 2 }", () => { + const input = [{ integer: 1 }, { integer: 3 }, { integer: 2 }]; + const input2 = [{ integer: 4 }, { integer: 3 }, { integer: 2 }]; + const input3 = [{ integer: 1 }, { integer: 3 }, { integer: 4 }]; + const actual = utils.findLargestInteger(input); + const actual2 = utils.findLargestInteger(input2); + const actual3 = utils.findLargestInteger(input3); + expect(actual).toBe(3); + expect(actual2).toBe(4); + expect(actual3).toBe(4); + }); }); describe("[Exercise 4] Counter", () => { @@ -46,15 +53,20 @@ describe("[Exercise 4] Counter", () => { beforeEach(() => { counter = new utils.Counter(3); // each test must start with a fresh counter }); - // test("[6] the FIRST CALL of counter.countDown returns the initial count", () => { - // expect(); - // }); - // test("[7] the SECOND CALL of counter.countDown returns the initial count minus one", () => { - // expect(); - // }); - // test("[8] the count eventually reaches zero but does not go below zero", () => { - // expect(); - // }); + 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", () => { From 1e023ae80c84f094131d0e1480ce2d1d47a7fb0a Mon Sep 17 00:00:00 2001 From: Stan Date: Tue, 10 Aug 2021 22:33:28 -0700 Subject: [PATCH 7/9] Seasons test --- index.js | 9 +++++++++ index.test.js | 37 +++++++++++++++++++++++++++++++------ 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index f85d8bd0..8649bc83 100644 --- a/index.js +++ b/index.js @@ -88,6 +88,8 @@ class Seasons { */ constructor() { // ✨ initialize whatever properties are needed + this.seasons = ["summer", "fall", "winter", "spring"]; + this.currentSeason = 0; } /** @@ -104,6 +106,13 @@ class Seasons { */ 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 9f96138b..49436768 100644 --- a/index.test.js +++ b/index.test.js @@ -74,12 +74,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", () => { From edd756e6dcce5e8d2217d4bff45760beec0448c2 Mon Sep 17 00:00:00 2001 From: Stan Date: Wed, 11 Aug 2021 13:16:10 -0700 Subject: [PATCH 8/9] car-test --- index.js | 19 +++++++++++++++++-- index.test.js | 46 +++++++++++++++++++++++++++++----------------- 2 files changed, 46 insertions(+), 19 deletions(-) diff --git a/index.js b/index.js index 8649bc83..4b7f2eda 100644 --- a/index.js +++ b/index.js @@ -127,6 +127,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; } /** @@ -144,7 +146,15 @@ class Car { */ drive(distance) { // ✨ implement - this.odometer += distance; + const milesCanDrive = this.tank * this.mpg; + if (distance <= milesCanDrive) { + this.odometer = this.odometer + distance; + this.tank = this.tank - distance / this.mpg; + } else { + this.tank = 0; + this.odometer = this.odometer + milesCanDrive; + } + return this.odometer; } /** @@ -160,7 +170,12 @@ class Car { */ refuel(gallons) { // ✨ implement - // this.gallons += gallons; + if (gallons <= this.tankSize - this.tank) { + this.tank = this.tank + gallons; + } else { + this.tank = this.tankSize; + } + return this.tank * this.mpg; } } diff --git a/index.test.js b/index.test.js index 49436768..41f0fa1e 100644 --- a/index.test.js +++ b/index.test.js @@ -1,8 +1,8 @@ const utils = require("./index"); -// test("works great", () => { -// //empty test -// }); +test("this test works great", () => { + //empty test +}); describe("[Exercise 1] trimProperties", () => { test("[1] returns an object with the properties trimmed", () => { @@ -116,23 +116,35 @@ describe("[Exercise 6] Car", () => { expect(focus.odometer).toBe(0); }); test("[15] driving the car returns the updated odometer", () => { - focus.drive(100); - focus.drive(200); - focus.drive(300); + expect(focus.drive(100)).toBe(100); + expect(focus.drive(100)).toBe(200); + expect(focus.drive(100)).toBe(300); + expect(focus.drive(200)).toBe(500); + // expect(focus.drive(200)).toBe(600); + }); + + test("[16] driving the car uses gas", () => { + focus.drive(600); + expect(focus.drive(1)).toBe(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(20000000); + focus.drive(100000); expect(focus.odometer).toBe(600); }); - // test("[16] driving the car uses gas", () => { - // const actualGallons = focus.drive(600); - // // const expectedGallons = 10; - // expect(actualGallons).toBe(10); - // }); - // test("[17] refueling allows to keep driving", () => { - // expect(); - // }); - // test("[18] adding fuel to a full tank has no effect", () => { - // expect(); - // }); }); describe("[Exercise 7] isEvenNumberAsync", () => { From 1eeebe7353d267ba0d6d34ee691a49d39ab8ece6 Mon Sep 17 00:00:00 2001 From: Stan Date: Wed, 11 Aug 2021 13:42:25 -0700 Subject: [PATCH 9/9] Test Pass --- index.js | 14 +++++++++++--- index.test.js | 16 ++++++++++++---- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 4b7f2eda..6b14a783 100644 --- a/index.js +++ b/index.js @@ -190,9 +190,17 @@ class Car { * // result is false * }) */ -function isEvenNumberAsync(number) { - // ✨ implement - number = 2; +async function isEvenNumberAsync(number) { + if (typeof number !== "number" || isNaN(number)) { + throw new Error("error"); + } + return number % 2 === 0 || false; + // ✨ or: + // if (number % 2 === 0) { + // return true; + // } else { + // return false; + // } } module.exports = { diff --git a/index.test.js b/index.test.js index 41f0fa1e..cc44e724 100644 --- a/index.test.js +++ b/index.test.js @@ -149,12 +149,20 @@ describe("[Exercise 6] Car", () => { describe("[Exercise 7] isEvenNumberAsync", () => { test("[19] resolves true if passed an even number", async () => { - const evenNr = await Promise.resolve(true); - expect(evenNr).toBe(true); + const result = await utils.isEvenNumberAsync(2); + expect(result).toBe(true); }); test("[20] resolves false if passed an odd number", async () => { - const oddNr = await Promise.resolve(false); - expect(oddNr).toBe(false); + const result = await utils.isEvenNumberAsync(3); + expect(result).toBe(false); + }); + + test("[21] rejects an error - NOT ON TEST", async () => { + try { + await utils.isEvenNumberAsync("isNaN"); + } catch (error) { + expect(error.message).toMatch(/error/i); + } }); });