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

function getAngleType(angle) {
// TODO: Implement this function
if (angle <= 0 || angle >= 360) {
return "Invalid angle";
}
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.
// This will be useful in the "rewrite tests with jest" step.
module.exports = getAngleType;

// This helper function is written to make our assertions easier to read.
// If the actual output matches the target output, the test will pass
// Helper function for testing
function assertEquals(actualOutput, targetOutput) {
console.assert(
actualOutput === targetOutput,
`Expected ${actualOutput} to equal ${targetOutput}`
);
}

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

const acute = getAngleType(45);
assertEquals(acute, "Acute angle");

const obtuse = getAngleType(135);
assertEquals(obtuse, "Obtuse angle");

const straight = getAngleType(180);
assertEquals(straight, "Straight angle");

const reflex = getAngleType(270);
assertEquals(reflex, "Reflex angle");

const invalid = getAngleType(-45);
assertEquals(invalid, "Invalid angle");
assertEquals(getAngleType(0), "Invalid angle");
assertEquals(getAngleType(89), "Acute angle");
assertEquals(getAngleType(91), "Obtuse angle");
assertEquals(getAngleType(179), "Obtuse angle");
assertEquals(getAngleType(181), "Reflex angle");
assertEquals(getAngleType(359), "Reflex angle");
assertEquals(getAngleType(360), "Invalid angle");
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
// execute the code to ensure all tests pass.

function isProperFraction(numerator, denominator) {
// TODO: Implement this function
if (denominator === 0) {
return false; // A fraction with a zero denominator is undefined, so we return false.
}
return Math.abs(numerator) < Math.abs(denominator);
}

// 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;
Expand All @@ -31,3 +33,9 @@ function assertEquals(actualOutput, targetOutput) {

// Example: 1/2 is a proper fraction
assertEquals(isProperFraction(1, 2), true);
assertEquals(isProperFraction(2, 1), false);
assertEquals(isProperFraction(-1, 2), true);
assertEquals(isProperFraction(1, -2), true);
assertEquals(isProperFraction(-1, -2), true);
assertEquals(isProperFraction(0, 2), true);
assertEquals(isProperFraction(2, 0), false);
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,49 @@
// execute the code to ensure all tests pass.

function getCardValue(card) {
// TODO: Implement this function
const validRanks = [
"A",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"J",
"Q",
"K",
];
const validSuits = ["♠", "♥", "♦", "♣"];

if (typeof card !== "string") {
throw new Error("Invalid card");
}

if (card.length < 2) {
throw new Error("Invalid card");
}

const rank = card.substring(0, card.length - 1);
const suit = card.substring(card.length - 1);

if (!validRanks.includes(rank)) {
throw new Error("Invalid card");
}

if (!validSuits.includes(suit)) {
throw new Error("Invalid card");
}

if (rank === "A") {
return 11;
} else if (rank === "J" || rank === "Q" || rank === "K") {
return 10;
} else {
return parseInt(rank);
}
}

// The line below allows us to load the getCardValue function into tests in other files.
Expand All @@ -40,7 +82,10 @@ function assertEquals(actualOutput, targetOutput) {
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
// Examples:
assertEquals(getCardValue("9♠"), 9);

assertEquals(getCardValue("A♥"), 11);
assertEquals(getCardValue("J♦"), 10);
assertEquals(getCardValue("Q♣"), 10);
assertEquals(getCardValue("K♦"), 10);
// Handling invalid cards
try {
getCardValue("invalid");
Expand All @@ -50,3 +95,22 @@ try {
} catch (e) {}

// What other invalid card cases can you think of?
assertEquals(getCardValue("A♣"), 11);
assertEquals(getCardValue("J♥"), 10);
assertEquals(getCardValue("Q♦"), 10);
assertEquals(getCardValue("K♠"), 10);

try {
getCardValue("A");
console.error("Error was not thrown for invalid card");
} catch (e) {}

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

try {
getCardValue("2♠♣");
console.error("Error was not thrown for invalid card");
} catch (e) {}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
// 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.

// Case 1: Acute angles
test(`should return "Acute angle" when (0 < angle < 90)`, () => {
// Test various acute angles, including boundary cases
Expand All @@ -14,7 +11,31 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => {
});

// Case 2: Right angle
test(`should return "Right angle" when angle is exactly 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(179)).toEqual("Obtuse angle");
});

// Case 4: Straight angle
test(`should return "Straight angle" when angle is exactly 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(359)).toEqual("Reflex angle");
});

// Case 6: Invalid angles
test(`should return "Invalid angle" when angle is negative`, () => {
expect(getAngleType(-1)).toEqual("Invalid angle");
});
test(`should return "Invalid angle" when angle is 360 or greater`, () => {
expect(getAngleType(360)).toEqual("Invalid angle");
});
Comment thread
cjyuan marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,31 @@ 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);
});

// Case: numerator is zero
test(`should return true when numerator is zero and denominator is positive`, () => {
expect(isProperFraction(0, 1)).toEqual(true);
});
test("should return true when numerator is smaller than denominator and both are positive", () => {
expect(isProperFraction(1, 2)).toEqual(true);
});

test("should return false when numerator is equal to denominator", () => {
expect(isProperFraction(2, 2)).toEqual(false);
});

test("should return false when numerator is greater than denominator", () => {
expect(isProperFraction(3, 2)).toEqual(false);
});

test("should return false when numerator is negative", () => {
expect(isProperFraction(-1, 2)).toEqual(false);
});

test("should return true when both numerator and denominator are negative and numerator is smaller than denominator", () => {
expect(isProperFraction(-1, -2)).toEqual(true);
});

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.

The test description on line 28 is not quite accurate.

For proper fractions, the condition is, "the absolute value of denominator is larger than the absolute value of the numerator". So we can create a category for proper fractions and describe it as:
should return true when abs(numerator) < abs(denominator)

and then test all combinations of positive/negative numerator and denominator (for proper fraction) in this category.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thank you for the feedback. I’ve updated the tests to follow the rule that a proper fraction is when abs(numerator) < abs(denominator). I grouped the proper fraction cases together and included the different positive and negative combinations. Please let me know if there is anything else I should improve.

Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,23 @@ 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 correct value for number cards", () => {
expect(getCardValue("2♠")).toEqual(2);
expect(getCardValue("5♥")).toEqual(5);
expect(getCardValue("10♦")).toEqual(10);
});
// 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);
});
// Invalid Cards
test(`Should throw an error for invalid cards`, () => {
expect(() => getCardValue("1♠")).toThrow();
expect(() => getCardValue("Z♣")).toThrow();
});

// 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