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

function getAngleType(angle) {
// TODO: Implement this function
// Check invalid angles first
if (angle <= 0 || angle >= 360) {
return "Invalid angle";
}

// Acute angle
if (angle > 0 && angle < 90) {
return "Acute angle";
}

// Right angle
if (angle === 90) {
return "Right angle";
}

// Obtuse angle
if (angle > 90 && angle < 180) {
return "Obtuse angle";
}

// Straight angle
if (angle === 180) {
return "Straight angle";
}

// Reflex angle
if (angle > 180 && angle < 360) {
return "Reflex angle";
}
}

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

// Acute angle tests
assertEquals(getAngleType(30), "Acute angle");
assertEquals(getAngleType(1), "Acute angle");

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

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

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

// Invalid angle tests
assertEquals(getAngleType(0), "Invalid angle");
assertEquals(getAngleType(360), "Invalid angle");
assertEquals(getAngleType(-20), "Invalid angle");
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,22 @@
// execute the code to ensure all tests pass.

function isProperFraction(numerator, denominator) {
// TODO: Implement this function
// A fraction with denominator 0 is invalid.
if (denominator === 0) {
return false;
}

// For this implementation, proper fractions are positive fractions
// where the numerator is smaller than the denominator.
if (numerator < 0 || denominator < 0) {
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 @@ -31,3 +46,16 @@ function assertEquals(actualOutput, targetOutput) {

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

// Proper fractions
assertEquals(isProperFraction(3, 5), true);
assertEquals(isProperFraction(0, 5), true);

// Improper fractions
assertEquals(isProperFraction(5, 5), false);
assertEquals(isProperFraction(7, 3), false);

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

function getCardValue(card) {
// TODO: Implement this function
const validSuits = ["♠", "♥", "♦", "♣"];
const validNumberRanks = ["2", "3", "4", "5", "6", "7", "8", "9", "10"];

if (typeof card !== "string" || card.length < 2) {
throw new Error("Invalid card");
}

const suit = card.slice(-1);
const rank = card.slice(0, -1);

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

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

if (["J", "Q", "K"].includes(rank)) {
return 10;
}

if (validNumberRanks.includes(rank)) {
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,15 +64,57 @@ function assertEquals(actualOutput, targetOutput) {
}

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

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

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

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

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

// This line will not be reached if an error is thrown as expected
try {
getCardValue("1♠");
console.error("Error was not thrown for invalid card");
} catch (e) {}

// What other invalid card cases can you think of?
try {
getCardValue("11♠");
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("♠");
console.error("Error was not thrown for invalid card");
} catch (e) {}

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

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

try {
getCardValue("0002♠");
console.error("Error was not thrown for invalid card");
} catch (e) {}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,40 @@ const getAngleType = require("../implement/1-get-angle-type");
// including boundary and invalid cases.

// Case 1: Acute angles
test(`should return "Acute angle" when (0 < angle < 90)`, () => {
test(`should return "Acute angle" when 0 < angle < 90`, () => {
// Test various acute angles, including boundary cases
expect(getAngleType(1)).toEqual("Acute angle");
expect(getAngleType(45)).toEqual("Acute angle");
expect(getAngleType(89)).toEqual("Acute angle");
});

// 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(270)).toEqual("Reflex angle");
expect(getAngleType(359)).toEqual("Reflex angle");
});

// Case 6: Invalid angles
test(`should return "Invalid angle" for angles less than or equal to 0, or greater than or equal to 360`, () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

It is ok to use pseudo-code in the description if they can make the description more concise.

For example, ... when angle <= 0 or angle >= 360

expect(getAngleType(0)).toEqual("Invalid angle");
expect(getAngleType(360)).toEqual("Invalid angle");
expect(getAngleType(-10)).toEqual("Invalid angle");
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,39 @@
// 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.
// These tests cover different combinations of values such as:
// positive numbers, negative numbers, zeros, and improper fractions.

// Special case: numerator is zero
test(`should return false when denominator is zero`, () => {
expect(isProperFraction(1, 0)).toEqual(false);
describe("isProperFraction", () => {
// Special case: denominator is zero
test("should return false when denominator is zero", () => {
expect(isProperFraction(1, 0)).toEqual(false);
});

// Special case: numerator is zero
test("should return true when numerator is zero and denominator is positive", () => {
expect(isProperFraction(0, 5)).toEqual(true);
});

// Proper fraction: numerator is positive and less than denominator
test("should return true for a proper fraction", () => {
expect(isProperFraction(1, 2)).toEqual(true);
expect(isProperFraction(3, 4)).toEqual(true);
});
Comment on lines +20 to +23
Copy link
Contributor

Choose a reason for hiding this comment

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

Would be more informative to indicate the conditions
"numerator >= denominator, both values non-negative"
in the description.

This way, the person implementing the function don't have to lookup the definition of "proper fraction" (which they may find a different definition).


// Improper fraction: numerator is greater than or equal to denominator
test("should return false for an improper fraction", () => {
expect(isProperFraction(5, 4)).toEqual(false);
expect(isProperFraction(4, 4)).toEqual(false);
});

// Negative numerator
test("should return false when numerator is negative", () => {
expect(isProperFraction(-1, 2)).toEqual(false);
});

// Negative denominator
test("should return false when denominator is negative", () => {
expect(isProperFraction(1, -2)).toEqual(false);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,32 @@ test(`Should return 11 when given an ace card`, () => {
// please refer to the Jest documentation:
// https://jestjs.io/docs/expect#tothrowerror

// Case 2: Number cards
test("should return the numeric value when given a valid number card", () => {
expect(getCardValue("2♠")).toEqual(2);
expect(getCardValue("9♠")).toEqual(9);
expect(getCardValue("10♥")).toEqual(10);
});

// Case 3: Face cards
test("should return 10 when given a face card", () => {
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 strings", () => {
expect(() => getCardValue("invalid")).toThrow();
expect(() => getCardValue("1♠")).toThrow();
expect(() => getCardValue("11♠")).toThrow();
expect(() => getCardValue("A")).toThrow();
expect(() => getCardValue("♠")).toThrow();
});

// Case 5: Invalid numeric literal strings
test("should throw an error for strings that JavaScript can coerce to numbers but are not valid card ranks", () => {
expect(() => getCardValue("0x02♠")).toThrow();
expect(() => getCardValue("2.1♠")).toThrow();
expect(() => getCardValue("0002♠")).toThrow();
});
Loading