Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,27 @@
// execute the code to ensure all tests pass.

function getAngleType(angle) {
// TODO: Implement this function
if (angle > 0 && angle < 90) {
return "Acute angle";
}

if (angle === 90) {
return "Right angle";
}

if (angle > 90 && angle < 180) {
return "Obtuse angle";
}

if (angle === 180) {
return "Straight angle";
}

if (angle > 180 && angle < 360) {
return "Reflex angle";
}

return "Invalid angle";
}

// The line below allows us to load the getAngleType function into tests in other files.
Expand All @@ -31,7 +51,22 @@ 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");
// Acute angle
assertEquals(getAngleType(45), "Acute angle");

// Right angle
assertEquals(getAngleType(90), "Right angle");

// Obtuse angle
assertEquals(getAngleType(120), "Obtuse angle");

// Straight angle
assertEquals(getAngleType(180), "Straight angle");

// Reflex angle
assertEquals(getAngleType(270), "Reflex angle");

// Invalid angles
assertEquals(getAngleType(0), "Invalid angle");
assertEquals(getAngleType(360), "Invalid angle");
assertEquals(getAngleType(-10), "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 numerator >= 0 && denominator > 0 && numerator < denominator;
}

// The line below allows us to load the isProperFraction function into tests in other files.
Expand All @@ -29,5 +29,12 @@ function assertEquals(actualOutput, targetOutput) {
// TODO: Write tests to cover all cases.
// What combinations of numerators and denominators should you test?

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

// Not proper fractions
assertEquals(isProperFraction(2, 2), false);
assertEquals(isProperFraction(5, 2), false);
assertEquals(isProperFraction(-1, 2), false);
assertEquals(isProperFraction(1, 0), false);
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,26 @@
// execute the code to ensure all tests pass.

function getCardValue(card) {
// TODO: Implement this function
const rank = card.slice(0, -1);
const suit = card.slice(-1);

if (!["♠", "♥", "♦", "♣"].includes(suit)) {
throw new Error("Invalid card");
}

if (rank === "A") {
return 11;
}

if (rank === "J" || rank === "Q" || rank === "K") {
return 10;
}

if (["2", "3", "4", "5", "6", "7", "8", "9", "10"].includes(rank)) {
return Number(rank);
}

throw new Error("Invalid card");
}

// The line below allows us to load the getCardValue function into tests in other files.
Expand All @@ -37,18 +56,36 @@ function assertEquals(actualOutput, targetOutput) {
);
}

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

// Ace
assertEquals(getCardValue("A♣"), 11);

// Handling invalid cards
// Face cards
assertEquals(getCardValue("J♦"), 10);
assertEquals(getCardValue("Q♥"), 10);
assertEquals(getCardValue("K♠"), 10);

// Invalid cards
try {
getCardValue("invalid");

// 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 🎉");
}

// What other invalid card cases can you think of?
try {
getCardValue("1♠");
console.error("Error was not thrown for invalid rank 😢");
} catch (e) {
console.log("Error thrown for invalid rank 🎉");
}

try {
getCardValue("AX");
console.error("Error was not thrown for invalid suit 😢");
} catch (e) {
console.log("Error thrown for invalid suit 🎉");
}
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 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 invalid values", () => {
expect(getAngleType(0)).toEqual("Invalid angle");
expect(getAngleType(-10)).toEqual("Invalid angle");
expect(getAngleType(360)).toEqual("Invalid angle");
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,23 @@ test(`Should return 11 when given an ace card`, () => {
expect(getCardValue("A♠")).toEqual(11);
});

// Suggestion: Group the remaining test data into these categories:
// Number Cards (2-10)
// Face Cards (J, Q, K)
// Invalid Cards
// Number Cards
test("Should return the value of number cards", () => {
expect(getCardValue("2♥")).toEqual(2);
expect(getCardValue("9♣")).toEqual(9);
expect(getCardValue("10♦")).toEqual(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
// Face Cards
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("invalid")).toThrow();
expect(() => getCardValue("AX")).toThrow();
});
Loading