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..3bda30351 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,38 @@ 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(45); + assert.equal(acute, "Acute angle"); +}); + +test("Classifies obtuse angle", () => { + + const obtuse = getAngleType(140); + assert.equal(obtuse, "Obtuse angle"); +}); + +test("Classifies straight angle", () => { + + const straight = getAngleType(180); + assert.equal(straight, "Straight angle"); +}); + +test("Classifies reflex angle", () => { + + const reflex = getAngleType(250); + assert.equal(reflex, "Reflex angle"); +}); + +test("Classifies invalid angle", () => { + + assert.equal(getAngleType("-1"), "Invalid angle"); + assert.equal(getAngleType("365"), "Invalid angle"); +}); \ No newline at end of file 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..d5d4edd2d 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 @@ -3,10 +3,9 @@ import test from "node:test"; import { isProperFraction } from "../implement/2-is-proper-fraction.js"; -// TODO: Write tests to cover all cases. -// What combinations of numerators and denominators should you test? - test("Basic proper fraction", () => { - // Example: 1/2 is a proper fraction assert.equal(isProperFraction(1, 2), true); + assert.equal(isProperFraction(2, 1), false); + assert.equal(isProperFraction(-2, 1), true); + assert.equal(isProperFraction(1, -2), 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..0203d28b3 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 @@ -3,14 +3,54 @@ import test from "node:test"; import { getCardValue } from "../implement/3-get-card-value.js"; -// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards. - test("Valid single-digit card", () => { assert.equal(getCardValue("9♠"), 9); }); +test("Ace card returns 11", () => { + assert.equal(getCardValue("A♠"), 11); +}); + +test("Face cards returns 10", () => { + assert.equal(getCardValue("J♣"), 10); + assert.equal(getCardValue("Q♦"), 10); + assert.equal(getCardValue("K♥"), 10); +}); test("Arbitrary non-card string", () => { - assert.throws(() => getCardValue("invalid"), /Expected a number followed by a suit, but got "invalid"/, "Expected clear error"); + assert.throws( + () => getCardValue("invalid"), + /Expected a number followed by a suit, but got "invalid"/, + "Expected clear error" + ); +}); + +test("Negative values string", () => { + assert.throws( + () => getCardValue("invalid"), /Expected a number followed by a suit, but got "invalid"/, + "Expected clear error" + ); +}); + +test("Lowercase ranks", () => { + assert.equal(getCardValue("a♦"), 11); + assert.equal(getCardValue("j♦"), 10); +}); + +test("Missing or broken suit", () => { + assert.throws( + () => (getCardValue("9")), /Expected/ + ); + assert.throws( + () => (getCardValue("")), /Expected/ + ); +}); + +test("Two digit rank", () => { + assert.equal(getCardValue("10♦"), 10); +}); + +test("Adding junks with extra character", () => { + () => assert.throws(getCardValue("9♦y"), /Expected/ +); }); -// TODO: What other invalid card cases can you think of? 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..267286061 100644 --- a/implement-and-rewrite-tests/implement/1-get-angle-type.js +++ b/implement-and-rewrite-tests/implement/1-get-angle-type.js @@ -1,21 +1,22 @@ -// Implement a function getAngleType -// -// Don't forget to write tests in implement-tests-with-node-test. -// -// When given an angle in degrees, it should return a string indicating the type of angle: -// - "Acute angle" for angles greater than 0° and less than 90° -// - "Right angle" for exactly 90° -// - "Obtuse angle" for angles greater than 90° and less than 180° -// - "Straight angle" for exactly 180° -// - "Reflex angle" for angles greater than 180° and less than 360° -// - "Invalid angle" for angles outside the valid range. +export function getAngleType(angle){ + if (angle === 90){ + return "Right angle"; + } + if (angle > 0 && angle < 90){ + return "Acute angle"; + } + if (angle > 90 && angle < 180){ + return "Obtuse angle"; + } + if (angle === 180){ + return "Straight angle"; + } + if (angle >180 && angle < 360){ + return "Reflex angle"; + } else + return "Invalid angle" + } + -// Assumption: The parameter is a valid number. (You do not need to handle non-numeric inputs.) -// Acceptance criteria: -// After you have implemented the function, write tests to cover all the cases, and -// execute the code to ensure all tests pass. -export function getAngleType(angle) { - // TODO: Implement this function -} 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..4b8d8cadd 100644 --- a/implement-and-rewrite-tests/implement/2-is-proper-fraction.js +++ b/implement-and-rewrite-tests/implement/2-is-proper-fraction.js @@ -1,18 +1,17 @@ -// Implement a function isProperFraction, -// -// Don't forget to write tests in implement-tests-with-node-test. -// -// When given two numbers, a numerator and a denominator, it should return true if -// the given numbers form a proper fraction, and false otherwise. - -// Assumption: The parameters are valid numbers (not NaN or Infinity). - -// Note: If you are unfamiliar with proper fractions, please look up its mathematical definition. - -// Acceptance criteria: -// After you have implemented the function, write tests to cover all the cases, and -// execute the code to ensure all tests pass. - export function isProperFraction(numerator, denominator) { - // TODO: Implement this function + if (numerator < denominator) { + return true; + } + if (numerator > denominator) { + return false; + } + if (numerator === denominator){ + return false + } + if (-numerator < denominator) { + return true; + } + if (numerator > -denominator) { + return false; + } } 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..3ecf2844c 100644 --- a/implement-and-rewrite-tests/implement/3-get-card-value.js +++ b/implement-and-rewrite-tests/implement/3-get-card-value.js @@ -1,28 +1,34 @@ -// This problem involves playing cards: https://en.wikipedia.org/wiki/Standard_52-card_deck - -// Don't forget to write tests in implement-tests-with-node-test. - -// Implement a function getCardValue, when given a string representing a playing card, -// should return the numerical value of the card. - -// A valid card string will contain a rank followed by the suit. -// The rank can be one of the following strings: -// "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" -// The suit can be one of the following emojis: -// "♠", "♥", "♦", "♣" -// For example: "A♠", "2♥", "10♥", "J♣", "Q♦", "K♦". +export function getCardValue(card) { + const suit = card.slice(-1); + const rank = card.slice(0, -1).toUpperCase(); -// When the card is an ace ("A"), the function should return 11. -// When the card is a face card ("J", "Q", "K"), the function should return 10. -// When the card is a number card ("2" to "10"), the function should return its numeric value. + const validSuit = ["♠", "♥", "♦", "♣"]; + const validRank = [ + "A", + "2", + "3", + "4", + "5", + "6", + "7", + "8", + "9", + "10", + "J", + "Q", + "K", + ]; -// When the card string is invalid (not following the above format), the function should -// throw an error. + if (!validSuit.includes(suit) || !validRank.includes(rank)) { + throw new Error(`Expected a number followed by a suit, but got "${card}"`); + } + if (rank === "A") { + return 11; + } + if (rank === "J" || rank === "Q" || rank === "K") { + return 10; + } + return Number(rank); +} -// Acceptance criteria: -// After you have implemented the function, write tests to cover all the cases, and -// execute the code to ensure all tests pass. -export function getCardValue(card) { - // TODO: Implement this function -} 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..ccf7cd423 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,34 @@ 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"); }); -// Case 2: Right angle -// Case 3: Obtuse angles -// Case 4: Straight angle -// Case 5: Reflex angles -// Case 6: Invalid angles +test(`Should return "Right angle" when (angle === 90)`, () => { + expect(getAngleType(90)).toEqual("Right angle"); +}); + +test(`Should return "Obtuse Angle" when (angle > 90 and angle < 180)`, () => { + expect(getAngleType(91)).toEqual("Obtuse angle"); + expect(getAngleType(120)).toEqual("Obtuse angle"); + expect(getAngleType(170)).toEqual("Obtuse angle"); +}); + +test(`Should return "Straight angle" when (angle === 180)`, () => { + expect(getAngleType(180)).toEqual("Straight angle"); +}); + +test(`Should return "Reflex angle" when (angle > 180 and angle < 360)`, () => { + expect(getAngleType(181)).toEqual("Reflex angle"); + expect(getAngleType(195)).toEqual("Reflex angle"); + expect(getAngleType(290)).toEqual("Reflex angle"); +}); + +test(`Should return "Invalid angle when (angle < 0 and angle > 360)`, () => { + expect(getAngleType(-1)).toEqual("Invalid angle"); + expect(getAngleType(361)).toEqual("Invalid angle"); + expect(getAngleType(0)).toEqual("Invalid angle"); + expect(getAngleType(360)).toEqual("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..7c5bb1422 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 @@ -1,8 +1,21 @@ 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 negative`, () => { + expect(isProperFraction(1, -2)).toEqual(false); +}); + +test(`Should return true when denominator is > numerator`, () => { + expect(isProperFraction(1, 2)).toEqual(true); +}); + +test(`Should return true when numerator is zero`, () => { + expect(isProperFraction(0, 4)).toEqual(true); +}); + +test(`Should return false when numerator === denominator`, () => { + expect(isProperFraction(5, 5)).toEqual(false); +}); 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..3698be7a1 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 @@ -1,18 +1,27 @@ import { getCardValue } from "../implement/3-get-card-value.js"; -// 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); }); -// Suggestion: Group the remaining test data into these categories: -// Number Cards (2-10) -// Face Cards (J, Q, K) -// Invalid Cards +test(`Should return 10 when given "J" "Q" "K" face card`, () => { + expect(getCardValue("J♠")).toEqual(10); + expect(getCardValue("Q♥")).toEqual(10); + expect(getCardValue("K♣")).toEqual(10); +}); -// 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 +test(`Should return 2 to 10 when given Number card`, () => { + expect(getCardValue("2♣")).toEqual(2); + expect(getCardValue("5♥")).toEqual(5); + expect(getCardValue("8♠")).toEqual(8); +}); +test(`Should throw error when given invalid card`, () => { + expect(() => getCardValue("x♣")).toThrow(); + expect(() => getCardValue("1♥1")).toThrow(); + expect(() => getCardValue("b♣")).toThrow(); + expect(() => getCardValue("k♦j")).toThrow(); + expect(() => getCardValue("")).toThrow(); + expect(() => getCardValue("9")).toThrow(); + expect(() => getCardValue("-8♦")).toThrow(); +});