diff --git a/implement-and-rewrite-tests/implement-tests-with-node-test/1-get-angle-type.test.js b/implement-and-rewrite-tests/implement-tests-with-node-test/1-get-angle-type.test.js index cf50f6cad..abb38c0aa 100644 --- a/implement-and-rewrite-tests/implement-tests-with-node-test/1-get-angle-type.test.js +++ b/implement-and-rewrite-tests/implement-tests-with-node-test/1-get-angle-type.test.js @@ -3,10 +3,29 @@ import test from "node:test"; import { getAngleType } from "../implement/1-get-angle-type.js"; -// TODO: Write tests to cover all cases, including boundary and invalid cases. -// Example: Identify Right Angles test("Classifies right angles", () => { const right = getAngleType(90); assert.equal(right, "Right angle"); }); +test("Classifies acute angles", () => { + const acute = getAngleType(60); + assert.equal(acute, "Acute angle"); +}); +test("Classifies obtuse angles", () => { + const obtuse = getAngleType(120); + assert.equal(obtuse, "Obtuse angle"); +}); +test("Classifies straight angles", () => { + const straight = getAngleType(180); + assert.equal(straight, "Straight angle"); +}); +test("Classifies reflex angles", () => { + const reflex = getAngleType(350); + assert.equal(reflex, "Reflex angle"); +}); +test("Classifies invalid angles", () => { + const invalid = getAngleType(370); + assert.equal(invalid, "Invalid angle"); +}); + diff --git a/implement-and-rewrite-tests/implement-tests-with-node-test/2-is-proper-fraction.test.js b/implement-and-rewrite-tests/implement-tests-with-node-test/2-is-proper-fraction.test.js index a29084605..88fd7a692 100644 --- a/implement-and-rewrite-tests/implement-tests-with-node-test/2-is-proper-fraction.test.js +++ b/implement-and-rewrite-tests/implement-tests-with-node-test/2-is-proper-fraction.test.js @@ -10,3 +10,26 @@ test("Basic proper fraction", () => { // Example: 1/2 is a proper fraction assert.equal(isProperFraction(1, 2), true); }); + +test("decimal numerator should return flase", () => { + + assert.equal(isProperFraction(0.5, 2), false); +}); + + +test("zero numerator", () => { + assert.equal(isProperFraction(0, 5), true); +}); + +test("numerator bigger then denumnator", () => { + assert.equal(isProperFraction(20, 15), false); +}); +test("zero denumator", () => { + assert.equal(isProperFraction(100, 0), false); +}); + +test("equal numrator and denumerator ", () => { + assert.equal(isProperFraction(3, 3), false); +}); + + diff --git a/implement-and-rewrite-tests/implement-tests-with-node-test/3-get-card-value.test.js b/implement-and-rewrite-tests/implement-tests-with-node-test/3-get-card-value.test.js index f5092d9b1..147cfde29 100644 --- a/implement-and-rewrite-tests/implement-tests-with-node-test/3-get-card-value.test.js +++ b/implement-and-rewrite-tests/implement-tests-with-node-test/3-get-card-value.test.js @@ -14,3 +14,35 @@ test("Arbitrary non-card string", () => { }); // TODO: What other invalid card cases can you think of? +test("Ace returns 11", () => { + assert.equal(getCardValue("A♠"), 11); +}); + +test("Number card returns its numeric value", () => { + assert.equal(getCardValue("2♥"), 2); + assert.equal(getCardValue("7♦"), 7); + assert.equal(getCardValue("10♣"), 10); +}); + +test("Face cards return 10", () => { + assert.equal(getCardValue("J♣"), 10); + assert.equal(getCardValue("Q♦"), 10); + assert.equal(getCardValue("K♥"), 10); +}); + +test("Invalid rank throws an error", () => { + assert.throws(() => getCardValue("11♠"), Error); + assert.throws(() => getCardValue("X♥"), Error); +}); + +test("Invalid suit throws an error", () => { + assert.throws(() => getCardValue("A★"), Error); +}); + +test("Invalid format throws an error", () => { + assert.throws(() => getCardValue("A"), Error); + assert.throws(() => getCardValue("♠A"), Error); + assert.throws(() => getCardValue(" A♠"), Error); + assert.throws(() => getCardValue("A♠♠"), Error); +}); + diff --git a/implement-and-rewrite-tests/implement/1-get-angle-type.js b/implement-and-rewrite-tests/implement/1-get-angle-type.js index 8a6fba050..24cc3f859 100644 --- a/implement-and-rewrite-tests/implement/1-get-angle-type.js +++ b/implement-and-rewrite-tests/implement/1-get-angle-type.js @@ -1,4 +1,5 @@ // Implement a function getAngleType + // // Don't forget to write tests in implement-tests-with-node-test. // @@ -17,5 +18,22 @@ // execute the code to ensure all tests pass. export function getAngleType(angle) { - // TODO: Implement this function + if (angle < 90) { + return "Acute angle"; + } + if (angle == 90) { + return "Right angle"; + } + if (angle < 180) { + return "Obtuse angle"; + } + if (angle == 180) { + return "Straight angle"; + } + if (angle < 360) { + return "Reflex angle"; + } + return "Invalid angle"; + + } diff --git a/implement-and-rewrite-tests/implement/2-is-proper-fraction.js b/implement-and-rewrite-tests/implement/2-is-proper-fraction.js index f983800f0..1b72ec62e 100644 --- a/implement-and-rewrite-tests/implement/2-is-proper-fraction.js +++ b/implement-and-rewrite-tests/implement/2-is-proper-fraction.js @@ -14,5 +14,16 @@ // execute the code to ensure all tests pass. export function isProperFraction(numerator, denominator) { - // TODO: Implement this function + if (Number.isNaN(numerator) || Number.isNaN(denominator)|| denominator===0) { + return false; + } + if (!Number.isInteger(numerator) || !Number.isInteger(denominator)) { + return false; + } + if (numerator >= denominator ) { + return false; + } + return true; + + } diff --git a/implement-and-rewrite-tests/implement/3-get-card-value.js b/implement-and-rewrite-tests/implement/3-get-card-value.js index 9614ab1ee..f994b1c4d 100644 --- a/implement-and-rewrite-tests/implement/3-get-card-value.js +++ b/implement-and-rewrite-tests/implement/3-get-card-value.js @@ -24,5 +24,21 @@ // execute the code to ensure all tests pass. export function getCardValue(card) { - // TODO: Implement this function -} + const match = /^(A|[2-9]|10|J|Q|K)([♠♥♦♣])$/.exec(card); + + if (!match) { + throw new Error(`Expected a number followed by a suit, but got "${card}"`); + } + + const rank = match[1]; + + if (rank === "A") { + return 11; + } + + if (["J", "Q", "K"].includes(rank)) { + return 10; + } + + return Number(rank); +} \ No newline at end of file diff --git a/implement-and-rewrite-tests/rewrite-tests-with-jest/1-get-angle-type.test.js b/implement-and-rewrite-tests/rewrite-tests-with-jest/1-get-angle-type.test.js index e0ba786ab..3803bb758 100644 --- a/implement-and-rewrite-tests/rewrite-tests-with-jest/1-get-angle-type.test.js +++ b/implement-and-rewrite-tests/rewrite-tests-with-jest/1-get-angle-type.test.js @@ -1,18 +1,30 @@ import { getAngleType } from "../implement/1-get-angle-type.js"; -// TODO: Write tests in Jest syntax to cover all cases/outcomes, -// including boundary and invalid cases. - -// Case 1: Acute angles -test(`should return "Acute angle" when (0 < angle < 90)`, () => { - // Test various acute angles, including boundary cases - expect(getAngleType(1)).toEqual("Acute angle"); - expect(getAngleType(45)).toEqual("Acute angle"); - expect(getAngleType(89)).toEqual("Acute angle"); + + + +test("Classifies right angles", () => { + expect(getAngleType(90)).toBe("Right angle"); +}); + +test("Classifies acute angles", () => { + expect(getAngleType(60)).toBe("Acute angle"); +}); + +test("Classifies obtuse angles", () => { + expect(getAngleType(120)).toBe("Obtuse angle"); }); -// Case 2: Right angle -// Case 3: Obtuse angles -// Case 4: Straight angle -// Case 5: Reflex angles -// Case 6: Invalid angles +test("Classifies straight angles", () => { + expect(getAngleType(180)).toBe("Straight angle"); +}); + +test("Classifies reflex angles", () => { + expect(getAngleType(350)).toBe("Reflex angle"); +}); + +test("Classifies invalid angles", () => { + expect(getAngleType(370)).toBe("Invalid angle"); +}); + + diff --git a/implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js b/implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js index 5d49e2d8b..ab4189973 100644 --- a/implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js +++ b/implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js @@ -2,7 +2,50 @@ import { isProperFraction } from "../implement/2-is-proper-fraction.js"; // TODO: Write tests in Jest syntax to cover all combinations of positives, negatives, zeros, and other categories. -// Special case: numerator is zero -test(`should return false when denominator is zero`, () => { - expect(isProperFraction(1, 0)).toEqual(false); +test("should return false when denominator is zero", () => { + expect(isProperFraction(1, 0)).toBe(false); }); +test("decimal numerator should return false", () => { + expect(isProperFraction(0.5, 2)).toBe(false); +}); + +test("zero numerator", () => { + expect(isProperFraction(0, 5)).toBe(true); +}); + +test("numerator bigger than denominator", () => { + expect(isProperFraction(20, 15)).toBe(false); +}); + +test("zero denominator", () => { + expect(isProperFraction(100, 0)).toBe(false); +}); + +test("equal numerator and denominator", () => { + expect(isProperFraction(3, 3)).toBe(false); +}); + +test("should return false when denominator is zero", () => { + expect(isProperFraction(1, 0)).toBe(false); +}); + +test("decimal numerator should return false", () => { + expect(isProperFraction(0.5, 2)).toBe(false); +}); + +test("zero numerator", () => { + expect(isProperFraction(0, 5)).toBe(true); +}); + +test("numerator bigger than denominator", () => { + expect(isProperFraction(20, 15)).toBe(false); +}); + +test("zero denominator should fail", () => { + expect(isProperFraction(100, 0)).toBe(false); +}); + +test("negative numerator should pass", () => { + expect(isProperFraction(-75, 100)).toBe(true); +}); + diff --git a/implement-and-rewrite-tests/rewrite-tests-with-jest/3-get-card-value.test.js b/implement-and-rewrite-tests/rewrite-tests-with-jest/3-get-card-value.test.js index a6de5a82a..db7ff9561 100644 --- a/implement-and-rewrite-tests/rewrite-tests-with-jest/3-get-card-value.test.js +++ b/implement-and-rewrite-tests/rewrite-tests-with-jest/3-get-card-value.test.js @@ -16,3 +16,34 @@ test(`Should return 11 when given an ace card`, () => { // please refer to the Jest documentation: // https://jestjs.io/docs/expect#tothrowerror +test("Number card returns its numeric value", () => { + expect(getCardValue("2♥")).toEqual(2); + expect(getCardValue("7♦")).toEqual(7); + expect(getCardValue("10♣")).toEqual(10); +}); + +test("Face cards return 10", () => { + expect(getCardValue("J♣")).toEqual(10); + expect(getCardValue("Q♦")).toEqual(10); + expect(getCardValue("K♥")).toEqual(10); +}); + +test("Invalid rank throws an error", () => { + expect(() => getCardValue("11♠")).toThrow(Error); + expect(() => getCardValue("X♥")).toThrow(Error); +}); + +test("Invalid suit throws an error", () => { + expect(() => getCardValue("A★")).toThrow(Error); +}); + +test("Invalid format throws an error", () => { + expect(() => getCardValue("A")).toThrow(Error); + expect(() => getCardValue("♠A")).toThrow(Error); + expect(() => getCardValue(" A♠")).toThrow(Error); + expect(() => getCardValue("A♠♠")).toThrow(Error); +}); + +test("Arbitrary non-card string", () => { + expect(() => getCardValue("invalid")).toThrow(); +}); \ No newline at end of file diff --git a/package.json b/package.json index 6ae16268c..1ddf89543 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "keywords": [], "author": "Code Your Future", "license": "ISC", - "dependencies": { - "jest": "^29.7.0" + "devDependencies": { + "jest": "^30.5.2" } -} \ No newline at end of file +}