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 9e05a871e..b7c482b55 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 @@ -1,37 +1,27 @@ -// Implement a function getAngleType -// -// 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. +function getAngleType(angle) { + if (angle <= 0 || angle >= 360) { + return "Invalid angle"; + } -// Assumption: The parameter is a valid number. (You do not need to handle non-numeric inputs.) + if (angle > 0 && angle < 90) { + return "Acute angle"; + } -// Acceptance criteria: -// After you have implemented the function, write tests to cover all the cases, and -// execute the code to ensure all tests pass. + if (angle === 90) { + return "Right angle"; + } -function getAngleType(angle) { - // TODO: Implement this function -} + if (angle > 90 && angle < 180) { + return "Obtuse angle"; + } -// The line below allows us to load the getAngleType function into tests in other files. -// This will be useful in the "rewrite tests with jest" step. -module.exports = getAngleType; + if (angle === 180) { + return "Straight angle"; + } -// This helper function is written to make our assertions easier to read. -// If the actual output matches the target output, the test will pass -function assertEquals(actualOutput, targetOutput) { - console.assert( - actualOutput === targetOutput, - `Expected ${actualOutput} to equal ${targetOutput}` - ); + if (angle > 180 && angle < 360) { + return "Reflex angle"; + } } -// TODO: Write tests to cover all cases, including boundary and invalid cases. -// Example: Identify Right Angles -const right = getAngleType(90); -assertEquals(right, "Right angle"); +module.exports = getAngleType; \ 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 970cb9b64..a8a51ff7a 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 @@ -1,33 +1,9 @@ -// Implement a function isProperFraction, -// 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. - function isProperFraction(numerator, denominator) { - // TODO: Implement this function -} - -// The line below allows us to load the isProperFraction function into tests in other files. -// This will be useful in the "rewrite tests with jest" step. -module.exports = isProperFraction; + if (denominator === 0) { + return false; + } -// Here's our helper again -function assertEquals(actualOutput, targetOutput) { - console.assert( - actualOutput === targetOutput, - `Expected ${actualOutput} to equal ${targetOutput}` - ); + return Math.abs(numerator) < Math.abs(denominator); } -// 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); +module.exports = isProperFraction; \ No newline at end of file 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 ff5c532e1..058ba613b 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 @@ -1,54 +1,31 @@ -// This problem involves playing cards: https://en.wikipedia.org/wiki/Standard_52-card_deck - -// Implement a function getCardValue, when given a string representing a playing card, -// should return the numerical value of the card. +function getCardValue(card) { + const suits = ["♠", "♥", "♦", "♣"]; -// 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♦". + if (typeof card !== "string" || card.length < 2) { + throw new Error("Invalid card"); + } -// 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 suit = card.slice(-1); + const rank = card.slice(0, -1); -// When the card string is invalid (not following the above format), the function should -// throw an error. + if (!suits.includes(suit)) { + throw new Error("Invalid card"); + } -// Acceptance criteria: -// After you have implemented the function, write tests to cover all the cases, and -// execute the code to ensure all tests pass. + // Ace + if (rank === "A") return 11; -function getCardValue(card) { - // TODO: Implement this function -} - -// The line below allows us to load the getCardValue function into tests in other files. -// This will be useful in the "rewrite tests with jest" step. -module.exports = getCardValue; - -// Helper functions to make our assertions easier to read. -function assertEquals(actualOutput, targetOutput) { - console.assert( - actualOutput === targetOutput, - `Expected ${actualOutput} to equal ${targetOutput}` - ); -} + // Face cards + if (["J", "Q", "K"].includes(rank)) return 10; -// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards. -// Examples: -assertEquals(getCardValue("9♠"), 9); + // Number cards + const validNumbers = ["2","3","4","5","6","7","8","9","10"]; -// Handling invalid cards -try { - getCardValue("invalid"); + if (validNumbers.includes(rank)) { + return Number(rank); + } - // This line will not be reached if an error is thrown as expected - console.error("Error was not thrown for invalid card 😢"); -} catch (e) { - console.log("Error thrown for invalid card 🎉"); + throw new Error("Invalid card"); } -// What other invalid card cases can you think of? +module.exports = getCardValue; \ No newline at end of file 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 d777f348d..97b1c9c1c 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 @@ -1,20 +1,33 @@ -// This statement loads the getAngleType function you wrote in the implement directory. -// We will use the same function, but write tests for it using Jest in this file. const getAngleType = require("../implement/1-get-angle-type"); -// TODO: Write tests in Jest syntax to cover all cases/outcomes, -// including boundary and invalid cases. +describe("getAngleType", () => { + test("returns Acute angle", () => { + expect(getAngleType(1)).toBe("Acute angle"); + expect(getAngleType(45)).toBe("Acute angle"); + expect(getAngleType(89)).toBe("Acute angle"); + }); -// 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("returns Right angle", () => { + expect(getAngleType(90)).toBe("Right angle"); + }); -// Case 2: Right angle -// Case 3: Obtuse angles -// Case 4: Straight angle -// Case 5: Reflex angles -// Case 6: Invalid angles + test("returns Obtuse angle", () => { + expect(getAngleType(100)).toBe("Obtuse angle"); + expect(getAngleType(179)).toBe("Obtuse angle"); + }); + + test("returns Straight angle", () => { + expect(getAngleType(180)).toBe("Straight angle"); + }); + + test("returns Reflex angle", () => { + expect(getAngleType(200)).toBe("Reflex angle"); + expect(getAngleType(359)).toBe("Reflex angle"); + }); + + test("returns Invalid angle", () => { + expect(getAngleType(0)).toBe("Invalid angle"); + expect(getAngleType(360)).toBe("Invalid angle"); + expect(getAngleType(-10)).toBe("Invalid angle"); + }); +}); \ No newline at end of file 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 7f087b2ba..6748db66a 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 @@ -1,10 +1,30 @@ -// This statement loads the isProperFraction function you wrote in the implement directory. -// We will use the same function, but write tests for it using Jest in this file. 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. +describe("isProperFraction", () => { + test("returns true when numerator is smaller than denominator", () => { + expect(isProperFraction(1, 2)).toBe(true); + expect(isProperFraction(3, 4)).toBe(true); + }); -// Special case: numerator is zero -test(`should return false when denominator is zero`, () => { - expect(isProperFraction(1, 0)).toEqual(false); -}); + test("returns false when numerator equals denominator", () => { + expect(isProperFraction(5, 5)).toBe(false); + }); + + test("returns false when numerator is larger than denominator", () => { + expect(isProperFraction(7, 3)).toBe(false); + }); + + test("returns true when numerator is zero", () => { + expect(isProperFraction(0, 5)).toBe(true); + }); + + test("returns false when denominator is zero", () => { + expect(isProperFraction(1, 0)).toBe(false); + }); + + test("works with negative numbers", () => { + expect(isProperFraction(-1, 2)).toBe(true); + expect(isProperFraction(1, -2)).toBe(true); + expect(isProperFraction(-3, -2)).toBe(false); + }); +}); \ No newline at end of file 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 cf7f9dae2..b5760ed64 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 @@ -1,20 +1,44 @@ -// This statement loads the getCardValue function you wrote in the implement directory. -// We will use the same function, but write tests for it using Jest in this file. const getCardValue = require("../implement/3-get-card-value"); -// TODO: Write tests in Jest syntax to cover all possible outcomes. +describe("getCardValue", () => { + // Ace + test("returns 11 for Ace", () => { + expect(getCardValue("A♠")).toBe(11); + }); -// Case 1: Ace (A) -test(`Should return 11 when given an ace card`, () => { - expect(getCardValue("A♠")).toEqual(11); -}); + // Face cards + test("returns 10 for face cards", () => { + expect(getCardValue("J♥")).toBe(10); + expect(getCardValue("Q♦")).toBe(10); + expect(getCardValue("K♣")).toBe(10); + }); -// Suggestion: Group the remaining test data into these categories: -// Number Cards (2-10) -// Face Cards (J, Q, K) -// Invalid Cards + // Number cards + test("returns correct value for number cards", () => { + expect(getCardValue("2♠")).toBe(2); + expect(getCardValue("10♥")).toBe(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 + // Invalid format + test("throws error for invalid strings", () => { + expect(() => getCardValue("invalid")).toThrow(); + expect(() => getCardValue("A")).toThrow(); + }); + // Invalid numbers (REVIEWER CHECK 🔥) + test("throws error for invalid numeric formats", () => { + expect(() => getCardValue("0x02♠")).toThrow(); + expect(() => getCardValue("2.1♠")).toThrow(); + expect(() => getCardValue("0002♠")).toThrow(); + }); + + // Invalid rank + test("throws error for invalid rank", () => { + expect(() => getCardValue("11♠")).toThrow(); + }); + + // Invalid suit + test("throws error for invalid suit", () => { + expect(() => getCardValue("A?")).toThrow(); + }); +}); \ No newline at end of file