From 80d0651ca767354e41251cf073a56e9e5c5e6e2c Mon Sep 17 00:00:00 2001 From: Sandani Kannangara Date: Sun, 21 Jun 2026 16:50:52 +0100 Subject: [PATCH 1/3] Implement getAngleType with assert and Jest tests --- .../implement/1-get-angle-type.js | 28 ++++++++++++-- .../1-get-angle-type.test.js | 38 ++++++++++++++++--- 2 files changed, 57 insertions(+), 9 deletions(-) diff --git a/Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js b/Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js index 9e05a871e2..cd9ade3019 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js +++ b/Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js @@ -12,10 +12,15 @@ // Acceptance criteria: // After you have implemented the function, write tests to cover all the cases, and -// execute the code to ensure all tests pass. +// execute the code to ensure all tests pass.Reflex function getAngleType(angle) { - // TODO: Implement this function + if (angle > 0 && angle < 90) return "Acute angle"; + if (angle === 90) return "Right angle"; + if (angle > 90 && angle < 180) return "Obtuse angle"; + if (angle === 180) return "Straight angle"; + if (angle > 180 && angle < 360) return "Reflex angle"; + return "Invalid angle"; } // The line below allows us to load the getAngleType function into tests in other files. @@ -33,5 +38,20 @@ function assertEquals(actualOutput, targetOutput) { // TODO: Write tests to cover all cases, including boundary and invalid cases. // Example: Identify Right Angles -const right = getAngleType(90); -assertEquals(right, "Right angle"); +assertEquals(getAngleType(1), "Acute angle"); +assertEquals(getAngleType(45), "Acute angle"); +assertEquals(getAngleType(89), "Acute angle"); +assertEquals(getAngleType(90), "Right angle"); +assertEquals(getAngleType(91), "Obtuse angle"); +assertEquals(getAngleType(135), "Obtuse angle"); +assertEquals(getAngleType(179), "Obtuse angle"); +assertEquals(getAngleType(180), "Straight angle"); +assertEquals(getAngleType(181), "Reflex angle"); +assertEquals(getAngleType(269), "Reflex angle"); +assertEquals(getAngleType(270), "Reflex angle"); +assertEquals(getAngleType(271), "Reflex angle"); +assertEquals(getAngleType(359), "Reflex angle"); +assertEquals(getAngleType(360), "Invalid angle"); +assertEquals(getAngleType(0), "Invalid angle"); +assertEquals(getAngleType(-1), "Invalid angle"); +assertEquals(getAngleType(400), "Invalid angle"); diff --git a/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/1-get-angle-type.test.js b/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/1-get-angle-type.test.js index d777f348d3..8c30d01ff0 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/1-get-angle-type.test.js +++ b/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/1-get-angle-type.test.js @@ -6,11 +6,39 @@ const getAngleType = require("../implement/1-get-angle-type"); // 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"); +describe("getAngleType", function () { + test("Acute angles", function () { + expect(getAngleType(1)).toEqual("Acute angle"); + expect(getAngleType(45)).toEqual("Acute angle"); + expect(getAngleType(89)).toEqual("Acute angle"); + }); + + test("Right angle", function () { + expect(getAngleType(90)).toEqual("Right angle"); + }); + + test("Obtuse angles", function () { + expect(getAngleType(91)).toEqual("Obtuse angle"); + expect(getAngleType(135)).toEqual("Obtuse angle"); + expect(getAngleType(179)).toEqual("Obtuse angle"); + }); + test("Straight angle", function () { + expect(getAngleType(180)).toEqual("Straight angle"); + }); + + test("Reflex angles", function () { + expect(getAngleType(181)).toEqual("Reflex angle"); + expect(getAngleType(269)).toEqual("Reflex angle"); + expect(getAngleType(270)).toEqual("Reflex angle"); + expect(getAngleType(271)).toEqual("Reflex angle"); + expect(getAngleType(359)).toEqual("Reflex angle"); + }); + test("Invalid angle", function () { + expect(getAngleType(0)).toEqual("Invalid angle"); + expect(getAngleType(360)).toEqual("Invalid angle"); + expect(getAngleType(400)).toEqual("Invalid angle"); + expect(getAngleType(-1)).toEqual("Invalid angle"); + }); }); // Case 2: Right angle From b1186bdd1fadb09d12541ce44d63e4a6e44c3485 Mon Sep 17 00:00:00 2001 From: Sandani Kannangara Date: Sun, 21 Jun 2026 19:25:32 +0100 Subject: [PATCH 2/3] Implement isProperFraction and add assert and Jest tests" --- .../implement/2-is-proper-fraction.js | 19 +++++++++-- .../2-is-proper-fraction.test.js | 33 +++++++++++++++++-- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js b/Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js index 970cb9b641..abd93fe6b7 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js +++ b/Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js @@ -11,7 +11,12 @@ // execute the code to ensure all tests pass. function isProperFraction(numerator, denominator) { - // TODO: Implement this function + let numeratorVal = Math.abs(numerator); + let denominatorVal = Math.abs(denominator); + + if (denominator === 0) return false; + if (numeratorVal < denominatorVal) return true; + return false; } // The line below allows us to load the isProperFraction function into tests in other files. @@ -26,8 +31,18 @@ function assertEquals(actualOutput, targetOutput) { ); } +assertEquals(isProperFraction(0, 4), true); +assertEquals(isProperFraction(4, 0), false); +assertEquals(isProperFraction(7, 4), false); +assertEquals(isProperFraction(4, 7), true); +assertEquals(isProperFraction(-7, 4), false); +assertEquals(isProperFraction(7, -4), false); +assertEquals(isProperFraction(-4, 7), true); +assertEquals(isProperFraction(4, -7), true); +assertEquals(isProperFraction(4, 4), false); + // TODO: Write tests to cover all cases. // What combinations of numerators and denominators should you test? // Example: 1/2 is a proper fraction -assertEquals(isProperFraction(1, 2), true); +//assertEquals(isProperFraction(1, 2), true); diff --git a/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js b/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js index 7f087b2ba1..f54d6889bc 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js +++ b/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js @@ -4,7 +4,34 @@ const isProperFraction = require("../implement/2-is-proper-fraction"); // 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); +describe("isProperFraction", () => { + test("returns true when numerator is zero", () => { + expect(isProperFraction(0, 4)).toEqual(true); + }); + + test("returns false when denominator is zero", () => { + expect(isProperFraction(4, 0)).toEqual(false); + }); + + test("returns true for proper positive fractions", () => { + expect(isProperFraction(4, 7)).toEqual(true); + }); + + test("returns false for improper fractions", () => { + expect(isProperFraction(7, 4)).toEqual(false); + }); + + test("handles proper negative fractions", () => { + expect(isProperFraction(-4, 7)).toEqual(true); + expect(isProperFraction(4, -7)).toEqual(true); + }); + + test("handles improper negative fractions", () => { + expect(isProperFraction(-7, 4)).toEqual(false); + expect(isProperFraction(7, -4)).toEqual(false); + }); + + test("returns false when numerator equals denominator", () => { + expect(isProperFraction(4, 4)).toEqual(false); + }); }); From fc56676d0d381b72704d1095166fe452f7807a11 Mon Sep 17 00:00:00 2001 From: Sandani Kannangara Date: Sun, 21 Jun 2026 21:36:27 +0100 Subject: [PATCH 3/3] Implement getCardValue and add assert and Jest tests --- .../implement/3-get-card-value.js | 39 +++++++++++++++++-- .../3-get-card-value.test.js | 29 ++++++++++++-- 2 files changed, 60 insertions(+), 8 deletions(-) diff --git a/Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js b/Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js index ff5c532e1d..96051b232a 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js +++ b/Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js @@ -22,7 +22,14 @@ // execute the code to ensure all tests pass. function getCardValue(card) { - // TODO: Implement this function + let rank = card.slice(0, -1); + let suit = card.slice(-1); + const suitArray = ["♠", "♥", "♦", "♣"]; + if (!suitArray.includes(suit)) throw new Error("Invalid card"); + if (rank === "A") return 11; + if (rank === "Q" || rank === "K" || rank === "J") return 10; + if (Number(rank) >= 2 && Number(rank) <= 10) return Number(rank); + throw new Error("Invalid card"); } // The line below allows us to load the getCardValue function into tests in other files. @@ -39,13 +46,37 @@ function assertEquals(actualOutput, targetOutput) { // TODO: Write tests to cover all outcomes, including throwing errors for invalid cards. // Examples: -assertEquals(getCardValue("9♠"), 9); +assertEquals(getCardValue("A♠"), 11); +assertEquals(getCardValue("A♣"), 11); +assertEquals(getCardValue("Q♠"), 10); +assertEquals(getCardValue("K♣"), 10); +assertEquals(getCardValue("J♦"), 10); +assertEquals(getCardValue("2♠"), 2); +assertEquals(getCardValue("10♣"), 10); // Handling invalid cards try { - getCardValue("invalid"); + getCardValue("AX"); + console.error("Error was not thrown for invalid card 😢"); +} catch (e) { + console.log("Error thrown for invalid card 🎉"); +} +try { + getCardValue("B♣"); + console.error("Error was not thrown for invalid card 😢"); +} catch (e) { + console.log("Error thrown for invalid card 🎉"); +} + +try { + getCardValue("1♦"); + console.error("Error was not thrown for invalid card 😢"); +} catch (e) { + console.log("Error thrown for invalid card 🎉"); +} - // This line will not be reached if an error is thrown as expected +try { + getCardValue("11♣"); console.error("Error was not thrown for invalid card 😢"); } catch (e) { console.log("Error thrown for invalid card 🎉"); diff --git a/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/3-get-card-value.test.js b/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/3-get-card-value.test.js index cf7f9dae2e..d30a290e9f 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/3-get-card-value.test.js +++ b/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/3-get-card-value.test.js @@ -4,9 +4,31 @@ const getCardValue = require("../implement/3-get-card-value"); // TODO: Write tests in Jest syntax to cover all possible outcomes. -// Case 1: Ace (A) -test(`Should return 11 when given an ace card`, () => { - expect(getCardValue("A♠")).toEqual(11); +describe("getCardValue", () => { + describe("Valid Card", () => { + test("Ace cards return 11", () => { + expect(getCardValue("A♠")).toEqual(11); + expect(getCardValue("A♣")).toEqual(11); + }); + test("Face cards return 10", () => { + expect(getCardValue("Q♠")).toEqual(10); + expect(getCardValue("K♣")).toEqual(10); + expect(getCardValue("J♦")).toEqual(10); + }); + test("Number cards return their value", () => { + expect(getCardValue("2♠")).toEqual(2); + expect(getCardValue("10♣")).toEqual(10); + }); + }); + + describe("Invalid Card", () => { + test("Throw error for invalid inputs", () => { + expect(() => getCardValue("AX")).toThrow("Invalid card"); + expect(() => getCardValue("B♣")).toThrow("Invalid card"); + expect(() => getCardValue("1♦")).toThrow("Invalid card"); + expect(() => getCardValue("11♣")).toThrow("Invalid card"); + }); + }); }); // Suggestion: Group the remaining test data into these categories: @@ -17,4 +39,3 @@ test(`Should return 11 when given an ace card`, () => { // To learn how to test whether a function throws an error as expected in Jest, // please refer to the Jest documentation: // https://jestjs.io/docs/expect#tothrowerror -