Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@

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";
}
}

// The line below allows us to load the getAngleType function into tests in other files.
Expand All @@ -33,5 +46,13 @@ function assertEquals(actualOutput, targetOutput) {

// TODO: Write tests to cover all cases, including boundary and invalid cases.
// Example: Identify Right Angles
const right = getAngleType(90);
assertEquals(right, "Right angle");

assertEquals(getAngleType(90), "Right angle");
assertEquals(getAngleType(45), "Acute angle");
assertEquals(getAngleType(135), "Obtuse angle");
assertEquals(getAngleType(180), "Straight angle");
assertEquals(getAngleType(270), "Reflex angle");
assertEquals(getAngleType(-10), "Invalid angle");
assertEquals(getAngleType(360), "Invalid angle");
assertEquals(getAngleType(359.999), "Reflex angle");
assertEquals(getAngleType("90"), "Invalid angle");
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// execute the code to ensure all tests pass.

function isProperFraction(numerator, denominator) {
// TODO: Implement this function
return Math.abs(numerator) < Math.abs(denominator);
}

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

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

// Basic non-proper examples
assertEquals(isProperFraction(2, 1), false);

// Negative numerators or denominators should still use absolute values
assertEquals(isProperFraction(-1, 2), true);
assertEquals(isProperFraction(1, -2), true);
assertEquals(isProperFraction(-1, -2), true);

// Zero numerator is a proper fraction when denominator != 0
assertEquals(isProperFraction(0, 5), true);

// Equal magnitude (including signs) is not proper
assertEquals(isProperFraction(5, 5), false);
assertEquals(isProperFraction(-5, 5), false);

// Denominator zero (no division here, but should be considered invalid/proper=false)
assertEquals(isProperFraction(0, 0), false);
assertEquals(isProperFraction(3, 0), false);

// Larger values and decimals
assertEquals(isProperFraction(100, 101), true);
assertEquals(isProperFraction(101, 100), false);
assertEquals(isProperFraction(0.5, 1), true);
assertEquals(isProperFraction(0.9999, 1), true);
assertEquals(isProperFraction(1, 1.0001), true);
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,25 @@
// After you have implemented the function, write tests to cover all the cases, and
// execute the code to ensure all tests pass.

const validSuits = ["♠", "♣", "♥", "♦"];

function getCardValue(card) {
// TODO: Implement this function
if (typeof card !== "string" || card.length < 2) {
throw new Error("Invalid card");
}

const suit = card.slice(-1);
if (!validSuits.includes(suit)) {
throw new Error("Invalid card");
}

const rank = card.slice(0, -1);
if (rank === "A") return 11;
if (rank === "J" || rank === "Q" || rank === "K") return 10;

const num = Number(rank);
if (Number.isInteger(num) && num >= 2 && num <= 10) return num;
Comment thread
cjyuan marked this conversation as resolved.
Outdated
throw new Error("Invalid card");
}

// The line below allows us to load the getCardValue function into tests in other files.
Expand All @@ -39,8 +56,14 @@ function assertEquals(actualOutput, targetOutput) {

// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
// Examples:
assertEquals(getCardValue("2♠"), 2);
assertEquals(getCardValue("3♣"), 3);
assertEquals(getCardValue("9♠"), 9);

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");
Expand All @@ -52,3 +75,6 @@ try {
}

// What other invalid card cases can you think of?
// lowercase rank
// invalid suit
// non-string input
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,33 @@ 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(91)).toEqual("Obtuse angle");
expect(getAngleType(135)).toEqual("Obtuse angle");
expect(getAngleType(179)).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(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" when invalid`, () => {
expect(getAngleType(-1)).toEqual("Invalid angle");
expect(getAngleType(0)).toEqual("Invalid angle");
expect(getAngleType("90")).toEqual("Invalid angle");
expect(getAngleType(1000)).toEqual("Invalid angle");
expect(getAngleType(9999.999)).toEqual("Invalid angle");
});
Comment thread
cjyuan marked this conversation as resolved.
Outdated
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,36 @@ 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);
expect(isProperFraction(-1, 0)).toEqual(false);
});

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

test(`should return true when absolute value of numerator is less than absolute value of denominator`, () => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: Using pseudocode and notations like abs(...) or | ... | is a common and widely accepted practice for describing conditions in a concise manner.

expect(isProperFraction(1, 2)).toEqual(true);
expect(isProperFraction(-1, 2)).toEqual(true);
expect(isProperFraction(1, -2)).toEqual(true);
expect(isProperFraction(-1, -2)).toEqual(true);
});

test(`should return false when absolute value of numerator is greater than or equal to absolute value of denominator`, () => {
expect(isProperFraction(1, 1)).toEqual(false);
expect(isProperFraction(2, 1)).toEqual(false);
expect(isProperFraction(-1, 1)).toEqual(false);
expect(isProperFraction(1, -1)).toEqual(false);
expect(isProperFraction(-1, -1)).toEqual(false);
});

test(`floating point values should be compared using absolute values`, () => {
expect(isProperFraction(0.5, 1)).toEqual(true);
expect(isProperFraction(1, 0.5)).toEqual(false);
expect(isProperFraction(-0.5, 1)).toEqual(true);
expect(isProperFraction(0.5, -1)).toEqual(true);
expect(isProperFraction(-0.5, -1)).toEqual(true);
});
Comment thread
cjyuan marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,34 @@ test(`Should return 11 when given an ace card`, () => {

// Suggestion: Group the remaining test data into these categories:
// Number Cards (2-10)
test(`Should return the numeric value for number cards`, () => {
Comment thread
cjyuan marked this conversation as resolved.
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);
});

// Face Cards (J, Q, K)
test(`Should return 10 for face cards (J, Q, K)`, () => {
expect(getCardValue("J♠")).toEqual(10);
expect(getCardValue("Q♠")).toEqual(10);
expect(getCardValue("K♠")).toEqual(10);
});

// Invalid Cards
test(`Should throw an error for invalid cards`, () => {
expect(() => getCardValue("1♠")).toThrow("Invalid card");
expect(() => getCardValue("A")).toThrow("Invalid card");
expect(() => getCardValue("J")).toThrow("Invalid card");
expect(() => getCardValue("♠")).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:
// https://jestjs.io/docs/expect#tothrowerror

Loading