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
@@ -1,33 +1,70 @@
// Implement a function isProperFraction,
// when given two numbers, a numerator and a denominator, it should return true if
// the given numbers form a proper fraction, and false otherwise.
// Implement a function isProperFraction.
// When given two numbers, a numerator and a denominator, it should return true
// if the given numbers form a proper fraction, and false otherwise.

// Assumption: The parameters are valid numbers (not NaN or Infinity).

// Note: If you are unfamiliar with proper fractions, please look up its mathematical definition.
// Definition:
// A proper fraction is a fraction where:
// - the denominator is not zero
// - both numbers are non-negative
// - the numerator is smaller than the denominator
Comment thread
cjyuan marked this conversation as resolved.
Outdated

// Acceptance criteria:
// After you have implemented the function, write tests to cover all the cases, and
// execute the code to ensure all tests pass.
// After implementing the function, write tests to cover all cases
// and run 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;
}

// Negative values are not allowed
if (numerator < 0 || denominator < 0) {
return false;
}

// A proper fraction must have numerator smaller than denominator
if (numerator < denominator) {
return true;
}

// All other cases are not proper fractions
return false;
}

// 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.
module.exports = isProperFraction;

// Here's our helper again
// Helper function for simple assertions in this file
function assertEquals(actualOutput, targetOutput) {
console.assert(
actualOutput === targetOutput,
`Expected ${actualOutput} to equal ${targetOutput}`
);
}

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

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

// Proper fractions (numerator smaller than denominator)
assertEquals(isProperFraction(3, 5), true);
assertEquals(isProperFraction(2, 7), true);

// Improper fractions (numerator greater than or equal to denominator)
assertEquals(isProperFraction(5, 5), false);
assertEquals(isProperFraction(7, 3), false);

// Negative numbers should return false
assertEquals(isProperFraction(-2, 7), false);
assertEquals(isProperFraction(2, -7), false);

// Zero numerator is allowed if denominator is positive
assertEquals(isProperFraction(0, 5), true);

// Invalid fraction (denominator is zero)
assertEquals(isProperFraction(2, 0), 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 @@ -14,7 +14,32 @@ 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(270)).toEqual("Reflex angle");
expect(getAngleType(359)).toEqual("Reflex angle");
});

// Case 6: Invalid angles
test(`should return "Invalid angle" for angles outside valid range`, () => {
expect(getAngleType(0)).toEqual("Invalid angle");
expect(getAngleType(360)).toEqual("Invalid angle");
expect(getAngleType(-10)).toEqual("Invalid angle");
});
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.

You could make this description more informative by indicating what this "valid range" is.

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.

I removed the unsupported definition wording in implement/2-is-proper-fraction.js and kept the comments focused on the behavior implemented in the function.

I also updated the Jest description in rewrite-tests-with-jest/1-get-angle-type.test.js so that the invalid range is now stated explicitly.

I pushed the changes to the same branch.

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
Copy Markdown
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