Skip to content
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,21 @@ function assertEquals(actualOutput, targetOutput) {
// Example: Identify Right Angles
const right = getAngleType(90);
assertEquals(right, "Right angle");

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

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

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

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

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

const invalid2 = getAngleType(372);
assertEquals(invalid2, "Invalid angle");
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,19 @@

function isProperFraction(numerator, denominator) {
// TODO: Implement this function
}

if (denominator === 0) {
return false; // A fraction with a denominator of 0 is undefined, so it's not a proper fraction.
}
if (numerator === 0) {
return true; // A fraction with a numerator of 0 is a proper fraction (0/1 = 0).
}
if (Math.abs(numerator) < Math.abs(denominator)) {
return true; // If the absolute value of the numerator is less than the absolute value of the denominator,
// it's a proper fraction.
}
return false; // Otherwise, it's not a proper fraction.
}
// 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 +42,15 @@ function assertEquals(actualOutput, targetOutput) {

// 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: 0/5 is a proper fraction
assertEquals(isProperFraction(0, 5), true);

// Example: -5/10 is a proper fraction
assertEquals(isProperFraction(-5, 10), true);

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

function getCardValue(card) {
// TODO: Implement this function

// 1. Define valid suits and ranks
const suits = ["♠", "♥", "♦", "♣"];
const ranks = [
"A",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"J",
"Q",
"K",
];

// 2. Basic validation: ensure card is a string and not empty
if (typeof card !== "string" || card.length === 0) {
throw new Error("Card must be a non-empty string");
}

// 3. Extract rank and suit from the card string
const rank = card.substring(0, card.length - 1);
const suit = card.substring(card.length - 1);

// 4. Validate rank and suit
if (!ranks.includes(rank)) {
throw new Error("Invalid rank");
}
if (!suits.includes(suit)) {
throw new Error("Invalid suit");
}

// 5. Return the appropriate value based on the rank
if (rank === "A") {
return 11;
} else if (["J", "Q", "K"].includes(rank)) {
return 10;
} else {
return parseInt(rank);
}
}

// The line below allows us to load the getCardValue function into tests in other files.
Expand Down Expand Up @@ -50,3 +94,53 @@ try {
} catch (e) {}

// What other invalid card cases can you think of?

try {
getCardValue("11♠");
console.error("Error was not thrown for invalid rank");
} catch (e) {}

try {
getCardValue("A♤");
console.error("Error was not thrown for invalid suit");
} catch (e) {}

try {
getCardValue("");
console.error("Error was not thrown for empty string");
} catch (e) {}

try {
getCardValue(123);
console.error("Error was not thrown for non-string input");
} catch (e) {}

try {
getCardValue("10");
console.error("Error was not thrown for missing suit");
} catch (e) {}

try {
getCardValue("♠");
console.error("Error was not thrown for missing rank");
} catch (e) {}

try {
getCardValue("A");
console.error("Error was not thrown for missing suit");
} catch (e) {}

try {
getCardValue("1♠");
console.error("Error was not thrown for invalid rank");
} catch (e) {}

try {
getCardValue("10XX");
console.error("Error was not thrown for extra characters");
} catch (e) {}

try {
getCardValue("Z♦");
console.error("Error was not thrown for invalid rank");
} catch (e) {}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,30 @@ 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(120)).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(270)).toEqual("Reflex angle");
expect(getAngleType(359)).toEqual("Reflex angle");
});
// Case 6: Invalid angles
test(`should throw an error for negative angles`, () => {
expect(() => getAngleType(-1)).toThrow("Invalid angle");
});
test(`should throw an error for angles >= 360`, () => {
expect(() => getAngleType(360)).toThrow("Invalid angle");
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,60 @@ const isProperFraction = require("../implement/2-is-proper-fraction");
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);
});

// Proper fractions with positive numbers
test(`should return true for proper fractions with positive numerator and denominator`, () => {
expect(isProperFraction(1, 2)).toEqual(true);
expect(isProperFraction(3, 4)).toEqual(true);
});

// Improper fractions with positive numbers
test(`should return false for improper fractions with positive numerator and denominator`, () => {
expect(isProperFraction(2, 1)).toEqual(false);
expect(isProperFraction(5, 3)).toEqual(false);
});

// Proper fractions with negative numbers
test(`should return true for proper fractions with negative numerator and positive denominator`, () => {
expect(isProperFraction(-1, 2)).toEqual(true);
expect(isProperFraction(-3, 4)).toEqual(true);
});

test(`should return true for proper fractions with positive numerator and negative denominator`, () => {
expect(isProperFraction(1, -2)).toEqual(true);
expect(isProperFraction(3, -4)).toEqual(true);
});

// Improper fractions with negative numbers
test(`should return false for improper fractions with negative numerator and positive denominator`, () => {
expect(isProperFraction(-2, 1)).toEqual(false);
expect(isProperFraction(-5, 3)).toEqual(false);
});
test(`should return false for improper fractions with positive numerator and negative denominator`, () => {
expect(isProperFraction(2, -1)).toEqual(false);
expect(isProperFraction(5, -3)).toEqual(false);
});

// Special case: both numerator and denominator are zero
test(`should return false when both numerator and denominator are zero`, () => {
expect(isProperFraction(0, 0)).toEqual(false);
});

// Numerator equals denominator
test("should return false when numerator equals denominator", () => {
expect(isProperFraction(5, 5)).toBe(false);
expect(isProperFraction(-5, -5)).toBe(false);
});

// Both numerator and denominator are negative
test("should return true for proper fractions with two negative numbers", () => {
expect(isProperFraction(-1, -3)).toBe(true);
});

test("should return false for improper fractions with two negative numbers", () => {
expect(isProperFraction(-10, -2)).toBe(false);
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,34 @@ 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("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);
});
// 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("11♠")).toThrow();
expect(() => getCardValue("B♠")).toThrow();
expect(() => getCardValue("Z♠")).toThrow();
expect(() => getCardValue("A")).toThrow();
expect(() => getCardValue("10")).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