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 < 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 @@ -31,7 +43,10 @@ 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");
assertEquals(getAngleType(90), "Right angle");
assertEquals(getAngleType(45), "Acute angle");
assertEquals(getAngleType(91), "Obtuse angle");
assertEquals(getAngleType(180), "Straight angle");
assertEquals(getAngleType(185), "Reflex angle");
assertEquals(getAngleType(365), "Invalid angle");
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@
// execute the code to ensure all tests pass.

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

if (numerator < denominator) {
return true;
}
return false;
}

// The line below allows us to load the isProperFraction function into tests in other files.
Expand All @@ -26,8 +33,11 @@ function assertEquals(actualOutput, targetOutput) {
);
}

// TODO: Write tests to cover all cases.
// What combinations of numerators and denominators should you test?

// Example: 1/2 is a proper fraction
assertEquals(isProperFraction(1, 2), true);
assertEquals(isProperFraction(2, 2), false);
assertEquals(isProperFraction(3, 2), false);
assertEquals(isProperFraction(4312, 5000), true);
assertEquals(isProperFraction("Invalid", "data"), false);
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,34 @@
// execute the code to ensure all tests pass.

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

if (rank === "10") {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This works, but I'm curious - why did you decide to handle 10 specially here?

What would happen if someone passed the string "10Q" to this function? What would you want to happen? What other edge cases may you want to test for in your tests?

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.

Well because "10♠" is three characters so I decided to write a special case for it, otherwise all the others combinations should be something like this "K♠" so only two characters. It's probably not the best way to handle this but it works 🤷

return 10;
}

if (card.length > 2) {
throw new Error("Invalid input length");
}

if (!(suit === "♠" || suit === "♥" || suit === "♦" || suit === "♣")) {
throw new Error(
"Invalid second character detected, only suits(♠♥♦♣) are allowed, but got this character:",
suit
);
}

if (rank == "A") {
return 11;
} else if (rank == "J" || rank == "Q" || rank == "K") {
return 10;
} else if (!isNaN(rank)) {
return Number(rank);
} else {
throw new Error("Invalid card");
}
}

// The line below allows us to load the getCardValue function into tests in other files.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,36 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => {
});

// Case 2: Right angle
test(`should return "Right angle" when (angle == 90)`, () => {
// Test various acute angles, including boundary cases
expect(getAngleType(90)).toEqual("Right angle");
});

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

// Case 4: Straight angle
test(`should return "Straight angle" when (angle == 180)`, () => {
// Test various acute angles, including boundary cases
expect(getAngleType(180)).toEqual("Straight angle");
});

// Case 5: Reflex angles
test(`should return "Reflex angle" when (180 < angle < 360)`, () => {
// Test various acute angles, including boundary cases
expect(getAngleType(181)).toEqual("Reflex angle");
expect(getAngleType(220)).toEqual("Reflex angle");
expect(getAngleType(359)).toEqual("Reflex angle");
});

// Case 6: Invalid angles
test(`should return "Invalid angle"`, () => {
// Test various acute angles, including boundary cases
expect(getAngleType(361)).toEqual("Invalid angle");
expect(getAngleType("Test data")).toEqual("Invalid angle");
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@ 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, 2)).toEqual(false);
expect(isProperFraction(3, 2)).toEqual(false);
expect(isProperFraction(3, 4)).toEqual(true);
expect(isProperFraction(4312, 5000)).toEqual(true);
expect(isProperFraction("invalid", "data")).toEqual(false);
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,30 @@ const getCardValue = require("../implement/3-get-card-value");
// Case 1: Ace (A)
test(`Should return 11 when given an ace card`, () => {
expect(getCardValue("A♠")).toEqual(11);
expect(() => getCardValue("AA♠")).toThrow(Error);
});

// Case 2
test(`Should return 10 when given an jack, queen, king`, () => {
expect(getCardValue("J♠")).toEqual(10);
expect(getCardValue("Q♥")).toEqual(10);
expect(getCardValue("K♠")).toEqual(10);
expect(() => getCardValue("K🤣")).toThrow(Error);
expect(() => getCardValue("KKW🤣")).toThrow(Error);
});

// Case 3
test(`Should return number when given a number`, () => {
expect(getCardValue("2♠")).toEqual(2);
expect(getCardValue("10♥")).toEqual(10);
expect(() => getCardValue("423🤣")).toThrow(Error);
});

test("Should return error on invalid input", () => {
expect(() => getCardValue(null)).toThrow(Error);
expect(() => getCardValue(undefined)).toThrow(Error);
expect(() => getCardValue(12398)).toThrow(Error);
expect(() => getCardValue(NaN)).toThrow(Error);
});

// Suggestion: Group the remaining test data into these categories:
Expand All @@ -17,4 +41,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

Loading