Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,29 @@
// execute the code to ensure all tests pass.

function getAngleType(angle) {
// TODO: Implement this function
if (angle > 0 && angle < 90) {
return "Acute angle";
} else if (angle === 90) {
return "Right angle";
} else if (angle > 90 && angle < 180) {
return "Obtuse angle";
} else if (angle === 180) {
return "Straight angle";
} else if (angle > 180 && angle < 360) {
return "Reflex angle";
} else {
return "Invalid angle";
}
}

console.assert(getAngleType(45) === "Acute angle");
console.assert(getAngleType(90) === "Right angle");
console.assert(getAngleType(120) === "Obtuse angle");
console.assert(getAngleType(180) === "Straight angle");
console.assert(getAngleType(270) === "Reflex angle");
console.assert(getAngleType(0) === "Invalid angle");
console.assert(getAngleType(360) === "Invalid 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;
Expand All @@ -35,3 +55,27 @@ function assertEquals(actualOutput, targetOutput) {
// Example: Identify Right Angles
const right = getAngleType(90);
assertEquals(right, "Right angle");

// Example: Identify Acute Angles
const acute = getAngleType(45);
assertEquals(acute, "Acute angle");

// Example: Identify Obtuse Angles
const obtuse = getAngleType(120);
assertEquals(obtuse, "Obtuse angle");

// Example: Identify Straight Angles
const straight = getAngleType(180);
assertEquals(straight, "Straight angle");

// Example: Identify Reflex Angles
const reflex = getAngleType(270);
assertEquals(reflex, "Reflex angle");

// Example: Identify Invalid Angles
const invalid1 = getAngleType(0);
assertEquals(invalid1, "Invalid angle");

// Example: Identify Invalid Angles
const invalid2 = getAngleType(360);
assertEquals(invalid2, "Invalid angle");
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@

function isProperFraction(numerator, denominator) {
// TODO: Implement this function
if (denominator <= 0) {
return false;
}
if (numerator < denominator && numerator >= 0) {
return true;
}
return false;
}

// The line below allows us to load the isProperFraction function into tests in other files.
Expand All @@ -31,3 +38,27 @@ function assertEquals(actualOutput, targetOutput) {

// Example: 1/2 is a proper fraction
assertEquals(isProperFraction(1, 2), true);

// Example: 3/5 is a proper fraction
assertEquals(isProperFraction(3, 5), true);

// Example: 1/100 is a proper fraction
assertEquals(isProperFraction(1, 100), true);

// Example: 5/2 is not a proper fraction
assertEquals(isProperFraction(5, 2), false);

// Example: 7/7 is an improper fraction
assertEquals(isProperFraction(7, 7), false);

// Example: 8/3 is an improper fraction
assertEquals(isProperFraction(8, 3), false);

// Example: 5/0 is an invalid fraction
assertEquals(isProperFraction(5, 0), false);

// Example: 2/-6 is an invalid fraction
assertEquals(isProperFraction(2, -6), false);

// Example: -1/4 is an invalid fraction
assertEquals(isProperFraction(-1, 4), false);
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@

function getCardValue(card) {
// TODO: Implement this function
if (!["♠", "♣", "♦", "♥"].includes(card.slice(-1)))
throw new Error("Invalid card rank.");
const rank = card.slice(0, card.length - 1);
if (rank === "A") return 11;
if (["10", "J", "Q", "K"].includes(rank)) return 10;
if (["2", "3", "4", "5", "6", "7", "8", "9"].includes(rank))
return Number(rank);
throw new Error("Invalid card rank.");
}

// The line below allows us to load the getCardValue function into tests in other files.
Expand All @@ -41,6 +49,12 @@ function assertEquals(actualOutput, targetOutput) {
// Examples:
assertEquals(getCardValue("9♠"), 9);

assertEquals(getCardValue("6♥"), 6);

assertEquals(getCardValue("J♣"), 10);

assertEquals(getCardValue("A♦"), 11);

// Handling invalid cards
try {
getCardValue("invalid");
Expand All @@ -50,3 +64,33 @@ try {
} catch (e) {}

// What other invalid card cases can you think of?

try {
getCardValue("9K");

console.log("Error was not thrown for invalid card");
} catch (error) {}

try {
getCardValue("");

console.log("Error was not thrown for invalid card");
} catch (error) {}

try {
getCardValue("ABC");

console.log("Error was not thrown for invalid card");
} catch (error) {}

try {
getCardValue("A");

console.log("Error was not thrown for invalid card");
} catch (error) {}

try {
getCardValue("JK");

console.log("Error was not thrown for invalid card");
} catch (error) {}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,32 @@ 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 identify obtuse angle (90° < angle < 180°)", () => {
expect(getAngleType(91)).toEqual("Obtuse angle");
expect(getAngleType(130)).toEqual("Obtuse angle");
expect(getAngleType(179)).toEqual("Obtuse angle");
});

// Case 4: Straight angle
test("should identify straight angle (angle = 180°)", () => {
expect(getAngleType(180)).toEqual("Straight angle");
});

// Case 5: Reflex angles
test("should identify reflex angle (180° < angle < 360)", () => {
expect(getAngleType(181)).toEqual("Reflex angle");
expect(getAngleType(280)).toEqual("Reflex angle");
expect(getAngleType(359)).toEqual("Reflex angle");
});

// Case 6: Invalid angles
test("should identify invalid angle (angle <= 0 or angle >= 360)", () => {
expect(getAngleType(0)).toEqual("Invalid angle");
expect(getAngleType(360)).toEqual("Invalid angle");
expect(getAngleType(361)).toEqual("Invalid angle");
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,69 @@ 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
// Special case: denominator is zero
test(`should return false when denominator is zero`, () => {
expect(isProperFraction(1, 0)).toEqual(false);
});

test("should return true when numerator is zero and denominator is non-zero", () => {
expect(isProperFraction(0, 5)).toEqual(true);
});

test("should return false when both numerator and denominator are zero", () => {
expect(isProperFraction(0, 0)).toEqual(false);
});

// Special case: Identify proper Fractions:
test("should return true for proper fraction", () => {
expect(isProperFraction(2, 3)).toEqual(true);
});

// Special case: Identify Negative Fractions:
test("should return true for proper negative fraction", () => {
expect(isProperFraction(-3, 6)).toEqual(true);
});

test("should return false for improper negative fraction", () => {
expect(isProperFraction(-5, 2)).toEqual(false);
});

test("should return true for proper fraction with negative denominator", () => {
expect(isProperFraction(2, -5)).toEqual(true);
});

test("should return false for improper fraction with negative denominator", () => {
expect(isProperFraction(7, -3)).toEqual(false);
});

// Special case: Identify Equal Numerator and Denominator:
test("should return false for improper fraction (numerator === denominator)", () => {
expect(isProperFraction(3, 3)).toEqual(false);
});

// Special case: Identify both Numerator and Denominator as negative
test("should return true when both numerator and denominator are negative and proper", () => {
expect(isProperFraction(-2, -5)).toEqual(true);
});

test("should return false when both numerator and denominator are negative and improper", () => {
expect(isProperFraction(-6, -3)).toEqual(false);
});

// Special case: Identify both Numerator and Denominator as decimal
test("should return true for proper decimal fractions", () => {
expect(isProperFraction(1.5, 2.5)).toEqual(true);
});

test("should return false for improper decimal fractions", () => {
expect(isProperFraction(2.5, 1.5)).toEqual(false);
});

// Special case: Identify both Numerator and Denominator as large numbers
test("should return true for large proper fractions", () => {
expect(isProperFraction(100, 1000)).toEqual(true);
});

test("should return false for large improper fractions", () => {
expect(isProperFraction(1000, 100)).toEqual(false);
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,74 @@ 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);
expect(getCardValue("A♥")).toEqual(11);
});

// Case 2: Handle Number Cards (2-10):
test("should return the appropriate number from 2 to 10", () => {
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);
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);
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);
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);
});

