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..1085b2bfd 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 @@ -1,12 +1,40 @@ +import {getAngleType} from "../implement/1-get-angle-type.js"; import assert from "node:assert"; import test from "node:test"; -import { getAngleType } from "../implement/1-get-angle-type.js"; +test("corrctly get angle type", function(){ + assert.equal(getAngleType(75), "Acute angle"); +}); + +test("corrctly get angle type", function(){ + assert.equal(getAngleType(90), "Right angle"); +}); + +test("corrctly get angle type", function(){ + assert.equal(getAngleType(120), "Obtuse angle"); +}); + +test("corrctly get angle type", function(){ + assert.equal(getAngleType(180), "Straight angle"); +}); -// TODO: Write tests to cover all cases, including boundary and invalid cases. -// Example: Identify Right Angles +test("corrctly get angle type", function(){ + assert.equal(getAngleType(210), "Reflex angle"); +}); + +test("corrctly get angle type", function(){ + assert.equal(getAngleType(400), "Invalid angle"); +}); + +test("corrctly get angle type", function(){ + assert.equal(getAngleType(0), "Invalid angle"); +}); -test("Classifies right angles", () => { - const right = getAngleType(90); - assert.equal(right, "Right angle"); +test("corrctly get angle type", function(){ + assert.equal(getAngleType(360), "Invalid angle"); }); + +test("corrctly get angle type", function(){ + assert.equal(getAngleType(-70), "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..ec3036b0e 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 @@ -7,6 +7,45 @@ import { isProperFraction } from "../implement/2-is-proper-fraction.js"; // 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); }); + +test("Basic proper fraction", () => { + assert.equal(isProperFraction(2, 1), false); +}); + +test("Basic proper fraction", () => { + assert.equal(isProperFraction(1, 0), false); +}); + +test("Basic proper fraction", () => { + assert.equal(isProperFraction(0, 1), true); +}); + +test("Basic proper fraction", () => { + assert.equal(isProperFraction(1, 1), false); +}); + +test("Basic proper fraction", () => { + assert.equal(isProperFraction(-1, 2), true); +}); + +test("Basic proper fraction", () => { + assert.equal(isProperFraction(1, -2), true); +}); + +test("Basic proper fraction", () => { + assert.equal(isProperFraction(-1, -2), true); +}); + +test("Basic proper fraction", () => { + assert.equal(isProperFraction(-5, 2), false); +}); + +test("Basic proper fraction", () => { + assert.equal(isProperFraction(5, -2), false); +}); + +test("Basic proper fraction", () => { + assert.equal(isProperFraction(-3, -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..39eb05249 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 @@ -9,8 +9,66 @@ test("Valid single-digit card", () => { assert.equal(getCardValue("9♠"), 9); }); +test("Valid ten", () => { + assert.equal(getCardValue("10♥"), 10); +}); + +test("Ace returns 11", () => { + assert.equal(getCardValue("A♦"), 11); +}); + +test("Face cards return 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("Missing suit throws", () => { + assert.throws(() => getCardValue("A"), /Expected a number followed by a suit/); }); + // TODO: What other invalid card cases can you think of? + +test("Empty string throws", () => { + assert.throws( + () => getCardValue(""), + /Expected a number followed by a suit/ + ); +}); + +test("Suit only throws", () => { + assert.throws( + () => getCardValue("♠"), + /Expected a number followed by a suit/ + ); +}); + +test("Invalid rank throws", () => { + assert.throws( + () => getCardValue("1♠"), + /Expected a number followed by a suit/ + ); +}); + +test("Invalid suit throws", () => { + assert.throws( + () => getCardValue("9X"), + /Expected a number followed by a suit/ + ); +}); + +test("Rank with no suit character throws", () => { + assert.throws( + () => getCardValue("10"), + /Expected a number followed by a suit/ + ); +}); 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..e126ea800 100644 --- a/implement-and-rewrite-tests/implement/1-get-angle-type.js +++ b/implement-and-rewrite-tests/implement/1-get-angle-type.js @@ -1,5 +1,17 @@ // Implement a function getAngleType -// +function getAngleType(angle) { + return angle > 0 && angle < 90 + ? "Acute angle" + : angle === 90 + ? "Right angle" + : angle > 90 && angle < 180 + ? "Obtuse angle" + : angle === 180 + ? "Straight angle" + : angle > 180 && angle < 360 + ? "Reflex angle" + : "Invalid angle"; +} // 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: @@ -16,6 +28,4 @@ // 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 -} +export { getAngleType } 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..b8c7173b8 100644 --- a/implement-and-rewrite-tests/implement/2-is-proper-fraction.js +++ b/implement-and-rewrite-tests/implement/2-is-proper-fraction.js @@ -13,6 +13,12 @@ // 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 +function isProperFraction(numerator, denominator) { + if (denominator === 0) { + return false; + } + return Math.abs(numerator) < Math.abs(denominator); } + + +export { isProperFraction } 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..a60c17103 100644 --- a/implement-and-rewrite-tests/implement/3-get-card-value.js +++ b/implement-and-rewrite-tests/implement/3-get-card-value.js @@ -23,6 +23,33 @@ // 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 +function getCardValue(card) { + const suit = card.slice(-1); + const rank = card.slice(0, -1); + const suits = ["♠", "♥", "♦", "♣"]; + const values = { + A: 11, + J: 10, + Q: 10, + K: 10, + "10": 10, + "9": 9, + "8": 8, + "7": 7, + "6": 6, + "5": 5, + "4": 4, + "3": 3, + "2": 2, + }; + + if (!suits.includes(suit) || values[rank] === undefined) { + throw new Error( + `Expected a number followed by a suit, but got "${card}"` + ); + } + + return values[rank]; } + +export { getCardValue } 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..222ac6ea6 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 @@ -12,7 +12,33 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => { }); // Case 2: Right angle +test(`should return "Right angle" when angle is 90`, () => { + expect(getAngleType(90)).toEqual("Right angle"); +}); + // Case 3: Obtuse angles +test(`should return "Obtuse angle" when (90 < angle < 180)`, () => { + expect(getAngleType(91)).toEqual("Obtuse angle"); + expect(getAngleType(120)).toEqual("Obtuse angle"); + expect(getAngleType(179)).toEqual("Obtuse angle"); +}); + // Case 4: Straight angle +test(`should return "Straight angle" when angle is 180`, () => { + expect(getAngleType(180)).toEqual("Straight angle"); +}); + // Case 5: Reflex angles +test(`should return "Reflex angle" when (180 < angle < 360)`, () => { + expect(getAngleType(181)).toEqual("Reflex angle"); + expect(getAngleType(270)).toEqual("Reflex angle"); + expect(getAngleType(359)).toEqual("Reflex angle"); +}); + // Case 6: Invalid angles +test(`should return "Invalid angle" for angles outside (0, 360)`, () => { + expect(getAngleType(0)).toEqual("Invalid angle"); + expect(getAngleType(360)).toEqual("Invalid angle"); + expect(getAngleType(-10)).toEqual("Invalid angle"); + expect(getAngleType(400)).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..ab11487c9 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 @@ -6,3 +6,20 @@ import { isProperFraction } from "../implement/2-is-proper-fraction.js"; test(`should return false when denominator is zero`, () => { expect(isProperFraction(1, 0)).toEqual(false); }); + +test(`should return true for proper fractions`, () => { + expect(isProperFraction(1, 2)).toEqual(true); + expect(isProperFraction(0, 5)).toEqual(true); + expect(isProperFraction(-1, 2)).toEqual(true); + expect(isProperFraction(1, -2)).toEqual(true); + expect(isProperFraction(-1, -2)).toEqual(true); +}); + +test(`should return false for improper fractions`, () => { + expect(isProperFraction(2, 1)).toEqual(false); + expect(isProperFraction(1, 1)).toEqual(false); + expect(isProperFraction(5, 3)).toEqual(false); + expect(isProperFraction(-5, 2)).toEqual(false); + expect(isProperFraction(5, -2)).toEqual(false); + expect(isProperFraction(-3, -2)).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..172eddcfb 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,45 @@ test(`Should return 11 when given an ace card`, () => { // please refer to the Jest documentation: // https://jestjs.io/docs/expect#tothrowerror +test(`Should return numeric value for number cards`, () => { + expect(getCardValue("2♥")).toEqual(2); + expect(getCardValue("3♠")).toEqual(3); + expect(getCardValue("4♣")).toEqual(4); + expect(getCardValue("5♥")).toEqual(5); + expect(getCardValue("6♦")).toEqual(6); + expect(getCardValue("7♠")).toEqual(7); + expect(getCardValue("8♣")).toEqual(8); + expect(getCardValue("9♠")).toEqual(9); + expect(getCardValue("10♦")).toEqual(10); +}); + +test(`Should return 10 for face cards`, () => { + expect(getCardValue("J♣")).toEqual(10); + expect(getCardValue("Q♠")).toEqual(10); + expect(getCardValue("K♥")).toEqual(10); +}); + +// Invalid cards +test(`should throw for invalid cards`, () => { + expect(() => getCardValue("invalid")).toThrow( + /Expected a number followed by a suit, but got "invalid"/ + ); + expect(() => getCardValue("A")).toThrow( + /Expected a number followed by a suit/ + ); + expect(() => getCardValue("")).toThrow( + /Expected a number followed by a suit/ + ); + expect(() => getCardValue("♠")).toThrow( + /Expected a number followed by a suit/ + ); + expect(() => getCardValue("1♠")).toThrow( + /Expected a number followed by a suit/ + ); + expect(() => getCardValue("9X")).toThrow( + /Expected a number followed by a suit/ + ); + expect(() => getCardValue("10")).toThrow( + /Expected a number followed by a suit/ + ); +});