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

function getAngleType(angle) {
// TODO: Implement this function
if (angle <= 0 || angle >= 360) {
return "Invalid angle";
} else if (angle > 0 && angle < 90) {
return "Acute angle";
} else if (angle > 90 && angle < 180) {
return "Obtuse angle";
} else if (angle === 90) {
return "Right angle";
} else if (angle===180) {
return "Straight angle";
} else if (angle > 180 && angle < 360) {
return "Reflex angle";
}
}

// The line below allows us to load the getAngleType function into tests in other files.
Expand All @@ -34,4 +46,22 @@ function assertEquals(actualOutput, targetOutput) {
// TODO: Write tests to cover all cases, including boundary and invalid cases.
// Example: Identify Right Angles
const right = getAngleType(90);
const acute1 = getAngleType(20);
const invalid = getAngleType(0);
const acute3 = getAngleType(89.999);
const obtuse = getAngleType(98.25);
const straightAngle = getAngleType(180);
const invalid1 = getAngleType(360);
const invalidAngle = getAngleType(362);
const reflexAngle2 = getAngleType(210);

assertEquals(right, "Right angle");
assertEquals(acute1, "Acute angle");
assertEquals(invalid, "Invalid angle");
assertEquals(obtuse, "Obtuse angle");
assertEquals(straightAngle, "Straight angle");
assertEquals(invalid1, "Invalid angle");
assertEquals(invalidAngle, "Invalid angle");
assertEquals(reflexAngle2, "Reflex angle");
assertEquals(acute3, "Acute angle");

Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,16 @@
// Acceptance criteria:
// After you have implemented the function, write tests to cover all the cases, and
// execute the code to ensure all tests pass.

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

if (Math.abs(numerator) >= Math.abs(denominator)) {
return false;
}

return true;
}

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

// Example: 1/2 is a proper fraction
assertEquals(isProperFraction(1, 2), true);
assertEquals(isProperFraction(1, 10000), true);
assertEquals(isProperFraction(100, 2), false);
assertEquals(isProperFraction(1e2, 1e3), true);
assertEquals(isProperFraction(1e4, 1), false);
assertEquals(isProperFraction(-1, 1), false);
assertEquals(isProperFraction(1, -1), false);
assertEquals(isProperFraction(0, -1), true);
assertEquals(isProperFraction(-1, -2), true);
assertEquals(isProperFraction(1, -2), true);
assertEquals(isProperFraction(-1, 2), true);
assertEquals(isProperFraction(1, 0), false);
assertEquals(isProperFraction(-1, 0), false);
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"
// The suit can be one of the following emojis:
// "♠", "♥", "♦", "♣"
// For example: "A♠", "2♥", "10♥", "J", "Q♦", "K♦".
// For example: "A♠", "2♥", "10♥", "J"2♠"", "Q♦", "K♦".

// When the card is an ace ("A"), the function should return 11.
// When the card is a face card ("J", "Q", "K"), the function should return 10.
Expand All @@ -22,7 +22,38 @@
// execute the code to ensure all tests pass.

function getCardValue(card) {
// TODO: Implement this function
if (typeof card !== "string") {
throw new Error("Invalid string");
}

const suit = card.slice(-1);
const rank = card.slice(0, -1);
const validSuits = ["♠", "♥", "♦", "♣"];
const validRanks = [
"A",
"J",
"Q",
"K",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
];

if (!validSuits.includes(suit) || !validRanks.includes(rank)) {
throw new Error("Invalid string");
} else if (rank === "A") {
return 11;
} else if (rank === "J" || rank === "Q" || rank === "K") {
return 10;
} else {
return Number(rank);
}
}

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

// Handling invalid cards
try {
getCardValue("invalid");
function throwInvalidStringError(card) {
try {
getCardValue(card);
// This line will not be reached if an error is thrown as expected
console.error("Error was not thrown for invalid card");
} catch (e) {
assertEquals(e.message, "Invalid string");
}
}

// This line will not be reached if an error is thrown as expected
console.error("Error was not thrown for invalid card");
} catch (e) {}
throwInvalidStringError("invalid");
throwInvalidStringError(" ♠");
throwInvalidStringError(" 6 ♠");
throwInvalidStringError(" ♠");
throwInvalidStringError(" **♠");
throwInvalidStringError("10 **");
throwInvalidStringError("A10♠");
throwInvalidStringError("0x02♠");
throwInvalidStringError("2.1♠");
throwInvalidStringError("0002♠");
throwInvalidStringError("22");
throwInvalidStringError("♠2");
throwInvalidStringError("2 ♠");
throwInvalidStringError("2.♠");
throwInvalidStringError("+2♠");
throwInvalidStringError("♠A");