// Case 3: Handle Face Cards (J, Q, K):
test("should return 10 for face cards", () => {
expect(getCardValue("J♠")).toEqual(10);
expect(getCardValue("Q♠")).toEqual(10);
expect(getCardValue("K♠")).toEqual(10);
expect(getCardValue("J♦")).toEqual(10);
expect(getCardValue("Q♦")).toEqual(10);
expect(getCardValue("K♦")).toEqual(10);
expect(getCardValue("J♣")).toEqual(10);
expect(getCardValue("Q♣")).toEqual(10);
expect(getCardValue("K♣")).toEqual(10);
expect(getCardValue("J♥")).toEqual(10);
expect(getCardValue("Q♥")).toEqual(10);
expect(getCardValue("K♥")).toEqual(10);
});

// Case 4: Handle Invalid Cards:
test("Should return 'Invalid card rank.' for invalid cards", () => {
expect(() => getCardValue("KJ")).toThrow("Invalid card rank.");
expect(() => getCardValue("AK")).toThrow("Invalid card rank.");
expect(() => getCardValue(" ")).toThrow("Invalid card rank.");
expect(() => getCardValue("S♠")).toThrow("Invalid card rank.");
expect(() => getCardValue("J♠♠")).toThrow("Invalid card rank.");
});

// Suggestion: Group the remaining test data into these categories:
Expand All @@ -17,4 +85,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