Skip to content
Open
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,24 @@
// execute the code to ensure all tests pass.

function getAngleType(angle) {
// TODO: Implement this function
if (angle === 180) {
return "Straight angle";
}
if (angle > 180 && angle < 360) {
return "Reflex angle";
}
if (angle === 90) {
return "Right angle";
}
if (angle > 0 && angle < 90) {
return "Acute angle";
}
if (angle > 90 && angle < 180) {
return "Obtuse angle";
}
return "Invalid angle";
// Run the tests, work out what Case 2 is testing, and implement the required code here.
// Then keep going for the other cases, one at a time.
}

// The line below allows us to load the getAngleType function into tests in other files.
Expand All @@ -35,3 +52,30 @@ function assertEquals(actualOutput, targetOutput) {
// Example: Identify Right Angles
const right = getAngleType(90);
assertEquals(right, "Right angle");

// Case 2: Identify Acute Angles:
// When the angle is less than 90 degrees,
// Then the function should return "Acute angle"
const acute = getAngleType(45);
assertEquals(acute, "Acute angle");

// Case 3: Identify Obtuse Angles:
// When the angle is greater than 90 degrees and less than 180 degrees,
// Then the function should return "Obtuse angle"
const obtuse = getAngleType(120);
// ====> write your test here, and then add a line to pass the test in the function above
assertEquals(obtuse, "Obtuse angle");

// Case 4: Identify Straight Angles:
// When the angle is exactly 180 degrees,
// Then the function should return "Straight angle"
// ====> write your test here, and then add a line to pass the test in the function above
const straight = getAngleType(180);
assertEquals(straight, "Straight angle");

// Case 5: Identify Reflex Angles:
// When the angle is greater than 180 degrees and less than 360 degrees,
// Then the function should return "Reflex angle"
// ====> write your test here, and then add a line to pass the test in the function above
const reflex = getAngleType(270);
assertEquals(reflex, "Reflex angle");
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// execute the code to ensure all tests pass.

function isProperFraction(numerator, denominator) {
// TODO: Implement this function
return Math.abs(numerator) < Math.abs(denominator) ? true : false;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

When an expression evaluates to a Boolean value, we can also just return the value of the expression:
return Math.abs(numerator) < Math.abs(denominator);

}

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

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

// Proper Fraction check:
// Input: numerator = 2, denominator = 3
// target output: true
// Explanation: The fraction 2/3 is a proper fraction, where the numerator is less than the denominator. The function should return true.
const properFraction = isProperFraction(2, 3);
assertEquals(properFraction, true);

// Improper Fraction check:
// Input: numerator = 5, denominator = 2
// target output: false
// Explanation: The fraction 5/2 is an improper fraction, where the numerator is greater than or equal to the denominator. The function should return false.
const improperFraction = isProperFraction(5, 2);
assertEquals(improperFraction, false);

// Negative Fraction check:
// Input: numerator = -4, denominator = 7
// target output: true
// Explanation: The fraction -4/7 is a proper fraction because the absolute value of the numerator (4) is less than the denominator (7). The function should return true.
const negativeFraction = isProperFraction(-4, 7);
// ====> complete with your assertion
assertEquals(negativeFraction, true);

// Equal Numerator and Denominator check:
// Input: numerator = 3, denominator = 3
// target output: false
// Explanation: The fraction 3/3 is not a proper fraction because the numerator is equal to the denominator. The function should return false.
const equalFraction = isProperFraction(3, 3);
// ====> complete with your assertion
assertEquals(equalFraction, false);

// Stretch:
// What other scenarios could you test for?

// Zero Numerator check:
// Input: numerator = 0, denominator = 5
// target output: true
// Explanation: The fraction 0/5 is a proper fraction because the numerator is less than the denominator.
const zeroNumerator = isProperFraction(0, 5);
assertEquals(zeroNumerator, true);

// Example: 1/2 is a proper fraction
assertEquals(isProperFraction(1, 2), true);
// Zero Denominator check:
// Input: numerator = 5, denominator = 0
// target output: false
// Explanation: The fraction 5/0 is undefined because division by zero is not allowed.
const zeroDenominator = isProperFraction(5, 0);
assertEquals(zeroDenominator, false);
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,23 @@
// execute the code to ensure all tests pass.

function getCardValue(card) {
// TODO: Implement this function
const rank = card.slice(0, -1);
const lastChar = card.slice(-1);
const validSuits = ["♠", "♥", "♦", "♣"];

if (!validSuits.includes(lastChar)) {
throw new Error("Invalid card suit");
}

if (rank === "A") return 11;
if (rank === "10" || rank === "J" || rank === "Q" || rank === "K") return 10;

const rankNum = Number(rank);
if (!Number.isNaN(rankNum) && rankNum >= 2 && rankNum < 10) {
return rankNum;
}
Comment on lines +36 to +45
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I added an if statement beforehand to check typeof for the variable. If it fails it will throw an error.

I don't see any statement involving typeof.

Currently, a card value like ""0x02♠" or "3.1416♠" can still pass the check on line 37.

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.

Apologies, I got it mixed up with a different task. It is resolved now.


throw new Error("Invalid card rank");
}

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

// Handling invalid cards
try {
getCardValue("invalid");
// Handle Number Cards (2-10):
// Given a card with a rank between "2" and "9",
// When the function is called with such a card,
// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
const fiveofHearts = getCardValue("5♥");
// ====> write your test here, and then add a line to pass the test in the function above
assertEquals(fiveofHearts, 5);

// Handle Face Cards (J, Q, K):
// Given a card with a rank of "10," "J," "Q," or "K",
// When the function is called with such a card,
// Then it should return the value 10, as these cards are worth 10 points each in blackjack.
const kingofDiamonds = getCardValue("K♦");
assertEquals(kingofDiamonds, 10);

// This line will not be reached if an error is thrown as expected
console.error("Error was not thrown for invalid card");
} catch (e) {}
// Handle Ace (A):
// Given a card with a rank of "A",
// When the function is called with an Ace,
// Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack.
const aceofClubs = getCardValue("A♣");
assertEquals(aceofClubs, 11);

// What other invalid card cases can you think of?
// Handle Invalid Cards:
// Given a card with an invalid rank (neither a number nor a recognized face card),
// When the function is called with such a card,
// Then it should throw an error indicating "Invalid card rank."
try {
getCardValue("1♠");
} catch (error) {
assertEquals(error.message, "Invalid card rank");
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,29 @@ 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 is <= 0 or >= 360`, () => {
expect(getAngleType(0)).toEqual("Invalid angle");
expect(getAngleType(-10)).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 @@ -8,3 +8,35 @@ 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 true for proper fraction with positive numerator and denominator`, () => {
expect(isProperFraction(1, 2)).toEqual(true);
});

test(`should return false for improper fraction with positive numerator and denominator`, () => {
expect(isProperFraction(3, 2)).toEqual(false);
});

Comment on lines +12 to +19
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What about negative numbers and different combinations of positive/negative/zero?

Think also what edge case to check?

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.

Added extra tests for negative numerators/denominators and all are passing.

test(`should return true for numerator zero and positive denominator`, () => {
expect(isProperFraction(0, 5)).toEqual(true);
});

test(`should return false for denominator zero`, () => {
expect(isProperFraction(5, 0)).toEqual(false);
});

test(`should return false for negative denominator`, () => {
expect(isProperFraction(5, -2)).toEqual(false);
});

test(`should return false for negative numerator`, () => {
expect(isProperFraction(-5, 2)).toEqual(false);
});

test(`should return false for negative numerator`, () => {
expect(isProperFraction(-5, 2)).toEqual(false);
});
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same test sample.


test(`should return true for negative numerator and negative denominator where numerator is smaller.`, () => {
expect(isProperFraction(-1, -2)).toEqual(true);
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,34 @@ test(`Should return 11 when given an ace card`, () => {
// Face Cards (J, Q, K)
// Invalid Cards

// Case 2: Number Cards (2-10)
test(`Should return the correct value for number cards`, () => {
expect(getCardValue("2♣")).toEqual(2);
expect(getCardValue("5♦")).toEqual(5);
expect(getCardValue("10♥")).toEqual(10);
});

// Case 3: Face Cards (J, Q, K)
test(`Should return 10 for face cards`, () => {
expect(getCardValue("J♠")).toEqual(10);
expect(getCardValue("Q♦")).toEqual(10);
expect(getCardValue("K♥")).toEqual(10);
});

// Case 4: Invalid Cards
test(`Should throw an error for Invalid card rank for invalid cards`, () => {
expect(() => getCardValue("1♠")).toThrow("Invalid card rank");
expect(() => getCardValue("11♥")).toThrow("Invalid card rank");
expect(() => getCardValue("Z♦")).toThrow("Invalid card rank");
});

// Case 5: Invalid suit character
test(`Should throw an error for invalid suit character`, () => {
expect(() => getCardValue("1*")).toThrow("Invalid card suit");
expect(() => getCardValue("11-")).toThrow("Invalid card suit");
expect(() => getCardValue("Z/")).toThrow("Invalid card suit");
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

  • Could also include invalid cases that involves invalid suit character.

  • To test if a function can throw an error as expected, we could use .toThrow(). You can find out more about how to use .toThrow() here: https://jestjs.io/docs/expect#tothrowerror (Note: Pay close attention to the syntax of the example)

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