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,13 @@
// execute the code to ensure all tests pass.

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

// The line below allows us to load the getAngleType function into tests in other files.
Expand All @@ -33,5 +39,24 @@ 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");

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

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

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

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

const invalid1 = getAngleType(400);
assertEquals(invalid1, "Invalid angle");

const invalid2 = getAngleType("400");
assertEquals(invalid2, "Invalid angle");
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
// execute the code to ensure all tests pass.

function isProperFraction(numerator, denominator) {
// TODO: Implement this function
numerator = Math.abs(numerator);
denominator = Math.abs(denominator);
if (numerator < denominator) return true;
else if (numerator > denominator) return false;
else if (numerator === denominator) return false;
}

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

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

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

// Example: -4/7 is a proper fraction
const negativeFraction = isProperFraction(-4, 7);
assertEquals(negativeFraction, true);

// Example: 3/3 is not a proper fraction
const equalFraction = isProperFraction(3, 3);
assertEquals(equalFraction, false);

// Example: 4/-7 is a proper fraction
const negativeFraction1 = isProperFraction(4, -7);
assertEquals(negativeFraction1, true);

// Example: -4/-7 is a proper fraction
const negativeFraction2 = isProperFraction(4, 7);
assertEquals(negativeFraction2, true);
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,14 @@
// execute the code to ensure all tests pass.

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 +48,15 @@ function assertEquals(actualOutput, targetOutput) {
// Examples:
assertEquals(getCardValue("9♠"), 9);

const fiveOfHearts = getCardValue("5♥");
assertEquals(fiveOfHearts, 5);

const faceCard = getCardValue("J♣");
assertEquals(faceCard, 10);

const aceCard = getCardValue("A♦");
assertEquals(aceCard, 11);

// Handling invalid cards
try {
getCardValue("invalid");
Expand All @@ -50,3 +66,28 @@ 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,31 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => {
});

// Case 2: Right angle
test(`should return "Acute 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 (0 > angle > 360)", () => {
expect(getAngleType(0)).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);
});

// Case 2: Identify Improper Fractions:
test("should return false for improper fraction", () => {
expect(isProperFraction(2, 3)).toEqual(true);
});

// Case 3: 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);
});

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

// Case 5: 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);
});

// Case 6: 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);
});

// Case 7: 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,34 @@ 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);
});
// 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);
});

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

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