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 @@ -8,30 +8,28 @@
// - "Reflex angle" for angles greater than 180° and less than 360°
// - "Invalid angle" for angles outside the valid range.

// Assumption: The parameter is a valid number. (You do not need to handle non-numeric inputs.)

// 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 getAngleType(angle) {
// TODO: Implement this function
if (angle <= 0 || angle >= 360) return "Invalid angle";
if (angle === 90) return "Right angle";
if (angle === 180) return "Straight angle";
if (angle < 90) return "Acute angle";
if (angle < 180) return "Obtuse angle";
return "Reflex angle";
}

// 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;

// This helper function is written to make our assertions easier to read.
// If the actual output matches the target output, the test will pass
function assertEquals(actualOutput, targetOutput) {
console.assert(
actualOutput === targetOutput,
`Expected ${actualOutput} to equal ${targetOutput}`
);
}

// TODO: Write tests to cover all cases, including boundary and invalid cases.
// Example: Identify Right Angles
const right = getAngleType(90);
assertEquals(right, "Right angle");
assertEquals(getAngleType(90), "Right angle");
assertEquals(getAngleType(45), "Acute angle");
assertEquals(getAngleType(135), "Obtuse angle");
assertEquals(getAngleType(180), "Straight angle");
assertEquals(getAngleType(270), "Reflex angle");
assertEquals(getAngleType(0), "Invalid angle");
assertEquals(getAngleType(360), "Invalid angle");
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,23 @@
// 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
assertEquals(isProperFraction(1, 2), true);
assertEquals(isProperFraction(2, 1), false);
assertEquals(isProperFraction(3, 3), false);
assertEquals(isProperFraction(-1, 2), true);
assertEquals(isProperFraction(1, -2), true);
assertEquals(isProperFraction(1, 0), false);
Original file line number Diff line number Diff line change
@@ -1,54 +1,39 @@
// This problem involves playing cards: https://en.wikipedia.org/wiki/Standard_52-card_deck

// Implement a function getCardValue, when given a string representing a playing card,
// should return the numerical value of the card.

// A valid card string will contain a rank followed by the suit.
// The rank can be one of the following strings:
// "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"
// The suit can be one of the following emojis:
// "♠", "♥", "♦", "♣"
// For example: "A♠", "2♥", "10♥", "J♣", "Q♦", "K♦".

// When the card is an ace ("A"), the function should return 11.
// When the card is a face card ("J", "Q", "K"), the function should return 10.
// When the card is a number card ("2" to "10"), the function should return its numeric value.

// When the card string is invalid (not following the above format), the function should
// throw an error.

// 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 getCardValue(card) {
// TODO: Implement this function
const suit = card.slice(-1);
const rank = card.slice(0, -1);
const validSuits = ["\u2660", "\u2665", "\u2666", "\u2663"];

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.

Why not just specify the suit characters as "♠", "♥", "♦", "♣"?

const validRanks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"];

if (!validSuits.includes(suit) || !validRanks.includes(rank)) {
throw new Error(`Invalid card: ${card}`);
}
if (rank === "A") return 11;
if (["J", "Q", "K"].includes(rank)) return 10;
return Number(rank);
}

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

// Helper functions to make our assertions easier to read.
function assertEquals(actualOutput, targetOutput) {
console.assert(
actualOutput === targetOutput,
`Expected ${actualOutput} to equal ${targetOutput}`
);
}

// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
// Examples:
assertEquals(getCardValue("9♠"), 9);
assertEquals(getCardValue("9\u2660"), 9);
assertEquals(getCardValue("A\u2660"), 11);
assertEquals(getCardValue("10\u2665"), 10);
assertEquals(getCardValue("J\u2663"), 10);
assertEquals(getCardValue("Q\u2666"), 10);
assertEquals(getCardValue("K\u2666"), 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 😢");
console.error("Error was not thrown for invalid card");
} catch (e) {
console.log("Error thrown for invalid card 🎉");
console.log("Error thrown for invalid card");
}

// What other invalid card cases can you think of?
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
// This statement loads the getAngleType function you wrote in the implement directory.
// We will use the same function, but write tests for it using Jest in this file.
const getAngleType = require("../implement/1-get-angle-type");

// TODO: Write tests in Jest syntax to cover all cases/outcomes,
// including boundary and invalid cases.

// Case 1: Acute angles
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
// Case 3: Obtuse angles
// Case 4: Straight angle
// Case 5: Reflex angles
// Case 6: Invalid angles
test(`should return "Right angle" for exactly 90`, () => {
expect(getAngleType(90)).toEqual("Right angle");
});

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");
});

test(`should return "Straight angle" for exactly 180`, () => {
expect(getAngleType(180)).toEqual("Straight angle");
});

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");
});

test(`should return "Invalid angle" for angles outside 0-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
@@ -1,10 +1,25 @@
// 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
test(`should return false when denominator is zero`, () => {
expect(isProperFraction(1, 0)).toEqual(false);
});

test(`should return true when numerator < denominator (positive)`, () => {
expect(isProperFraction(1, 2)).toEqual(true);
expect(isProperFraction(3, 7)).toEqual(true);
});

test(`should return false when numerator >= denominator`, () => {
expect(isProperFraction(2, 1)).toEqual(false);
expect(isProperFraction(3, 3)).toEqual(false);
});

test(`should handle negative numerator`, () => {
expect(isProperFraction(-1, 2)).toEqual(true);
expect(isProperFraction(-3, 2)).toEqual(false);
});

test(`should handle negative denominator`, () => {
expect(isProperFraction(1, -2)).toEqual(true);
expect(isProperFraction(3, -2)).toEqual(false);
});
Comment on lines +12 to +25

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.

Could consider using pseudo-code and notations like abs(...) or | ... | in the descriptions to make them more informative and concise.

Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
// 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`, () => {
expect(getCardValue("A♠")).toEqual(11);
expect(getCardValue("A\u2660")).toEqual(11);
expect(getCardValue("A\u2665")).toEqual(11);
});

// Suggestion: Group the remaining test data into these categories:
// Number Cards (2-10)
// Face Cards (J, Q, K)
// Invalid Cards
test(`Should return numeric value for number cards 2-10`, () => {
expect(getCardValue("2\u2665")).toEqual(2);
expect(getCardValue("5\u2666")).toEqual(5);
expect(getCardValue("10\u2665")).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
test(`Should return 10 for face cards J, Q, K`, () => {
expect(getCardValue("J\u2663")).toEqual(10);
expect(getCardValue("Q\u2666")).toEqual(10);
expect(getCardValue("K\u2660")).toEqual(10);
});

test(`Should throw an error for invalid cards`, () => {
expect(() => getCardValue("invalid")).toThrow();
expect(() => getCardValue("")).toThrow();
});
Loading