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

function getAngleType(angle) {
// TODO: Implement this function
if (angle > 0 && angle < 90) {
return "Acute angle";
}
if (angle == 90) {
return "Right angle";
}
if (angle > 90 && angle < 180) {
return "Obtuse angle";
}
if (angle == 180) {
return "Straight angle";
}
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 @@ -33,5 +47,23 @@ 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");

assertEquals(getAngleType(-1), "Invalid angle");
assertEquals(getAngleType(0), "Invalid angle");
assertEquals(getAngleType(360), "Invalid angle");
assertEquals(getAngleType(10000), "Invalid angle");

assertEquals(getAngleType(1), "Acute angle");
assertEquals(getAngleType(45), "Acute angle");
assertEquals(getAngleType(89.99), "Acute angle");
assertEquals(getAngleType(45), "Acute angle");

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

assertEquals(getAngleType(91), "Obtuse angle");
assertEquals(getAngleType(179), "Obtuse angle");

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

assertEquals(getAngleType(181), "Reflex angle");
assertEquals(getAngleType(359), "Reflex angle");
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// execute the code to ensure all tests pass.

function isProperFraction(numerator, denominator) {
// TODO: Implement this function
return Math.abs(numerator) < Math.abs(denominator);
}

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

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

assertEquals(isProperFraction(2, 1), false);
assertEquals(isProperFraction(2, 0.1), false);
assertEquals(isProperFraction(2, -1), false);
assertEquals(isProperFraction(-2, 1), false);
assertEquals(isProperFraction(200000, 100000), false);
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,24 @@
// execute the code to ensure all tests pass.

function getCardValue(card) {
// TODO: Implement this function
const suits = ["♠", "♥", "♦", "♣"];
if (!suits.includes(card.slice(-1))) {
throw new Error("Invalid card");
}

const value = card.slice(0, -1);
if (value == 10) {
return 10;
}
if (value >= 2 && value <= 9) {
return Number(value);
}
if (value == "J" || value == "Q" || value == "K") {
return 10;
}
if (value == "A") {
return 11;
} else throw new Error("Invalid card");
}

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

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

// Handling invalid cards
try {
Expand All @@ -52,3 +75,8 @@ try {
}

// What other invalid card cases can you think of?
// decimal numbers
// cards without suits
// numbers above 10
// negative numbers
// letters outside J K Q and A
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,43 @@ 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");
expect(getAngleType(89.99)).toEqual("Acute angle");
});

// Case 2: Right angle
test(`should return "Right angle" when angle == 90)`, () => {
// Test right angle
expect(getAngleType(90)).toEqual("Right angle");
});

// Case 3: Obtuse angles
test(`should return "Obtuse angle" when (90 < angle < 180)`, () => {
// Test various obtuse angles, including boundary cases
expect(getAngleType(90.01)).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 == 180)`, () => {
// Test straight angle
expect(getAngleType(180)).toEqual("Straight angle");
});

// Case 5: Reflex angles
test(`should return "Reflex Angle" when (180 < angle < 360)`, () => {
// Test various reflex angles, including boundary cases
expect(getAngleType(181)).toEqual("Reflex angle");
expect(getAngleType(270)).toEqual("Reflex angle");
expect(getAngleType(359.99)).toEqual("Reflex angle");
});

// Case 6: Invalid angles
test(`should return "Invalid Angle" when invalid`, () => {
// Test various invalid angles
expect(getAngleType(-1)).toEqual("Invalid angle");
expect(getAngleType(0)).toEqual("Invalid angle");
expect(getAngleType(360)).toEqual("Invalid angle");
expect(getAngleType(10000)).toEqual("Invalid angle");
expect(getAngleType("Im not an angle")).toEqual("Invalid angle");
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,52 @@ 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`, () => {
test("should return true when the numerator is less than the denominator (1 and 2)", () => {
expect(isProperFraction(1, 2)).toEqual(true);
});

test("should return true when the numerator is less than the denominator using decimals (0.1 and 2)", () => {
expect(isProperFraction(0.1, 2)).toEqual(true);
});

test("should return true when the numerator is less than the denominator and numerator is negative (-1 and 2)", () => {
expect(isProperFraction(-1, 2)).toEqual(true);
});

test("should return true when the numerator is less than the denominator and denominator is negative (1 and -2)", () => {
expect(isProperFraction(1, -2)).toEqual(true);
});

test("should return true for large numbers when numerator is less than denominator (1,000,000 and 2,000,000)", () => {
expect(isProperFraction(1000000, 2000000)).toEqual(true);
});

test("should return false when the numerator is greater than the denominator (2 and 1)", () => {
expect(isProperFraction(2, 1)).toEqual(false);
});

test("should return false when the numerator is larger than the denominator with decimals (2 and 0.1)", () => {
expect(isProperFraction(2, 0.1)).toEqual(false);
});

test("should return false when the numerator is larger than the denominator (2 and -1)", () => {
expect(isProperFraction(2, -1)).toEqual(false);
});

test("should return false when the numerator is larger than the denominator (-2 and 1)", () => {
expect(isProperFraction(-2, 1)).toEqual(false);
});

test("should return false for large numbers when the numerator is larger than the denominator (2,000,000 and 1,000,000)", () => {
expect(isProperFraction(2000000, 1000000)).toEqual(false);
});

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

// Special case: numerator is string
test("should return false when denominator is zero", () => {
expect(isProperFraction("hello", 0)).toEqual(false);
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,75 @@ 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)
// Number Cards (2-10)
test(`Should return 2 when given a "2♠" card`, () => {
expect(getCardValue("2♠")).toEqual(2);
});

test(`Should return 9 when given a "9♠" card`, () => {
expect(getCardValue("9♠")).toEqual(9);
});

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

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

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

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

// Invalid Cards
test(`Should return error when given a string`, () => {
expect(() => {
getCardValue("invalid");
}).toThrow("Invalid card");
});

test(`Should return error when given a wrong letter`, () => {
expect(() => {
getCardValue("L♦");
}).toThrow("Invalid card");
});

test(`Should return error when given a wrong letter`, () => {
expect(() => {
getCardValue("L♦");
}).toThrow("Invalid card");
});

test(`Should return error when given a wrong number`, () => {
expect(() => {
getCardValue("11♦");
}).toThrow("Invalid card");
});

test(`Should return error when given a wrong order`, () => {
expect(() => {
getCardValue("♦2");
}).toThrow("Invalid card");
});

test(`Should return error when given no suit`, () => {
expect(() => {
getCardValue("3");
}).toThrow("Invalid card");
});

test(`Should return error when given only suit`, () => {
expect(() => {
getCardValue("♦");
}).toThrow("Invalid card");
});

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