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 @@ -16,6 +16,18 @@

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

// The line below allows us to load the getAngleType function into tests in other files.
Expand All @@ -33,5 +45,29 @@ function assertEquals(actualOutput, 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");
// const right = getAngleType(90);
// Acute: (0, 90)
assertEquals(getAngleType(1), "Acute angle");
assertEquals(getAngleType(89), "Acute angle");

// Right: 90
assertEquals(getAngleType(90), "Right angle");

// Obtuse: (90, 180)
assertEquals(getAngleType(91), "Obtuse angle");
assertEquals(getAngleType(179), "Obtuse angle");

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

// Reflex: (180, 360)
assertEquals(getAngleType(181), "Reflex angle");
assertEquals(getAngleType(359), "Reflex angle");

// Invalid: outside valid range
assertEquals(getAngleType(0), "Invalid angle");
assertEquals(getAngleType(-10), "Invalid angle");
assertEquals(getAngleType(360), "Invalid angle");
assertEquals(getAngleType(361), "Invalid angle");

console.log("All tests passed!");
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@

function isProperFraction(numerator, denominator) {
// TODO: Implement this function
if (denominator === 0) return false;
if (denominator < 0) return false;

return Math.abs(numerator) < denominator;
}

// The line below allows us to load the isProperFraction function into tests in other files.
Expand All @@ -31,3 +35,24 @@ function assertEquals(actualOutput, targetOutput) {

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

// Improper fractions (numerator > denominator)
assertEquals(isProperFraction(2, 1), false);
assertEquals(isProperFraction(5, 4), false);

// Equal numerator/denominator is not proper
assertEquals(isProperFraction(5, 5), false);

// Zero numerator:
assertEquals(isProperFraction(0, 3), true);

// Negative numerator:
assertEquals(isProperFraction(-1, 2), true);
assertEquals(isProperFraction(-3, 2), false);

// Denominator edge cases
assertEquals(isProperFraction(1, 0), false);
assertEquals(isProperFraction(1, -2), false);
assertEquals(isProperFraction(-1, -2), false);

console.log ("All tests passed!")
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,21 @@
// 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
// Validate input type
if (typeof card !== "string") {
throw new Error("Card must be a string");
}
const validSuits = ["♠", "♥", "♦", "♣"];
const validRanks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"];
const suit = card.slice(-1);
const rank = card.slice(0, card.length - 1);
if (!validSuits.includes(suit) || !validRanks.includes(rank)) {
throw new Error(`Invalid card format: ${card}`);
}
if (rank === "A") return 11;
if (["J", "Q", "K"].includes(rank)) return 10;
return parseInt(rank, 10);
}

// The line below allows us to load the getCardValue function into tests in other files.
Expand All @@ -39,8 +51,13 @@ function assertEquals(actualOutput, targetOutput) {

// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
// Examples:
assertEquals(getCardValue("A♠"), 11);
assertEquals(getCardValue("2♥"), 2);
assertEquals(getCardValue("10♦"), 10);
assertEquals(getCardValue("J♣"), 10);
assertEquals(getCardValue("Q♠"), 10);
assertEquals(getCardValue("K♥"), 10);
assertEquals(getCardValue("9♠"), 9);

// Handling invalid cards
try {
getCardValue("invalid");
Expand All @@ -50,3 +67,11 @@ try {
} catch (e) {}

// What other invalid card cases can you think of?
// "invalid", / not following format
// "1♠", invalid rank
// "11♣", invalid rank
// "A", missing suit
// "♠", missing rank
// "A♦♦", invalid format
// 5, not a string
// "", empty string
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,44 @@ const getAngleType = require("../implement/1-get-angle-type");

// 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
test(`should return "Right angle" when (angle = 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 = 180)`, () => {
expect(getAngleType(180)).toEqual("Straight angle");
});

// Case 5: Reflex angles
// Case 6: Invalid 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 7: Invalid angles
test(`should return "Invalid angle" when the angle is 0, negative, or over 360`, () => {
expect(getAngleType(0)).toEqual("Invalid angle");
expect(getAngleType(-10)).toEqual("Invalid angle");
expect(getAngleType(361)).toEqual("Invalid angle");
expect(getAngleType(720)).toEqual("Invalid angle");
expect(getAngleType(360)).toEqual("Invalid angle");

});


Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,45 @@
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", () => {
// Denominator zero case
test("should return false when denominator is zero", () => {
expect(isProperFraction(1, 0)).toBe(false);
});

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

// Proper positive fraction (numerator < denominator)
test("should return true for positive proper fractions", () => {
expect(isProperFraction(1, 2)).toBe(true);
expect(isProperFraction(3, 4)).toBe(true);
});

// Improper positive fraction (numerator >= denominator)
test("should return false for improper positive fractions", () => {
expect(isProperFraction(4, 3)).toBe(false);
expect(isProperFraction(5, 5)).toBe(false);
});

// Negative numerators (proper fraction)
test("should return true for negative proper fractions", () => {
expect(isProperFraction(-1, 3)).toBe(true);
});

// Negative numerators that make fraction improper
test("should return false for negative improper fractions", () => {
expect(isProperFraction(-5, 5)).toBe(false);
});

// Mixed sign case (denominator positive, numerator positive/negative)
test("should handle mixed signs correctly", () => {
expect(isProperFraction(2, 3)).toBe(true);
expect(isProperFraction(-2, 3)).toBe(true);
expect(isProperFraction(4, 3)).toBe(false);
expect(isProperFraction(-4, 3)).toBe(false);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,64 @@
// 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);
});


// Suggestion: Group the remaining test data into these categories:
// Number Cards (2-10)
// Face Cards (J, Q, K)
// Invalid Cards


// 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
// [https://jestjs.io/docs/expect#tothrowerror](https://jestjs.io/docs/expect#tothrowerror)


// Number Cards (2–10)
test(`Should return 2 when given "2♦"`, () => {
expect(getCardValue("2♦")).toEqual(2);
});

test(`Should return 5 when given "5♣"`, () => {
expect(getCardValue("5♣")).toEqual(5);
});

test(`Should return 10 when given "10♥"`, () => {
expect(getCardValue("10♥")).toEqual(10);
});


// Face Cards (J, Q, K)
test(`Should return 10 when given "J♣"`, () => {
expect(getCardValue("J♣")).toEqual(10);
});

test(`Should return 10 when given "Q♦"`, () => {
expect(getCardValue("Q♦")).toEqual(10);
});

test(`Should return 10 when given "K♥"`, () => {
expect(getCardValue("K♥")).toEqual(10);
});


// Invalid Cards
test(`Should throw an error when given an invalid card symbol like "Z♠"`, () => {
expect(() => getCardValue("Z♠")).toThrowError();
});

test(`Should throw an error when given an empty string`, () => {
expect(() => getCardValue("")).toThrowError();
});

test(`Should throw an error when given null`, () => {
expect(() => getCardValue(null)).toThrowError();
});