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,19 @@

function getAngleType(angle) {
// TODO: Implement this function
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.
Expand All @@ -35,3 +48,23 @@ function assertEquals(actualOutput, targetOutput) {
// Example: Identify Right Angles
const right = getAngleType(90);
assertEquals(right, "Right angle");

// Example: Identify Acute Angles
const acute = getAngleType(45);
assertEquals(acute, "Acute angle");

// Example: Identify Obtuse Angles
const obtuse = getAngleType(135);
assertEquals(obtuse, "Obtuse angle");

// Example: Identify Straight Angles
const straight = getAngleType(180);
assertEquals(straight, "Straight angle");

// Example: Identify Reflex Angles
const reflex = getAngleType(270);
assertEquals(reflex, "Reflex angle");

// Example: Identify Invalid Angles
const invalid = getAngleType(-45);
assertEquals(invalid, "Invalid angle");
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@

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

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

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

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

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

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

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

// Example: -2/1 is not a proper fraction
assertEquals(isProperFraction(-2, 1), false);

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

// Example: 1/0 is not a proper fraction (undefined)
assertEquals(isProperFraction(1, 0), false);

// Example: -1/0 is not a proper fraction (undefined)
assertEquals(isProperFraction(-1, 0), false);

// Example: 0/0 is not a proper fraction (undefined)
assertEquals(isProperFraction(0, 0), false);
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@

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

// Validate the suit
const validSuits = ["♠", "♥", "♦", "♣"];
if (!validSuits.includes(suit)) {
throw new Error("Invalid card: Invalid suit");
}

if (rank === "A") {
return 11;
} else if (["J", "Q", "K"].includes(rank)) {
return 10;
} else if (["2", "3", "4", "5", "6", "7", "8", "9", "10"].includes(rank)) {
return Number(rank); // Convert the rank string to a number
} else {
throw new Error("Invalid card: Invalid rank");
}
}

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

// Handling invalid cards
try {
Expand All @@ -50,3 +71,16 @@ try {
} catch (e) {}

// What other invalid card cases can you think of?
try {
getCardValue("11♠");

// This line will not be reached if an error is thrown as expected
console.error("Error was not thrown for invalid card");
} catch (e) {}

try {
getCardValue("A♤");

// This line will not be reached if an error is thrown as expected
console.error("Error was not thrown for invalid card");
} catch (e) {}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,42 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => {
expect(getAngleType(1)).toEqual("Acute angle");
expect(getAngleType(45)).toEqual("Acute angle");
expect(getAngleType(89)).toEqual("Acute angle");
expect(getAngleType(89.9)).toBe("Acute angle");
});

// Case 2: Right angle
test(`should return "Right angle" when (angle === 90)`, () => {
// Test right angle
expect(getAngleType(90)).toEqual("Right angle");
});

// Case 3: Obtuse angles
test(`should return "Obtuse angle" when (90 < angle < 180)`, () => {
// Test various obtuse angles, including boundary cases
expect(getAngleType(91)).toEqual("Obtuse angle");
expect(getAngleType(135)).toEqual("Obtuse angle");
expect(getAngleType(179)).toEqual("Obtuse angle");
});

// Case 4: Straight angle
test(`should return "Straight angle" when (angle === 180)`, () => {
// Test straight angle
expect(getAngleType(180)).toEqual("Straight angle");
});

// Case 5: Reflex angles
test(`should return "Reflex angle" when (180 < angle < 360)`, () => {
// Test various reflex angles, including boundary cases
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" when (angle <= 0 or angle >= 360)`, () => {
// Test various invalid angles, including boundary cases
expect(getAngleType(-1)).toEqual("Invalid angle");
expect(getAngleType(0)).toEqual("Invalid angle");
expect(getAngleType(360)).toEqual("Invalid angle");
expect(getAngleType(361)).toEqual("Invalid angle");
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,20 @@ const isProperFraction = require("../implement/2-is-proper-fraction");
// Special case: numerator is zero
test(`should return false when denominator is zero`, () => {
expect(isProperFraction(1, 0)).toEqual(false);
expect(isProperFraction(-1, 0)).toEqual(false);
expect(isProperFraction(0, 0)).toEqual(false);
});

test(`should return true for valid proper fractions for Positives and Negatives`, () => {
expect(isProperFraction(1, 2)).toEqual(true);
expect(isProperFraction(-1, 2)).toEqual(true);
expect(isProperFraction(1, -2)).toEqual(true);
expect(isProperFraction(-1, -2)).toEqual(true);
});

test(`should return false for improper fraction`, () => {
expect(isProperFraction(-1, 0)).toEqual(false);
expect(isProperFraction(2, 1)).toEqual(false);
expect(isProperFraction(-2, 1)).toEqual(false);
expect(isProperFraction(2, 2)).toEqual(false);
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,35 @@ test(`Should return 11 when given an ace card`, () => {
expect(getCardValue("A♠")).toEqual(11);
});

// Case 2: 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 3: Number Cards (2-10)
test(`Should return the numeric value for Number Cards`, () => {
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);
});

// Handling invalid cards
test(`Should throw an error for invalid cards`, () => {
expect(() => getCardValue("invalid")).toThrow();
expect(() => getCardValue("AinvalidSuit")).toThrow();
expect(() => getCardValue("InvalidRank♠")).toThrow();
expect(() => getCardValue("A♤")).toThrow("Invalid card: Invalid suit");
expect(() => getCardValue("11♠")).toThrow("Invalid card: Invalid rank");
});

// Suggestion: Group the remaining test data into these categories:
// Number Cards (2-10)
// Face Cards (J, Q, K)
Expand All @@ -17,4 +46,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