Skip to content
Open
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,34 @@ 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 0`, () => {
expect(getAngleType(0)).toEqual("Invalid angle");
});
test(`should return "Invalid angle" when angle is 360 or greater`, () => {
expect(getAngleType(360)).toEqual("Invalid angle");
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,27 @@ 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
test(`should return false when denominator 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);
});

// Proper fractions
test("should return true when abs(numerator) < abs(denominator)", () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

You could also apply this notation to improper fractions.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for the suggestion! I updated the improper fraction test description to use the same abs notation and added a negative numerator/denominator case.

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

// Improper fractions
test("should return false when abs(numerator) >= abs(denominator)", () => {
expect(isProperFraction(2, 2)).toBe(false);
expect(isProperFraction(3, 2)).toBe(false);
expect(isProperFraction(-3, 2)).toBe(false);
});
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