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 @@ -16,6 +16,27 @@

function getAngleType(angle) {
// TODO: Implement this function
if (angle <= 0 || angle >= 360) {
return "Invalid angle";
}

if (angle < 90) {
return "Acute angle";
}

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

if (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 @@ -35,3 +56,12 @@ function assertEquals(actualOutput, targetOutput) {
// Example: Identify Right Angles
const right = getAngleType(90);
assertEquals(right, "Right angle");
assertEquals(getAngleType(45), "Acute angle");
assertEquals(getAngleType(90), "Right angle");
assertEquals(getAngleType(120), "Obtuse angle");
assertEquals(getAngleType(180), "Straight angle");
assertEquals(getAngleType(200), "Reflex angle");

assertEquals(getAngleType(0), "Invalid angle");
assertEquals(getAngleType(-10), "Invalid angle");
assertEquals(getAngleType(360), "Invalid angle");
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@

function isProperFraction(numerator, denominator) {
// TODO: Implement this function
if (denominator === 0) {
return false;
}

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 +36,9 @@ function assertEquals(actualOutput, targetOutput) {

// Example: 1/2 is a proper fraction
assertEquals(isProperFraction(1, 2), true);
assertEquals(isProperFraction(1, 2), true);
assertEquals(isProperFraction(3, 4), true);
assertEquals(isProperFraction(5, 10), true);
assertEquals(isProperFraction(5, 5), false);
assertEquals(isProperFraction(10, 5), false);
assertEquals(isProperFraction(7, 3), false);
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,27 @@

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

if (
rank !== "A" &&
rank !== "K" &&
rank !== "Q" &&
rank !== "J" &&
isNaN(rank)
) {
throw new Error("Invalid card");
}

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

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

return Number(rank);
}

// The line below allows us to load the getCardValue function into tests in other files.
Expand All @@ -40,6 +61,13 @@ 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("K♥"), 10);
assertEquals(getCardValue("Q♦"), 10);
assertEquals(getCardValue("J♣"), 10);
assertEquals(getCardValue("10♠"), 10);
assertEquals(getCardValue("2♥"), 2);
assertEquals(getCardValue("9♣"), 9);

// Handling invalid cards
try {
Expand All @@ -52,3 +80,30 @@ try {
}

// What other invalid card cases can you think of?
try {
getCardValue("1♠");
console.error("Should throw error");
} catch (e) {
console.log("Error thrown correctly");
}

try {
getCardValue("11♠");
console.error("Should throw error");
} catch (e) {
console.log("Error thrown correctly");
}

try {
getCardValue("A");
console.error("Should throw error");
} catch (e) {
console.log("Error thrown correctly");
}

try {
getCardValue("♠A");
console.error("Should throw error");
} catch (e) {
console.log("Error thrown correctly");
}
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 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 angles`, () => {
expect(getAngleType(0)).toEqual("Invalid angle");
expect(getAngleType(-10)).toEqual("Invalid angle");
expect(getAngleType(360)).toEqual("Invalid angle");
expect(getAngleType(400)).toEqual("Invalid angle");
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,43 @@ const isProperFraction = require("../implement/2-is-proper-fraction");
test(`should return false when denominator is zero`, () => {
expect(isProperFraction(1, 0)).toEqual(false);
});

// Positive proper fraction
test("should return true for a proper positive fraction", () => {
expect(isProperFraction(1, 2)).toEqual(true);
});

// Positive improper fraction
test("should return false for an improper positive fraction", () => {
expect(isProperFraction(5, 2)).toEqual(false);
});

// Equal numerator and denominator
test("should return false when numerator equals denominator", () => {
expect(isProperFraction(4, 4)).toEqual(false);
});

// Zero numerator
test("should return true when numerator is zero", () => {
expect(isProperFraction(0, 5)).toEqual(true);
});

// Zero denominator
test("should return false when denominator is zero", () => {
expect(isProperFraction(1, 0)).toEqual(false);
});

// Both numerator and denominator are negative
test("should return true for a proper fraction with both values negative", () => {
expect(isProperFraction(-1, -2)).toEqual(true);
});

// Negative numerator, positive denominator
test("should return true when numerator is negative and denominator is positive", () => {
expect(isProperFraction(-1, 2)).toEqual(true);
});

// Positive numerator, negative denominator
test("should return false when numerator is positive and denominator is negative", () => {
expect(isProperFraction(1, -2)).toEqual(false);
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,27 @@ test(`Should return 11 when given an ace card`, () => {
expect(getCardValue("A♠")).toEqual(11);
});

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

// Case 3: 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: Invalid cards
test("should throw an error for invalid cards", () => {
expect(() => getCardValue("1♠")).toThrow();
expect(() => getCardValue("11♣")).toThrow();
expect(() => getCardValue("Z♥")).toThrow();
expect(() => getCardValue("invalid")).toThrow();
});
// Suggestion: Group the remaining test data into these categories:
// Number Cards (2-10)
// Face Cards (J, Q, K)
Expand All @@ -17,4 +38,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

Loading