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,19 @@
// execute the code to ensure all tests pass.

function getAngleType(angle) {
// TODO: Implement this function
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";
} else {
return "Invalid angle";
}
}

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

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

// Test Obtuse angle
const obtuse = getAngleType(135);
assertEquals(obtuse, "Obtuse angle");

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

// Test Reflex angle
const reflex = getAngleType(270);
assertEquals(reflex, "Reflex angle");

// Test Invalid angles
const zero = getAngleType(0);
assertEquals(zero, "Invalid angle");

const negative = getAngleType(-45);
assertEquals(negative, "Invalid angle");

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

const over360 = getAngleType(400);
assertEquals(over360, "Invalid angle");
Original file line number Diff line number Diff line change
@@ -1,33 +1,31 @@
// 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.

// Acceptance criteria:
// After you have implemented the function, write tests to cover all the cases, and
// execute the code to ensure all tests pass.

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.
// This will be useful in the "rewrite tests with jest" step.
module.exports = isProperFraction;

// Here's our helper again
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?

// Example: 1/2 is a proper fraction
// Proper fractions
assertEquals(isProperFraction(1, 2), true);
assertEquals(isProperFraction(2, 3), true);
assertEquals(isProperFraction(7, 10), true);

Copy link

Choose a reason for hiding this comment

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

Great job, your current implementation treats every negative fraction as false, and your Jest tests are written to match that. But what is the actual rule for a “proper fraction”?

It might be worth checking whether your implementation is based on the maths definition, or whether the tests and implementation have just been made consistent with each other.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for pointing this out! I just checked the meaning of a proper fraction and realized that negative fractions can still be proper if the absolute value of the numerator is less than the absolute value of the denominator.

I’ve updated my implementation to use Math.abs() and adjusted the tests to include negative proper fractions as well.

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

// Negative fractions (still proper)
assertEquals(isProperFraction(-1, 2), true);
assertEquals(isProperFraction(1, -2), true);
assertEquals(isProperFraction(-3, -4), true);

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

function getCardValue(card) {
// TODO: Implement this function
if (typeof card !== 'string') {
throw new Error('Invalid card: must be a string');
}

const suits = ['♠', '♥', '♦', '♣'];
const ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'];

let rank, suit;
if (card.length === 2) {
rank = card[0];
suit = card[1];
} else if (card.length === 3 && card.startsWith('10')) {
rank = '10';
suit = card[2];
} else {
throw new Error('Invalid card format');
}

if (!suits.includes(suit)) {
throw new Error('Invalid suit');
}

if (!ranks.includes(rank)) {
throw new Error('Invalid rank');
}

if (rank === 'A') {
return 11;
} else if (['J', 'Q', 'K'].includes(rank)) {
return 10;
} else {
return parseInt(rank);
}
}

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

// Test all ranks
assertEquals(getCardValue("A♠"), 11);
assertEquals(getCardValue("2♥"), 2);
assertEquals(getCardValue("3♦"), 3);
assertEquals(getCardValue("4♣"), 4);
assertEquals(getCardValue("5♠"), 5);
assertEquals(getCardValue("6♥"), 6);
assertEquals(getCardValue("7♦"), 7);
assertEquals(getCardValue("8♣"), 8);
assertEquals(getCardValue("9♠"), 9);
assertEquals(getCardValue("10♥"), 10);
assertEquals(getCardValue("J♦"), 10);
assertEquals(getCardValue("Q♣"), 10);
assertEquals(getCardValue("K♠"), 10);

// Test different suits
assertEquals(getCardValue("A♥"), 11);
assertEquals(getCardValue("K♦"), 10);

// Handling invalid cards
try {
getCardValue("invalid");

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

// Test invalid rank
try {
Copy link

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’re thinking about invalid inputs as well as valid ones. One small improvement to think about: right now these checks only confirm that some error was thrown. Would it make the test more helpful if it also checked that the error explains the problem clearly?

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for the suggestion! It makes sense. I’ve just updated the tests so they also check the error message to ensure the error clearly explains the problem with the input.

getCardValue("1♠");
console.error("Error was not thrown for invalid rank");
} catch (e) { }

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

// Test wrong length
try {
getCardValue("A");
console.error("Error was not thrown for wrong length");
} catch (e) { }

// Test non-string
try {
getCardValue(123);
console.error("Error was not thrown for non-string");
} catch (e) { }

// What other invalid card cases can you think of?
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 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" for angles <= 0 or >= 360`, () => {
expect(getAngleType(0)).toEqual("Invalid angle");
expect(getAngleType(-1)).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,45 @@ const isProperFraction = require("../implement/2-is-proper-fraction");
test(`should return false when denominator is zero`, () => {
expect(isProperFraction(1, 0)).toEqual(false);
});

// Positive proper fractions
test(`should return true for positive proper fractions`, () => {
expect(isProperFraction(1, 2)).toEqual(true);
expect(isProperFraction(2, 3)).toEqual(true);
expect(isProperFraction(7, 10)).toEqual(true);
});

// Positive improper fractions
test(`should return false for positive improper fractions`, () => {
expect(isProperFraction(3, 2)).toEqual(false);
expect(isProperFraction(5, 5)).toEqual(false);
expect(isProperFraction(10, 3)).toEqual(false);
});

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

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

// Zero numerator
test(`should return false when numerator is zero`, () => {
expect(isProperFraction(0, 2)).toEqual(false);
expect(isProperFraction(0, -3)).toEqual(false);
});

// Both negative (improper)
test(`should return false for negative improper fractions`, () => {
expect(isProperFraction(-3, -2)).toEqual(false);
});

// Both negative (proper)
test(`should return false for negative proper fractions`, () => {
expect(isProperFraction(-1, -2)).toEqual(false);
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,41 @@ const getCardValue = require("../implement/3-get-card-value");
// Case 1: Ace (A)
test(`Should return 11 when given an ace card`, () => {
expect(getCardValue("A♠")).toEqual(11);
expect(getCardValue("A♥")).toEqual(11);
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
// Number Cards (2-10)
test(`Should return the numeric value for number cards`, () => {
expect(getCardValue("2♠")).toEqual(2);
expect(getCardValue("3♥")).toEqual(3);
expect(getCardValue("4♦")).toEqual(4);
expect(getCardValue("5♣")).toEqual(5);
expect(getCardValue("6♠")).toEqual(6);
expect(getCardValue("7♥")).toEqual(7);
expect(getCardValue("8♦")).toEqual(8);
expect(getCardValue("9♣")).toEqual(9);
expect(getCardValue("10♠")).toEqual(10);
});

// 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);
expect(getCardValue("J♠")).toEqual(10);
expect(getCardValue("Q♥")).toEqual(10);
expect(getCardValue("K♦")).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
// 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();
expect(() => getCardValue("")).toThrow();
expect(() => getCardValue(123)).toThrow();
});