// What other invalid card cases can you think of?
// card with a mix of suit
//card with 0 at the front 01♠
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,40 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => {
});

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

// Case 3: Obtuse angles

test(`should return "Obtuse angles" when (90< angle <180)`, () => {
expect(getAngleType(91)).toEqual("Obtuse angle");
expect(getAngleType(179)).toEqual("Obtuse angle");
expect(getAngleType(160)).toEqual("Obtuse angle");
expect(getAngleType(100)).toEqual("Obtuse angle");
expect(getAngleType(151.55)).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 (angle > 180 && angle < 360)`, () => {
expect(getAngleType(181)).toEqual("Reflex angle");
expect(getAngleType(270)).toEqual("Reflex angle");
expect(getAngleType(350)).toEqual("Reflex angle");
expect(getAngleType(359.5)).toEqual("Reflex angle");
expect(getAngleType(210)).toEqual("Reflex angle");
expect(getAngleType(200)).toEqual("Reflex angle");
});

// Case 6: Invalid angles
test(`Should return "Invalid angle" when (0 =<angle>=360)`, () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

How about (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(500)).toEqual("Invalid angle");
expect(getAngleType("1e5")).toEqual("Invalid angle");
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,32 @@ const isProperFraction = require("../implement/2-is-proper-fraction");
// TODO: Write tests in Jest syntax to cover all combinations of positives, negatives, zeros, and other categories.

// Special case: numerator is zero
test(`should return false when denominator is zero`, () => {
test(`should return true when numerator is zero`, () => {
expect(isProperFraction(0, -4)).toEqual(true);
expect(isProperFraction(0, 1)).toEqual(true);
});
test("should return false when denominator is zero", () => {
expect(isProperFraction(1, 0)).toEqual(false);
expect(isProperFraction(10, 0)).toEqual(false);
expect(isProperFraction(-2, 0)).toEqual(false);
expect(isProperFraction(-1, 0)).toEqual(false);
expect(isProperFraction(1, 0)).toEqual(false);
});
Copy link
Contributor

Choose a reason for hiding this comment

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

These data categories and samples are not comprehensive enough. You should test also those proper and improper fraction samples you had in implement/2-is-proper-fraction.js.

Copy link
Author

Choose a reason for hiding this comment

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

Alright, I will improve the test case. Thank you very much


test("should return false when denominator and numerator have both the same negative and positive integer", () => {
expect(isProperFraction(-1, 1)).toEqual(false);
expect(isProperFraction(1, -1)).toEqual(false);
});

test(`should return true when denominator is > numerator `, () => {
expect(isProperFraction(1, 2)).toEqual(true);
expect(isProperFraction(1, 10000)).toEqual(true);

expect(isProperFraction(1e2, 1e3)).toEqual(true);
expect(isProperFraction(-1, 2)).toEqual(true);
});

test("should return false when denominator < numerator", () => {
Comment on lines +25 to +33
Copy link
Contributor

Choose a reason for hiding this comment

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

We can use pseudo-code such as abs(...) to more accurately state the condition when the function should return true or false.

Otherwise, isProperFaction(-100, 2) would be expected to return true because 2 > -100.

expect(isProperFraction(1e4, 1)).toEqual(false);
expect(isProperFraction(100, 2)).toEqual(false);
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,48 @@ 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 Number from "2-10" when given a number 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)

test(`Should return 10 when given a face card`, () => {
expect(getCardValue("J♣")).toEqual(10);
expect(getCardValue("K♣")).toEqual(10);
expect(getCardValue("Q♣")).toEqual(10);
expect(getCardValue("Q♠")).toEqual(10);
expect(getCardValue("J♠")).toEqual(10);
expect(getCardValue("K♠")).toEqual(10);
});
// Invalid Cards

test("throws on Invalid String", () => {
expect(() => {
getCardValue("A10♠");
}).toThrow("Invalid string");
expect(() => {
getCardValue("A");
}).toThrow("Invalid string");
expect(() => {
getCardValue("110♠");
}).toThrow("Invalid string");
expect(() => {
getCardValue(" ♠");
}).toThrow("Invalid string");
expect(() => {
getCardValue("eeeeeej1234");
}).toThrow("Invalid string");
});

// 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