Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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
@@ -1,37 +1,27 @@
// Implement a function getAngleType
//
// When given an angle in degrees, it should return a string indicating the type of angle:
// - "Acute angle" for angles greater than 0° and less than 90°
// - "Right angle" for exactly 90°
// - "Obtuse angle" for angles greater than 90° and less than 180°
// - "Straight angle" for exactly 180°
// - "Reflex angle" for angles greater than 180° and less than 360°
// - "Invalid angle" for angles outside the valid range.
function getAngleType(angle) {
if (angle <= 0 || angle >= 360) {
return "Invalid angle";
}

// Assumption: The parameter is a valid number. (You do not need to handle non-numeric inputs.)
if (angle > 0 && angle < 90) {
return "Acute angle";
}

// Acceptance criteria:
// After you have implemented the function, write tests to cover all the cases, and
// execute the code to ensure all tests pass.
if (angle === 90) {
return "Right angle";
}

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

// 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}`
);
if (angle > 180 && angle < 360) {
return "Reflex angle";
}
}

// TODO: Write tests to cover all cases, including boundary and invalid cases.
// Example: Identify Right Angles
const right = getAngleType(90);
assertEquals(right, "Right angle");
module.exports = getAngleType;
Original file line number Diff line number Diff line change
@@ -1,33 +1,9 @@
// 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
}

// 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;
if (denominator === 0) {
return false;
}

// Here's our helper again
function assertEquals(actualOutput, targetOutput) {
console.assert(
actualOutput === targetOutput,
`Expected ${actualOutput} to equal ${targetOutput}`
);
return Math.abs(numerator) < Math.abs(denominator);
}

// 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);
module.exports = isProperFraction;
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
// This problem involves playing cards: https://en.wikipedia.org/wiki/Standard_52-card_deck
function getCardValue(card) {
const suits = ["♠", "♥", "♦", "♣"];

// Implement a function getCardValue, when given a string representing a playing card,
// should return the numerical value of the card.
if (typeof card !== "string" || card.length < 2) {
throw new Error("Invalid 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♦".
const suit = card.slice(-1);
const rank = card.slice(0, -1);

// 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.
if (!suits.includes(suit)) {
throw new Error("Invalid card");
}

// When the card string is invalid (not following the above format), the function should
// throw an error.
// Ace
if (rank === "A") return 11;

// Acceptance criteria:
// After you have implemented the function, write tests to cover all the cases, and
// execute the code to ensure all tests pass.
// Face cards
if (["J", "Q", "K"].includes(rank)) return 10;

function getCardValue(card) {
// TODO: Implement this function
// Number cards (STRICT check)
const validNumbers = ["2", "3", "4", "5", "6", "7", "8", "9", "10"];

if (validNumbers.includes(rank)) {
return Number(rank);
}

// Everything else = invalid
throw new Error("Invalid card");
}

<<<<<<< HEAD
module.exports = getCardValue;
=======
// 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;
Expand All @@ -41,6 +48,18 @@ function assertEquals(actualOutput, targetOutput) {
// Examples:
assertEquals(getCardValue("9♠"), 9);

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

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

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

// Handling invalid cards
try {
getCardValue("invalid");
Expand All @@ -51,4 +70,16 @@ try {
console.log("Error thrown for invalid card 🎉");
}

// More invalid cases
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) {}

// What other invalid card cases can you think of?
>>>>>>> 983743526bc0a40a31b8e20926f23fa3ceb5cf29
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.

Lines 32, 34, 85 are merge conflict markers.

Lines 33 is from one source and lines 35 to 84 are from another source. You need to

  1. Decide which lines of code you want to keep.
  2. Delete the markers (on the lines I mentioned).

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.
describe("getAngleType", () => {
test("returns Acute angle", () => {
expect(getAngleType(1)).toBe("Acute angle");
expect(getAngleType(45)).toBe("Acute angle");
expect(getAngleType(89)).toBe("Acute angle");
});

// 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");
});
test("returns Right angle", () => {
expect(getAngleType(90)).toBe("Right angle");
});

// Case 2: Right angle
// Case 3: Obtuse angles
// Case 4: Straight angle
// Case 5: Reflex angles
// Case 6: Invalid angles
test("returns Obtuse angle", () => {
expect(getAngleType(100)).toBe("Obtuse angle");
expect(getAngleType(179)).toBe("Obtuse angle");
});

test("returns Straight angle", () => {
expect(getAngleType(180)).toBe("Straight angle");
});

test("returns Reflex angle", () => {
expect(getAngleType(200)).toBe("Reflex angle");
expect(getAngleType(359)).toBe("Reflex angle");
});

test("returns Invalid angle", () => {
expect(getAngleType(0)).toBe("Invalid angle");
expect(getAngleType(360)).toBe("Invalid angle");
expect(getAngleType(-10)).toBe("Invalid angle");
});
});
Original file line number Diff line number Diff line change
@@ -1,10 +1,30 @@
// 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.
describe("isProperFraction", () => {
test("returns true when numerator is smaller than denominator", () => {
expect(isProperFraction(1, 2)).toBe(true);
expect(isProperFraction(3, 4)).toBe(true);
});

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

test("returns false when numerator is larger than denominator", () => {
expect(isProperFraction(7, 3)).toBe(false);
});

test("returns true when numerator is zero", () => {
expect(isProperFraction(0, 5)).toBe(true);
});

test("returns false when denominator is zero", () => {
expect(isProperFraction(1, 0)).toBe(false);
});

test("works with negative numbers", () => {
expect(isProperFraction(-1, 2)).toBe(true);
expect(isProperFraction(1, -2)).toBe(true);
expect(isProperFraction(-3, -2)).toBe(false);
});
});
Original file line number Diff line number Diff line change
@@ -1,20 +1,44 @@
// 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.
describe("getCardValue", () => {
// Ace
test("returns 11 for Ace", () => {
expect(getCardValue("A♠")).toBe(11);
});

// Case 1: Ace (A)
test(`Should return 11 when given an ace card`, () => {
expect(getCardValue("A♠")).toEqual(11);
});
// Face cards
test("returns 10 for face cards", () => {
expect(getCardValue("J♥")).toBe(10);
expect(getCardValue("Q♦")).toBe(10);
expect(getCardValue("K♣")).toBe(10);
});

// Suggestion: Group the remaining test data into these categories:
// Number Cards (2-10)
// Face Cards (J, Q, K)
// Invalid Cards
// Number cards
test("returns correct value for number cards", () => {
expect(getCardValue("2♠")).toBe(2);
expect(getCardValue("10♥")).toBe(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 format
test("throws error for invalid strings", () => {
expect(() => getCardValue("invalid")).toThrow();
expect(() => getCardValue("A")).toThrow();
});

// Invalid numbers (REVIEWER CHECK 🔥)
test("throws error for invalid numeric formats", () => {
expect(() => getCardValue("0x02♠")).toThrow();
expect(() => getCardValue("2.1♠")).toThrow();
expect(() => getCardValue("0002♠")).toThrow();
});

// Invalid rank
test("throws error for invalid rank", () => {
expect(() => getCardValue("11♠")).toThrow();
});

// Invalid suit
test("throws error for invalid suit", () => {
expect(() => getCardValue("A?")).toThrow();
});
});
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"keywords": [],
"author": "Code Your Future",
"license": "ISC",
"dependencies": {
"jest": "^29.7.0"
"devDependencies": {
"jest": "^30.2.0"
}
}
}
Loading