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,27 @@
// 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";
}

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

// Acute
assertEquals(getAngleType(45), "Acute angle");

// Right
assertEquals(getAngleType(90), "Right angle");

// Obtuse
assertEquals(getAngleType(120), "Obtuse angle");

// Straight
assertEquals(getAngleType(180), "Straight angle");

// Reflex
assertEquals(getAngleType(270), "Reflex angle");

// Invalid
assertEquals(getAngleType(0), "Invalid angle");
assertEquals(getAngleType(360), "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 (denominator === 0) {
return false;
}

return Math.abs(numerator) < Math.abs(denominator);
}

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

// Example: 1/2 is a proper fraction
assertEquals(isProperFraction(1, 2), true);
assertEquals(isProperFraction(-1, 2), true);
assertEquals(isProperFraction(1, -2), true);

// Improper fractions
assertEquals(isProperFraction(2, 2), false);
assertEquals(isProperFraction(3, 2), false);
assertEquals(isProperFraction(-3, 2), false);

// Invalid denominator
assertEquals(isProperFraction(1, 0), false);
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,38 @@
// execute the code to ensure all tests pass.

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

const validSuits = ["♠", "♥", "♦", "♣"];

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

if (rank === "A") {
return 11;
}

if (rank === "J" || rank === "Q" || rank === "K") {
return 10;
}

if (
rank === "2" ||
rank === "3" ||
rank === "4" ||
rank === "5" ||
rank === "6" ||
rank === "7" ||
rank === "8" ||
rank === "9" ||
rank === "10"
) {
return Number(rank);
}

throw new Error("Invalid card");
}

// The line below allows us to load the getCardValue function into tests in other files.
Expand All @@ -38,8 +69,19 @@ function assertEquals(actualOutput, targetOutput) {
}

// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
// Examples:

// Ace
assertEquals(getCardValue("A♠"), 11);

// Face cards
assertEquals(getCardValue("J♣"), 10);
assertEquals(getCardValue("Q♦"), 10);
assertEquals(getCardValue("K♥"), 10);

// Number cards
assertEquals(getCardValue("2♥"), 2);
assertEquals(getCardValue("9♠"), 9);
assertEquals(getCardValue("10♦"), 10);

// Handling invalid cards
try {
Expand All @@ -49,4 +91,17 @@ try {
console.error("Error was not thrown for invalid card");
} catch (e) {}

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

try {
getCardValue("A");
console.error("Error was not thrown for missing suit");
} catch (e) {}

try {
getCardValue("A★");
console.error("Error was not thrown for invalid suit");
} 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 = 90`, () => {
expect(getAngleType(90)).toEqual("Right angle");
});

// Case 3: Obtuse angles
test(`should return "Obtuse angle" when (90 < angle < 180)`, () => {
// Test various obtuse angles, including boundary cases
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 = 180`, () => {
expect(getAngleType(180)).toEqual("Straight angle");
});

// Case 5: Reflex angles
test(`should return "Reflex angle" when (180 < angle < 360)`, () => {
// Test various reflex angles, including boundary cases
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(360)).toEqual("Invalid angle");
});
Comment on lines +43 to +46
Copy link
Contributor

Choose a reason for hiding this comment

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

It's good that you tested one of the boundary cases. You could consider testing the other one.

Original file line number Diff line number Diff line change
@@ -1,10 +1,37 @@
// This statement loads the isProperFraction function you wrote in the implement directory.
// We will use the same function, but write tests for it using Jest in this file.
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
test(`should return false when denominator is zero`, () => {
expect(isProperFraction(1, 0)).toEqual(false);
});

// Proper fractions with positive numbers
test(`should return true when the numerator is smaller than the denominator`, () => {
expect(isProperFraction(1, 2)).toEqual(true);
expect(isProperFraction(3, 4)).toEqual(true);
});

// Improper fractions with positive numbers
test(`should return false when the numerator is equal to or greater than the denominator`, () => {
expect(isProperFraction(2, 2)).toEqual(false);
expect(isProperFraction(5, 4)).toEqual(false);
});

// Proper fractions with negative values
test(`should return true when the absolute value of the numerator is smaller than the absolute value of the denominator`, () => {
expect(isProperFraction(-1, 2)).toEqual(true);
expect(isProperFraction(1, -2)).toEqual(true);
});
Comment on lines +21 to +24
Copy link
Contributor

Choose a reason for hiding this comment

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

This category probably cover the positive case, and you can use notations like | ... | or abs(...) in the description to make it more concise.


// Improper fractions with negative values
test(`should return false when the absolute value of the numerator is equal to or greater than the absolute value of the denominator`, () => {
expect(isProperFraction(-3, 2)).toEqual(false);
expect(isProperFraction(3, -2)).toEqual(false);
expect(isProperFraction(-2, -2)).toEqual(false);
});

// Special case: numerator is zero
test(`should return true when numerator is zero and denominator is not zero`, () => {
expect(isProperFraction(0, 5)).toEqual(true);
expect(isProperFraction(0, -5)).toEqual(true);
});
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
// This statement loads the getCardValue function you wrote in the implement directory.
// We will use the same function, but write tests for it using Jest in this file.
const getCardValue = require("../implement/3-get-card-value");

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

// Case 1: Ace (A)
test(`Should return 11 when given an ace card`, () => {
test(`should return 11 when given an ace card`, () => {
expect(getCardValue("A♠")).toEqual(11);
expect(getCardValue("A♥")).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
test(`should return the numeric value for number cards`, () => {
expect(getCardValue("2♥")).toEqual(2);
expect(getCardValue("7♣")).toEqual(7);
expect(getCardValue("10♦")).toEqual(10);
});

// 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
// Case 3: Face cards
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 cards`, () => {
expect(() => getCardValue("invalid")).toThrow();
expect(() => getCardValue("1♠")).toThrow();
expect(() => getCardValue("A")).toThrow();
expect(() => getCardValue("A★")).toThrow();
});