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

function getAngleType(angle) {
// TODO: Implement this function
if (angle > 0 && angle < 90) {
return "Acute angle";
}
if (angle === 90) {
return "Right angle";
}

if (angle > 90 && angle < 180) {
return "Obtuse angle";
}

if (angle === 180) {
return "Straight angle";
}

if (angle > 180 && angle < 360) {
return "Reflex angle";
}
return "Invalid angle";
}

// TODO: Implement this function

// The line below allows us to load the getAngleType function into tests in other files.
// This will be useful in the "rewrite tests with jest" step.
module.exports = getAngleType;
Expand All @@ -35,3 +55,28 @@ function assertEquals(actualOutput, targetOutput) {
// Example: Identify Right Angles
const right = getAngleType(90);
assertEquals(right, "Right angle");

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

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

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

const reflex = getAngleType(275);
assertEquals(reflex, "Reflex angle");
// test invalid cases

const invalidAngleCase1 = getAngleType(0);
assertEquals(invalidAngleCase1, "Invalid angle");

const invalidAngleCase2 = getAngleType(-15);
assertEquals(invalidAngleCase2, "Invalid angle");

const invalidAngleCase3 = getAngleType(360);
assertEquals(invalidAngleCase3, "Invalid angle");

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

function isProperFraction(numerator, denominator) {
// TODO: Implement this function
// Denominator cannot be 0 because it won't make a fraction.
if (denominator === 0) {
return false;
}
// Compare absolute values to eliminate negative values
//Proper fraction = absolute value of numerator < absolute value of denominator
return Math.abs(numerator) < Math.abs(denominator);
}

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

// Example: 1/2 is a proper fraction
assertEquals(isProperFraction(1, 0), false);
assertEquals(isProperFraction(2, 1), false);
assertEquals(isProperFraction(1, 2), true);
assertEquals(isProperFraction(-1, -2), true);
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,55 @@
// After you have implemented the function, write tests to cover all the cases, and
// execute the code to ensure all tests pass.

// TODO: Implement this function
function getCardValue(card) {
// TODO: Implement this function
// 1. declare variables in an array to use for validation
const validRanks = [
"A",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"J",
"Q",
"K",
];
const validSuits = ["♠", "♥", "♦", "♣"];
//2. Check that input is a string
if (typeof card !== "string") {
throw new Error("Invalid card");
}
// 3. Must have at least 2 characters but 3 or less (rank and suit)
if (card.length < 2 || card.length > 3) {
throw new Error("Invalid card");
}

// 4. Defining the suit and rank
//Select character index -1, the last character only.
const suit = card.slice(-1);
//select everything from index 0 and stop before the last character
const rank = card.slice(0, -1);

// 5. Checking suit validity
//Checks if suit is in the array
if (!validSuits.includes(suit)) {
throw new Error("Invalid card");
}
// 6. Checking rank validity
//Checks if rank is in the array
if (!validRanks.includes(rank)) {
throw new Error("Invalid card");
}
//7. Determines the output values for special cards
if (rank === "A") return 11;
if (["J", "Q", "K"].includes(rank)) return 10;
// 8. Otherwise it returns the number of the card
return Number(rank);
}

// The line below allows us to load the getCardValue function into tests in other files.
Expand All @@ -40,13 +87,43 @@ 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);
assertEquals(getCardValue("J♣"), 10);

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

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

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

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

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

try {
getCardValue("♠♥♦♣");
console.error("Error was not thrown for invalid card");
} catch (error) {}

try {
getCardValue("5$");
console.error("Error was not thrown for invalid card");
} catch (error) {}

try {
getCardValue("£50");
console.error("Error was not thrown for invalid card");
} catch (error) {}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,48 @@ const getAngleType = require("../implement/1-get-angle-type");
// TODO: Write tests in Jest syntax to cover all cases/outcomes,
// including boundary and invalid cases.

