Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
0c4631a
Implement getAngleType function and add tests
djebsoft Mar 5, 2026
4a2804d
Implement a function getAngleType and Write tests to cover all cases
djebsoft Mar 12, 2026
6b4ee50
implement isproperfraction function and rewrite tests to cover all cases
djebsoft Mar 12, 2026
7d502dc
Implement getCardValue function and added tests for valid and invalid…
djebsoft Mar 13, 2026
82958b5
validate the first if statement to include the condition there the ra…
djebsoft Mar 13, 2026
c286e4a
Revert "validate the first if statement to include the condition ther…
djebsoft Mar 13, 2026
a70220f
add tests for all angle types and invalid angles
djebsoft Mar 13, 2026
bc11ec7
Write tests in Jest syntax to cover all combinations for isProperFrac…
djebsoft Mar 13, 2026
7eab738
Write tests in Jest syntax to cover all possible outcomes.
djebsoft Mar 13, 2026
3f7c3f8
enhancement: Implement getCardValue function to return correct card v…
djebsoft Mar 15, 2026
6769341
fixed syntax error
djebsoft Mar 15, 2026
f231516
combined similar items into one category
djebsoft Mar 16, 2026
608f1db
combined tha ace cases in one category
djebsoft Mar 16, 2026
9a3c314
added tests for negative numerator/denominator cases
djebsoft Mar 16, 2026
c099074
added tests for negative numerator/denominator cases
djebsoft Mar 16, 2026
fa537b2
added tests for negative numerator/denominator cases
djebsoft Mar 16, 2026
36e8c14
converted the cardRnk string to a number and added a check for the ac…
djebsoft Mar 16, 2026
af851f3
combine the tests with the same description into the same group
djebsoft Mar 16, 2026
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 @@ -35,3 +47,32 @@ function assertEquals(actualOutput, targetOutput) {
// Example: Identify Right Angles
const right = getAngleType(90);
assertEquals(right, "Right angle");

// Identify acute Angles
const acute = getAngleType(60);
assertEquals(acute, "acute angle");

// Identify obtuse Angles
const obtuse = getAngleType(120);
assertEquals(obtuse, "obtuse angle");

// Identify straight Angles
const straight = getAngleType(180);
assertEquals(straight, "straight angle");

// Identify reflex Angles
const reflex = getAngleType(250);
assertEquals(reflex, "reflex angle");

// Identify invalid Angles
const invalid = getAngleType(0);
assertEquals(invalid, "invalid angle");

const invalid = getAngleType(360);
assertEquals(invalid, "invalid angle");

const invalid = getAngleType(-100);
assertEquals(invalid, "invalid angle");

const invalid = getAngleType(1000);
assertEquals(invalid, "invalid angle");
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@

function isProperFraction(numerator, denominator) {
// TODO: Implement this function
if (Math.abs(numerator) < Math.abs(denominator)) {
return true;
} else {
return false;
}
}

// The line below allows us to load the isProperFraction function into tests in other files.
Expand All @@ -31,3 +36,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: 1/2 is a proper fraction
assertEquals(isProperFraction(-1, 2), true);
// Example: -1/2 is a proper fraction
assertEquals(isProperFraction(-1, -2), true);
// 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: 1/2 is not a proper fraction
assertEquals(isProperFraction(-2, -1), false);
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@

function getCardValue(card) {
// TODO: Implement this function
const cardRank = card.slice(0, -1);
const cardSuit = card.slice(-1);
if (card.length < 2 || !["♠", "♥", "♦", "♣"].includes(cardSuit))
throw new Error("invalid card suit");
if (cardRank === "A") return 11;
if (["J", "Q", "K"].includes(cardRank)) return 10;
if (["2", "3", "4", "5", "6", "7", "8", "9", "10"].includes(cardRank))
return Number(cardRank); // the parseint() or Number() can be used to convert the string to a number
throw new Error("invalid card rank");
}

// The line below allows us to load the getCardValue function into tests in other files.
Expand All @@ -41,6 +50,11 @@ function assertEquals(actualOutput, targetOutput) {
// Examples:
assertEquals(getCardValue("9♠"), 9);

assertEquals(getCardValue("A♥"), 11);
assertEquals(getCardValue("J♣"), 10);
assertEquals(getCardValue("Q♠"), 10);
assertEquals(getCardValue("2♦"), 2);

// Handling invalid cards
try {
getCardValue("invalid");
Expand All @@ -50,3 +64,37 @@ try {
} catch (e) {}

// What other invalid card cases can you think of?
try {
getCardValue("S");
console.error("Error was not thrown for invalid card");
} catch (e) {}

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

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

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

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

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

try {
getCardValue("");
console.error("Error was not thrown for invalid card");
} catch (e) {}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,33 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => {
});

// Case 2: Right angle
test(`should return "Right angle" when angle is 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 is 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 return "Invalid angle" when (angle <= 0 or angle >= 360)`, () => {
expect(getAngleType(-1)).toEqual("Invalid angle");
expect(getAngleType(0)).toEqual("Invalid angle");
expect(getAngleType(360)).toEqual("Invalid angle");
expect(getAngleType(361)).toEqual("Invalid angle");
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,33 @@ 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
// Special case: denominator is zero (undefined)
test(`should return false when denominator is zero`, () => {
expect(isProperFraction(1, 0)).toEqual(false);
// Special case: both numerator and denominator are zeros (undefined)
expect(isProperFraction(0, 0)).toEqual(false);
});

// Special case: numerator is zero (proper fraction)
test(`should return true for proper fraction`, () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

The grouping is good.

Could also consider including the condition abs(numerator) < abs(denominator) in the description so that one does not need to lookup the definition of proper fraction.

expect(isProperFraction(0, 1)).toEqual(true);
// case: both values are positive and the value of the numerater is less than the value of the denominater (proper fraction)
expect(isProperFraction(1, 2)).toEqual(true);
// case: the value of the numerater is negative and its absolute value is less than the value of the denominater (roper fraction)
expect(isProperFraction(-1, 2)).toEqual(true);
// case: the value of the denominator is negative and its absolute value is greater than the value of the numerator (proper fraction)
expect(isProperFraction(1, -2)).toEqual(true);
// case: both values of the numerator and denominator are negative and the absolute value of the numerator is less than the absolute value of the denominator (proper fraction)
expect(isProperFraction(-1, -2)).toEqual(true);
});

// case: both values are positive and the value of the denominater is less than the value of the numerater (improper fraction)
test(`should return false for improper fraction`, () => {
expect(isProperFraction(2, 1)).toEqual(false);
// case: the value of the numerater is negative and its absolute value is greater than the value of the denominater (improper fraction)
expect(isProperFraction(-2, 1)).toEqual(false);
// case: both values of the numerator and denominator are negative and the absolute value of the numerator is greater than the absolute value of the denominator (improper fraction)
expect(isProperFraction(-2, -1)).toEqual(false);
// case: the value of the denominator is negative and its absolute value is less than the value of the numerator (improper fraction)
expect(isProperFraction(2, -1)).toEqual(false);
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,44 @@ 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("A♥")).toEqual(11);
expect(getCardValue("A♦")).toEqual(11);
expect(getCardValue("A♣")).toEqual(11);
});

// Suggestion: Group the remaining test data into these categories:
// Number Cards (2-10)
test("should return the value of number cards (2-10)", () => {
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);
});
Comment on lines +17 to +27
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think your function can pass this test yet.

Copy link
Author

Choose a reason for hiding this comment

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

I changed the returned outcome to a number in the function
return Number(cardRank)


// Face Cards (J, Q, K)
test(`Should return the value of face cards (J, Q, K)`, () => {
expect(getCardValue("J♠")).toEqual(10);
expect(getCardValue("Q♦")).toEqual(10);
expect(getCardValue("K♥")).toEqual(10);
});
// Invalid Cards
test(`Should throw an error when given an invalid card`, () => {
expect(() => getCardValue("1♠")).toThrow();
expect(() => getCardValue("2❦")).toThrow();
expect(() => getCardValue("11♥")).toThrow();
expect(() => getCardValue("B♦")).toThrow();
expect(() => getCardValue("Z♣")).toThrow();
expect(() => getCardValue("♣2")).toThrow();
expect(() => getCardValue("s")).toThrow();
expect(() => getCardValue("♣♥♠♦")).toThrow();
expect(() => getCardValue("")).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