From 6a1888ea9740cd49d2e4b0dc38aa3f17039cd064 Mon Sep 17 00:00:00 2001 From: dcostaprakash Date: Wed, 4 Mar 2026 00:46:42 +0000 Subject: [PATCH 1/2] Completed the tasks of the coursework --- .../implement/1-get-angle-type.js | 54 +++++++++++++++++ .../implement/2-is-proper-fraction.js | 25 ++++++++ .../implement/3-get-card-value.js | 59 +++++++++++++++++-- .../1-get-angle-type.test.js | 23 ++++++++ .../2-is-proper-fraction.test.js | 19 ++++++ .../3-get-card-value.test.js | 24 ++++++++ 6 files changed, 200 insertions(+), 4 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..50889cd1d0 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 @@ -16,6 +16,24 @@ function getAngleType(angle) { // TODO: Implement this function + if (angle <= 0 || angle >=360) { + return "Invalid angle"; + } + 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"; + } } // The line below allows us to load the getAngleType function into tests in other files. @@ -35,3 +53,39 @@ function assertEquals(actualOutput, targetOutput) { // Example: Identify Right Angles const right = getAngleType(90); assertEquals(right, "Right angle"); + +// Acute Angles +let acute = getAngleType(1); +assertEquals(acute, "Acute angle"); +acute = getAngleType(45); +assertEquals(acute, "Acute angle"); +acute = getAngleType(89); +assertEquals(acute, "Acute angle"); + +// Obtuse Angles +let obtuse = getAngleType(91); +assertEquals(obtuse, "Obtuse angle"); +obtuse = getAngleType(135); +assertEquals(obtuse, "Obtuse angle"); +obtuse = getAngleType(179); +assertEquals(obtuse, "Obtuse angle"); + +// Straight Line Angles +let straight = getAngleType(180); +assertEquals(straight, "Straight angle"); + +// Reflex Angles +let reflex = getAngleType(181); +assertEquals(reflex, "Reflex angle"); +reflex = getAngleType(287); +assertEquals(reflex, "Reflex angle"); +reflex = getAngleType(359); +assertEquals(reflex, "Reflex angle"); + +// Invalid Angles +let invalid = getAngleType(0); +assertEquals(invalid, "Invalid angle"); +invalid = getAngleType(360); +assertEquals(invalid, "Invalid angle"); +invalid = getAngleType(-20); +assertEquals(invalid, "Invalid angle"); \ No newline at end of file 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..2eea951532 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 @@ -12,6 +12,10 @@ function isProperFraction(numerator, denominator) { // TODO: Implement this function + if (denominator === 0) { + return false; + } + return Math.abs(numerator) < Math.abs(denominator); } // The line below allows us to load the isProperFraction function into tests in other files. @@ -31,3 +35,24 @@ function assertEquals(actualOutput, targetOutput) { // Example: 1/2 is a proper fraction assertEquals(isProperFraction(1, 2), true); +assertEquals(isProperFraction(5, 1), false); +assertEquals(isProperFraction(7, 7), false); + + +//Negative fractions +assertEquals(isProperFraction(-5, -2), false); +assertEquals(isProperFraction(-1, -2), true); +assertEquals(isProperFraction(-5, 3), false); +assertEquals(isProperFraction(1, -2), true); +assertEquals(isProperFraction(4, -3), false); + +//0 in Numerator or denominator +assertEquals(isProperFraction(0, -2), true); +assertEquals(isProperFraction(-1, 0), false); +assertEquals(isProperFraction(0, 0), false); + +// With decimals +assertEquals(isProperFraction(2.5, -3), true); +assertEquals(isProperFraction(-3.4, 6.3), true); +assertEquals(isProperFraction(-7.4, 3.3), false); + 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 c7559e787e..afae3364d6 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 @@ -23,6 +23,26 @@ function getCardValue(card) { // TODO: Implement this function + const cardSuit = card.slice(-1); + const cardRank = card.slice(0, -1); + + const validSuits = ["♠", "♥", "♦", "♣"]; + const validRanks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]; + + if (!validSuits.includes(cardSuit) || !validRanks.includes(cardRank)) { + throw new Error("Invalid card"); + } + + switch (cardRank) { + case "A": + return 11; + case "K": + case "Q": + case "J": + return 10; + default: + return Number(cardRank); + } } // The line below allows us to load the getCardValue function into tests in other files. @@ -40,13 +60,44 @@ function assertEquals(actualOutput, targetOutput) { // TODO: Write tests to cover all outcomes, including throwing errors for invalid cards. // Examples: assertEquals(getCardValue("9♠"), 9); +assertEquals(getCardValue("3♣"), 3); +assertEquals(getCardValue("10♦"), 10); +assertEquals(getCardValue("J♥"), 10); +assertEquals(getCardValue("Q♠"), 10); +assertEquals(getCardValue("K♦"), 10); +assertEquals(getCardValue("A♣"), 11); // Handling invalid cards try { - getCardValue("invalid"); - - // This line will not be reached if an error is thrown as expected + getCardValue("11♠"); // This line will not be reached if an error is thrown as expected + console.error("Error was not thrown for invalid card"); +} catch (e) {} +try { + getCardValue("01♦"); + console.error("Error was not thrown for invalid card"); +} catch (e) {} +try { + getCardValue("1♣"); + console.error("Error was not thrown for invalid card"); +} catch (e) {} +try { + getCardValue(""); + console.error("Error was not thrown for invalid card"); +} catch (e) {} +try { + getCardValue("K♠♠"); + console.error("Error was not thrown for invalid card"); +} catch (e) {} +try { + getCardValue("QQ♣"); + console.error("Error was not thrown for invalid card"); +} catch (e) {} +try { + getCardValue("♦"); + console.error("Error was not thrown for invalid card"); +} catch (e) {} +try { + getCardValue("J"); console.error("Error was not thrown for invalid card"); } catch (e) {} - // What other invalid card cases can you think of? 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..2d8d695d06 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 @@ -14,7 +14,30 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => { }); // Case 2: Right angle +test(`should return "Right angle" when (angle === 90)`, () => { + expect(getAngleType(90)).toEqual("Right angle"); +}); // Case 3: Obtuse angles +test(`should return "Obtuse angle" when (90 < angle < 180)`, () => { + expect(getAngleType(90.1)).toEqual("Obtuse angle"); + expect(getAngleType(135)).toEqual("Obtuse angle"); + expect(getAngleType(179.9)).toEqual("Obtuse angle"); +}); // Case 4: Straight angle +test(`should return "Straight angle" when (angle === 180)`, () => { + expect(getAngleType(180)).toEqual("Straight angle"); +}); // Case 5: Reflex angles +test(`should return "Reflex angle" when (180 < angle < 360)`, () => { + expect(getAngleType(180.1)).toEqual("Reflex angle"); + expect(getAngleType(271)).toEqual("Reflex angle"); + expect(getAngleType(359.9)).toEqual("Reflex angle"); +}); + // Case 6: Invalid angles +test(`should return "Invalid angle" when (angle >= 360 || angle === 0 || angle < 0)`, () => { + expect(getAngleType(0)).toEqual("Invalid angle"); + expect(getAngleType(360)).toEqual("Invalid angle"); + expect(getAngleType(-25)).toEqual("Invalid angle"); + expect(getAngleType(460)).toEqual("Invalid angle"); +}); 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..9b5bf2d9a5 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 @@ -7,4 +7,23 @@ const isProperFraction = require("../implement/2-is-proper-fraction"); // Special case: numerator is zero test(`should return false when denominator is zero`, () => { expect(isProperFraction(1, 0)).toEqual(false); + expect(isProperFraction(0, 0)).toEqual(false); +}); + +// When numerator is greater than the denominator +test(`should return false when numerator > denominator`, () => { + expect(isProperFraction(5, 3)).toEqual(false); + expect(isProperFraction(140, 7)).toEqual(false); + expect(isProperFraction(-16, -5)).toEqual(false); + expect(isProperFraction(-70, 14)).toEqual(false); + expect(isProperFraction(100, -11)).toEqual(false); +}); + +// When the fraction is correct +test(`should return true when numerator < denominator`, () => { + expect(isProperFraction(2, 5)).toEqual(true); + expect(isProperFraction(1, -4)).toEqual(true); + expect(isProperFraction(-100, -214)).toEqual(true); + expect(isProperFraction(-1502, 4000)).toEqual(true); + expect(isProperFraction(18, 32)).toEqual(true); }); 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..807d16e6a9 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 @@ -7,12 +7,36 @@ const getCardValue = require("../implement/3-get-card-value"); // Case 1: Ace (A) test(`Should return 11 when given an ace card`, () => { expect(getCardValue("A♠")).toEqual(11); + expect(getCardValue("A♥")).toEqual(11); + expect(getCardValue("A♣")).toEqual(11); }); // Suggestion: Group the remaining test data into these categories: // Number Cards (2-10) +test(`Should return number when given an 2-10 card`, () => { + expect(getCardValue("8♠")).toEqual(8); + expect(getCardValue("5♥")).toEqual(5); + expect(getCardValue("9♣")).toEqual(9); + expect(getCardValue("2♥")).toEqual(2); + expect(getCardValue("7♦")).toEqual(7); +}); // Face Cards (J, Q, K) +test(`Should return 10 when given a face card`, () => { + expect(getCardValue("J♠")).toEqual(10); + expect(getCardValue("Q♣")).toEqual(10); + expect(getCardValue("K♥")).toEqual(10); + expect(getCardValue("J♦")).toEqual(10); +}); + // Invalid Cards +test(`Should return Invalid when given an Invalid card`, () => { + expect(() => getCardValue("11♠")).toThrow("Invalid card"); + expect(() => getCardValue("01♥")).toThrow("Invalid card"); + expect(() => getCardValue("1♣")).toThrow("Invalid card"); + expect(() => getCardValue("K♠♣")).toThrow("Invalid card"); + expect(() => getCardValue("QQ♥")).toThrow("Invalid card"); + expect(() => getCardValue("")).toThrow("Invalid card"); +}); // To learn how to test whether a function throws an error as expected in Jest, // please refer to the Jest documentation: From f55833e801698897fb101cdb82c563e856032043 Mon Sep 17 00:00:00 2001 From: dcostaprakash Date: Thu, 12 Mar 2026 17:10:36 +0000 Subject: [PATCH 2/2] Changes done as per the review --- .../implement/2-is-proper-fraction.js | 15 +++++++++++---- .../2-is-proper-fraction.test.js | 11 ++++++++++- .../3-get-card-value.test.js | 3 ++- 3 files changed, 23 insertions(+), 6 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 2eea951532..2e4ef734ad 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 @@ -12,7 +12,11 @@ function isProperFraction(numerator, denominator) { // TODO: Implement this function - if (denominator === 0) { + if (denominator === 0) { + return false; + } + // check if both numbers are integers + if (!Number.isInteger(numerator) || !Number.isInteger(denominator)) { return false; } return Math.abs(numerator) < Math.abs(denominator); @@ -38,7 +42,6 @@ assertEquals(isProperFraction(1, 2), true); assertEquals(isProperFraction(5, 1), false); assertEquals(isProperFraction(7, 7), false); - //Negative fractions assertEquals(isProperFraction(-5, -2), false); assertEquals(isProperFraction(-1, -2), true); @@ -52,7 +55,11 @@ assertEquals(isProperFraction(-1, 0), false); assertEquals(isProperFraction(0, 0), false); // With decimals -assertEquals(isProperFraction(2.5, -3), true); -assertEquals(isProperFraction(-3.4, 6.3), true); +assertEquals(isProperFraction(2.5, -3), false); +assertEquals(isProperFraction(-3.4, 6.3), false); assertEquals(isProperFraction(-7.4, 3.3), false); +// Boundary cases where |Numerator| equals |Denominator| +assertEquals(isProperFraction(7, -7), false); +assertEquals(isProperFraction(-21, -21), false); +assertEquals(isProperFraction(119, 119), false); 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 9b5bf2d9a5..b32624c842 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 @@ -11,12 +11,21 @@ test(`should return false when denominator is zero`, () => { }); // When numerator is greater than the denominator -test(`should return false when numerator > denominator`, () => { +test(`should return false when |numerator| >= |denominator|`, () => { expect(isProperFraction(5, 3)).toEqual(false); expect(isProperFraction(140, 7)).toEqual(false); expect(isProperFraction(-16, -5)).toEqual(false); expect(isProperFraction(-70, 14)).toEqual(false); expect(isProperFraction(100, -11)).toEqual(false); + expect(isProperFraction(-13, -13)).toEqual(false); +}); + +// When the numerator or denominator is not an integer +test(`should return false when numerator or denominator is not an integer`, () => { + expect(isProperFraction(-1.5, 2)).toEqual(false); + expect(isProperFraction(2.3, 4)).toEqual(false); + expect(isProperFraction(3.7, 7)).toEqual(false); + expect(isProperFraction(102.8, -103.2)).toEqual(false); }); // When the fraction is correct 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 807d16e6a9..f666e2af7f 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 @@ -36,9 +36,10 @@ test(`Should return Invalid when given an Invalid card`, () => { expect(() => getCardValue("K♠♣")).toThrow("Invalid card"); expect(() => getCardValue("QQ♥")).toThrow("Invalid card"); expect(() => getCardValue("")).toThrow("Invalid card"); + expect(() => getCardValue("5♡")).toThrow("Invalid card"); + expect(() => getCardValue("Q")).toThrow("Invalid 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 -