// Test various acute angles, including boundary cases
// Case 1: Acute angles
test(`should return "Acute angle" when (0 < angle < 90)`, () => {
// Test various acute angles, including boundary cases
// When angle is greater that 0 and less than 90
test(`should return "Acute angle" when (angle >0 angle < 90)`, () => {
expect(getAngleType(1)).toEqual("Acute angle");
expect(getAngleType(45)).toEqual("Acute angle");
expect(getAngleType(89)).toEqual("Acute angle");
});

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

// Case 3: Obtuse angles
// When angle is greater that 90 and less than 180
test(`should return "Obtuse angle" when (angle >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
// When angle is 180
test(`should return "Straight angle" when (angle = 180)`, () => {
expect(getAngleType(180)).toEqual("Straight angle");
});

// Case 5: Reflex angles
// When angle is greater that 180 and less than 360
test(`should return "Reflex angle" when (angle >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
// When angle is equal to or less than 0 and is equal to or greater than 360
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(520)).toEqual("Invalid angle");
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,15 @@ 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 false when absolute numerator is greater than or equal to absolute denominator", () => {
expect(isProperFraction(2, 1)).toEqual(false);
});
Copy link
Copy Markdown
Contributor

@cjyuan cjyuan Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test category is good.
To make the test more comprehensive, we could choose different combination of positive an negative values to test. For examples,

  expect(isProperFraction(2, 1)).toEqual(false);
  expect(isProperFraction(2, -1)).toEqual(false);
  expect(isProperFraction(-2, 1)).toEqual(false);
  expect(isProperFraction(-2, -1)).toEqual(false);

  expect(isProperFraction(2, 2)).toEqual(false);
  expect(isProperFraction(2, -2)).toEqual(false);
  expect(isProperFraction(-2, 2)).toEqual(false);
  expect(isProperFraction(-2, -2)).toEqual(false);

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cjyuan Thank you. I will take note and make improvements in my future coding projects.


test("should return true when absolute numerator is less than absolute denominator", () => {
expect(isProperFraction(1, 2)).toEqual(true);
});

test("should correctly handle negative numbers using absolute values", () => {
expect(isProperFraction(-1, -2)).toEqual(true);
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,97 @@ const getCardValue = require("../implement/3-get-card-value");

// TODO: Write tests in Jest syntax to cover all possible outcomes.

// Case 1: Ace (A) - works
// test(`Should return 11 when given an ace card`, () => {
// expect(getCardValue("A♠")).toEqual(11);
// });

// Suggestion: Group the remaining test data into these categories:
// Number Cards (2-10) - works
// test(`Should return a number given (2-10) card`, () => {
// 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) - works
// test(`Should return 10 when given a Queen, King and Jack cards`, () => {
// expect(getCardValue("Q♥")).toEqual(10);
// expect(getCardValue("K♦")).toEqual(10);
// expect(getCardValue("J♣")).toEqual(10);
// });

// Invalid Cards - works
// test('Should throw "Invalid card" for "invalid"', () => {
// expect(() => getCardValue("invalid")).toThrow("Invalid card");
// expect(() => getCardValue("789")).toThrow("Invalid card");
// expect(() => getCardValue("")).toThrow("Invalid card");
// expect(() => getCardValue("♠♥♦♣")).toThrow("Invalid card");
// expect(() => getCardValue("5$")).toThrow("Invalid card");
// expect(() => getCardValue("£50")).toThrow("Invalid card");
// });

// **Invalid Cards - this only checks the array of invalid input. I have to think about how to
// use the loop here in the reverse order, what is valid or else return invalid!**
// test('should throw "Invalid card" for invalid inputs', () => {
// const invalidCards = ["invalid", "789", "", "♠♥♦♣", "5$", "£50"];

// for (const card of invalidCards) {
// expect(() => getCardValue(card)).toThrow("Invalid card");
// }
// });

// Case 1: Ace (A)
test(`Should return 11 when given an ace card`, () => {
expect(getCardValue("A♠")).toEqual(11);
test("should return a value of 11 when given Ace cards", () => {
const validSuits = ["♠", "♥", "♦", "♣"];

for (let i = 0; i < validSuits.length; i++) {
const card = "A" + validSuits[i];
expect(getCardValue(card)).toEqual(11);
}
});

// Suggestion: Group the remaining test data into these categories:
// Number Cards (2-10)
// Face Cards (J, Q, K)
// Invalid Cards
// Case 2: Number Cards (2-10)
test("should return the value of number cards (2-10)", () => {
const validSuits = ["♠", "♥", "♦", "♣"];
for (let rank = 2; rank <= 10; rank++) {
for (let suit of validSuits) {
const card = `${rank}${suit}`;
expect(getCardValue(card)).toEqual(rank);
}
}
});

// Case 3: Face Cards (J, Q, K)
test("should return a value of 10 for cards (J, Q, K)", () => {
const validRanks = ["J", "Q", "K"];
const validSuits = ["♠", "♥", "♦", "♣"];

for (let rank of validRanks) {
for (let suit of validSuits) {
const card = `${rank}${suit}`;
expect(getCardValue(card)).toEqual(10);
}
}
});

// Case 4: Invalid Cards
test('Should throw "Invalid card" for "invalid"', () => {
expect(() => getCardValue("invalid")).toThrow("Invalid card");
expect(() => getCardValue("789")).toThrow("Invalid card");
expect(() => getCardValue("")).toThrow("Invalid card");
expect(() => getCardValue("♠♥♦♣")).toThrow("Invalid card");
expect(() => getCardValue("5$")).toThrow("Invalid card");
expect(() => getCardValue("£50")).toThrow("Invalid 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