Skip to content
Open
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";
}
return "Invalid angle";
}

// The line below allows us to load the getAngleType function into tests in other files.
Expand All @@ -32,6 +45,32 @@ 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");
// Test Cases
// cases are an array of arrays, where each inner array has two elements:
// - The first element is the input value (angle)
// - The second element is the expected output

const testCases = [
[1, "Acute angle"],
[45, "Acute angle"],
[89, "Acute angle"],
[90, "Right angle"],
[91, "Obtuse angle"],
[135, "Obtuse angle"],
[179, "Obtuse angle"],
[180, "Straight angle"],
[181, "Reflex angle"],
[225, "Reflex angle"],
[270, "Reflex angle"],
[359, "Reflex angle"],
[0, "Invalid angle"],
[-1, "Invalid angle"],
[360, "Invalid angle"],
[400, "Invalid angle"],
];

// loop through each case and assert that the output is correct
for (const [angle, expected] of testCases) {
const actual = getAngleType(angle);
assertEquals(actual, expected);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@

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

// The line below allows us to load the isProperFraction function into tests in other files.
Expand All @@ -30,4 +34,27 @@ function assertEquals(actualOutput, targetOutput) {
// What combinations of numerators and denominators should you test?

// Example: 1/2 is a proper fraction
assertEquals(isProperFraction(1, 2), true);
// Test Cases
// testCases is an array of arrays
// [
// [numerator, denominator, output]...
// ]
const testCases = [
[1, 2, true],
[2, 4, true],
[3, 6, true],
[4, 8, true],
[5, 10, true],
[2, 0, false],
[5, 4, false],
[6, 3, false],
[8, 4, false],
[10, 5, false],
];

// loop through each case and assert that the output is correct
for (const [numerator, denominator, expected] of testCases) {
const actual = isProperFraction(numerator, denominator);
assertEquals(actual, expected);
}
//
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,41 @@
// After you have implemented the function, write tests to cover all the cases, and
// execute the code to ensure all tests pass.

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

function getCardValue(card) {
// TODO: Implement this function
if (typeof card !== "string" || card.length < 2 || card.length > 3) {
throw new Error("Invalid card string");
}
const rank = card.slice(0, -1);
const suit = card.slice(-1);

if (!suits.includes(suit) || !ranks.includes(rank)) {
throw new Error("Invalid card string");
}

if (rank === "A") {
return 11;
}
if (rank === "J" || rank === "Q" || rank === "K") {
return 10;
}
return parseInt(rank, 10);
}

// The line below allows us to load the getCardValue function into tests in other files.
Expand All @@ -38,17 +71,40 @@ function assertEquals(actualOutput, targetOutput) {
}

// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
// Examples:
assertEquals(getCardValue("9♠"), 9);

// Handling invalid cards
try {
getCardValue("invalid");
// Test cases

// This line will not be reached if an error is thrown as expected
console.error("Error was not thrown for invalid card 😢");
} catch (e) {
console.log("Error thrown for invalid card 🎉");
}
const testCases = [
["A♠", 11],
["2♥", 2],
["10♥", 10],
["J♣", 10],
["Q♦", 10],
["K♦", 10],
["KK", undefined],
["A", undefined],
["2", undefined],
["3", undefined],
["44", undefined],
["AA1", undefined],
["♣♣", undefined],
["", undefined],
];

// What other invalid card cases can you think of?
// loop through each case and assert that the output is correct
for (const [card, expected] of testCases) {
try {
const actual = getCardValue(card);
if (expected === undefined) {
console.error("Error was not thrown for invalid card 😢");
} else {
assertEquals(actual, expected);
}
} catch (e) {
if (expected === undefined) {
console.log(`Successfully threw an error for invalid card "${card}" 🎉`);
} else {
console.error(`Unexpected error thrown for valid card "${card}": ${e.message} 😢`);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,34 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => {
});

// Case 2: Right angle
test(`should return "Right angle" when (angle === 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(135)).toEqual("Obtuse angle");
expect(getAngleType(179)).toEqual("Obtuse angle");
});

// Case 4: Straight angle
test(`should return "Straight angle" when (angle === 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(225)).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)`, () => {
expect(getAngleType(0)).toEqual("Invalid angle");
expect(getAngleType(-1)).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 @@ -7,4 +7,24 @@ 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(2, 0)).toEqual(false);

});

// Case 2: Denominator is greater than numerator
test(`should return true when denominator is greater than numerator`, () => {
expect(isProperFraction(1, 2)).toEqual(true);
expect(isProperFraction(2, 4)).toEqual(true);
expect(isProperFraction(3, 6)).toEqual(true);
expect(isProperFraction(4, 8)).toEqual(true);
expect(isProperFraction(5, 10)).toEqual(true);
});

// Case 3: Denominator is less than numerator
test(`should return false when denominator is less than numerator`, () => {
expect(isProperFraction(2, 1)).toEqual(false);
expect(isProperFraction(4, 2)).toEqual(false);
expect(isProperFraction(6, 3)).toEqual(false);
expect(isProperFraction(8, 4)).toEqual(false);
expect(isProperFraction(10, 5)).toEqual(false);
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,40 @@ test(`Should return 11 when given an ace card`, () => {
expect(getCardValue("A♠")).toEqual(11);
});


// Case 2: Jack (J), Queen (Q), and King (K) - Face Cards
test(`Should return 10 when given a face card`, () => {
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 when given a number card`, () => {
expect(getCardValue("2♥")).toEqual(2);
expect(getCardValue("10♥")).toEqual(10);
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 4: Invalid Cards
test(`Should throw an error when given an invalid card`, () => {
expect(() => getCardValue("KK")).toThrow("Invalid card string");
expect(() => getCardValue("A")).toThrow("Invalid card string");
expect(() => getCardValue("2")).toThrow("Invalid card string");
expect(() => getCardValue("3")).toThrow("Invalid card string");
expect(() => getCardValue("44")).toThrow("Invalid card string");
expect(() => getCardValue("AA1")).toThrow("Invalid card string");
expect(() => getCardValue("♣")).toThrow("Invalid card string");
expect(() => getCardValue("")).toThrow("Invalid card string");
});

// Suggestion: Group the remaining test data into these categories:
// Number Cards (2-10)
// Face Cards (J, Q, K)
Expand Down
Loading