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,9 +15,23 @@
// 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) {
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";
}
}

// TODO: Implement this function

// The line below allows us to load the getAngleType function into tests in other files.
// This will be useful in the "rewrite tests with jest" step.
module.exports = getAngleType;
Expand All @@ -35,3 +49,24 @@ function assertEquals(actualOutput, targetOutput) {
// Example: Identify Right Angles
const right = getAngleType(90);
assertEquals(right, "Right angle");

const acute = getAngleType(45);
assertEquals(acute, "Acute angle");

const obtuse = getAngleType(120);
assertEquals(obtuse, "Obtuse angle");

const straight = getAngleType(180);
assertEquals(straight, "Straight angle");

const Reflex = getAngleType(200);
assertEquals(Reflex, "Reflex angle");

const invalid1 = getAngleType(0);
assertEquals(invalid1, "Invalid angle");

const invalid2 = getAngleType(360);
assertEquals(invalid2, "Invalid angle");

const invalid3 = getAngleType(-10);
assertEquals(invalid3, "Invalid angle");
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@
// execute the code to ensure all tests pass.

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

// The line below allows us to load the isProperFraction function into tests in other files.
// This will be useful in the "rewrite tests with jest" step.
Expand All @@ -31,3 +36,13 @@ function assertEquals(actualOutput, targetOutput) {

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

assertEquals(isProperFraction(3, 2), false);

assertEquals(isProperFraction(2, 2), false);

assertEquals(isProperFraction(-1, 2), true);

assertEquals(isProperFraction(1, -2), true);

assertEquals(isProperFraction(-1, -2), true);
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,23 @@
// execute the code to ensure all tests pass.

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

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

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

if (rank === "A") return 11;
if (["K", "Q", "J"].includes(rank)) return 10;
if (["2", "3", "4", "5", "6", "7", "8", "9", "10"].includes(rank))
return Number(rank);

throw new Error("invalid card rank.");
}
// TODO: Implement this function

// The line below allows us to load the getCardValue function into tests in other files.
// This will be useful in the "rewrite tests with jest" step.
Expand All @@ -40,6 +55,11 @@ 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("Q♦"), 10);
assertEquals(getCardValue("K♠"), 10);
assertEquals(getCardValue("10♣"), 10);

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

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

// What other invalid card cases can you think of?
assertEquals(getCardValue("2♦"), 2);
assertEquals(getCardValue("5♥"), 5);
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,30 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => {
});

// Case 2: Right angle
test(`should return "Right angle" when angle is exactly 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(120)).toEqual("Obtuse angle");
expect(getAngleType(179)).toEqual("Obtuse angle");
});
// Case 4: Straight angle
test(`should return "Straight angle" when angle is exactly 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(200)).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(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,21 @@ 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 false when numerator is greater than denominator", () => {
expect(isProperFraction(5, 2)).toEqual(false);
});
test("should return true when |numerator| < |denominator|", () => {
expect(isProperFraction(-4, 7)).toEqual(true);
});
test("should return false when numerator equals denominator", () => {
expect(isProperFraction(3, 3)).toEqual(false);
});
test("should return true when numerator < denominator and denominator is negative", () => {
expect(isProperFraction(4, -7)).toEqual(true);
});
test("should return true when numerator and denominator are both negative and numerator < denominator", () => {
expect(isProperFraction(-4, -7)).toEqual(true);
});
Comment on lines +20 to +25
Copy link
Contributor

Choose a reason for hiding this comment

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

Could also use the absolute value notation on these descriptions.

In math, 4 is larger than -7, and -4 is larger than -7. The absolute value notation could help make the description concise.

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for the suggestion, I’ll keep this in mind for future tests.

test("should return true when numerator is zero and denominator is negative", () => {
expect(isProperFraction(0, -5)).toEqual(true);
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,22 @@ test(`Should return 11 when given an ace card`, () => {
// Number Cards (2-10)
// Face Cards (J, Q, K)
// Invalid Cards

test("should return the value of number cards", () => {
expect(getCardValue("2♣")).toEqual(2);
expect(getCardValue("5♠")).toEqual(5);
expect(getCardValue("10♦")).toEqual(10);
});
test("should return 10 for face cards", () => {
expect(getCardValue("J♣")).toEqual(10);
expect(getCardValue("Q♥")).toEqual(10);
expect(getCardValue("K♠")).toEqual(10);
});
test("should throw an error for invalid cards", () => {
expect(() => getCardValue("1♣")).toThrow("invalid card rank.");
expect(() => getCardValue("AX")).toThrow();
expect(() => getCardValue("K")).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