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 @@ -5,8 +5,43 @@ import { getAngleType } from "../implement/1-get-angle-type.js";

// TODO: Write tests to cover all cases, including boundary and invalid cases.
// Example: Identify Right Angles
test("Invalid value", () => {
assert.throws(
() => {
getAngleType("shirin");
},
{ message: "Input must be a number" }
);
});

test("Acute angle for angles greater than 0° and less than 90°", () => {
const message = getAngleType(70);
assert.equal(message, "Acute angle");
});

test("Right angle", () => {
const message = getAngleType(90);
assert.equal(message, "Right angle");
});

test("Obtuse angle for angles greater than 90° and less than 180°", () => {
const message = getAngleType(110);
assert.equal(message, "Obtuse angle");
});

test("Straight angle for exactly 180°", () => {
const message = getAngleType(180);
assert.equal(message, "Straight angle");
});

test("Reflex angle for angles greater than 180° and less than 360°", () => {
const message = getAngleType(260);
assert.equal(message, "Reflex angle");
});

test("Classifies right angles", () => {
const right = getAngleType(90);
assert.equal(right, "Right angle");
test("throws for invalid numeric angles", () => {
assert.throws(() => getAngleType(-10), { message: "Invalid angle" });
assert.throws(() => getAngleType(360), { message: "Invalid angle" });
assert.throws(() => getAngleType(0), { message: "Invalid angle" });
assert.throws(() => getAngleType(500), { message: "Invalid angle" });
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,50 @@ test("Basic proper fraction", () => {
// Example: 1/2 is a proper fraction
assert.equal(isProperFraction(1, 2), true);
});

test("improper fraction", () => {
// Example: 2/1 is a improper fraction
assert.equal(isProperFraction(2, 1), false);
});

test("Equal numerator and denominator", () => {
// Example: equal number
assert.equal(isProperFraction(5, 5), false);
});

test("zero numerator", () => {
// Example: zero numerator is a proper fraction?
assert.equal(isProperFraction(0, 6), true);
});

test("Negative numerator", () => {
// Example: negative numerator is a proper fraction?
assert.equal(isProperFraction(-3, 8), true);
assert.equal(isProperFraction(-9, 4), false);
});

test("Negative denominator", () => {
// Example: negative denominator is a proper fraction?
assert.equal(isProperFraction(2, -5), true);
});

test("Both negative", () => {
// Example: Both negative is a proper fraction?
assert.equal(isProperFraction(-3, -5), true);
assert.equal(isProperFraction(-5, -2), false);
});

test("Zero denominator", () => {
// Example: Zero denominator is a proper fraction?
assert.equal(isProperFraction(7, 0), false);
});

test("Zero numerator and negative denominator", () => {
// Example: Zero numerator and negative denominator is a proper fraction?
assert.equal(isProperFraction(0, -7), true);
});

test("Both Zero", () => {
// Example: Both Zero is a proper fraction?
assert.equal(isProperFraction(0, 0), false);
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,64 @@ test("Valid single-digit card", () => {
assert.equal(getCardValue("9♠"), 9);
});

test("Valid double-digit card", () => {
assert.equal(getCardValue("10♠"), 10);
});
test("Ace card", () => {
assert.equal(getCardValue("A♠"), 11);
assert.equal(getCardValue("A♣"), 11);
assert.equal(getCardValue("A♦"), 11);
});

test("Face card", () => {
assert.equal(getCardValue("J♠"), 10);
});

test("card is not a string", () => {
assert.throws(
() => {
getCardValue(9);
},
{ message: "Input must be a String" }
);
});

test("Arbitrary non-card string", () => {
assert.throws(() => getCardValue("invalid"), /Expected a number followed by a suit, but got "invalid"/, "Expected clear error");
assert.throws(
() => getCardValue("invalid"),
/Expected a number followed by a suit, but got invalid/,
"Expected clear error"
);

assert.throws(
() => getCardValue("1♠"),
/Expected a number followed by a suit, but got invalid/,
"Expected clear error"
);

assert.throws(
() => getCardValue("9X"),
/Expected a number followed by a suit, but got invalid/,
"Expected clear error"
);

assert.throws(
() => getCardValue("11♠"),
/Expected a number followed by a suit, but got invalid/,
"Expected clear error"
);

assert.throws(
() => getCardValue("extra"),
/Expected a number followed by a suit, but got invalid/,
"Expected clear error"
);

assert.throws(
() => getCardValue("08♠"),
/Expected a number followed by a suit, but got invalid/,
"Expected clear error"
);
});

// TODO: What other invalid card cases can you think of?
27 changes: 26 additions & 1 deletion implement-and-rewrite-tests/implement/1-get-angle-type.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Implement a function getAngleType
//

// Don't forget to write tests in implement-tests-with-node-test.
//
// When given an angle in degrees, it should return a string indicating the type of angle:
Expand All @@ -18,4 +18,29 @@

export function getAngleType(angle) {
// TODO: Implement this function
if (typeof angle != `number` || Number.isNaN(angle)) {
throw new Error("Input must be a number");
}

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

throw new Error("Invalid angle");
}
4 changes: 4 additions & 0 deletions implement-and-rewrite-tests/implement/2-is-proper-fraction.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@

export function isProperFraction(numerator, denominator) {
// TODO: Implement this function
if (denominator === 0) {
return false;
}
return Math.abs(numerator) < Math.abs(denominator);
}
34 changes: 34 additions & 0 deletions implement-and-rewrite-tests/implement/3-get-card-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,38 @@

export function getCardValue(card) {
// TODO: Implement this function
const validRanks = [
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"J",
"Q",
"K",
"A",
];
const validSuits = ["♠", "♥", "♦", "♣"];
if (typeof card != `string`) {
throw new Error("Input must be a String");
}
const rank = card.slice(0, -1);
const suit = card.slice(-1);

if (!validRanks.includes(rank) || !validSuits.includes(suit)) {
throw new Error("Expected a number followed by a suit, but got invalid");
}

if (rank === "A") {
return 11;
}

if (rank === "J" || rank === "Q" || rank === "K") {
return 10;
}
return Number(rank);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getAngleType } from "../implement/1-get-angle-type.js";

import { test, expect } from "@jest/globals";
// TODO: Write tests in Jest syntax to cover all cases/outcomes,
// including boundary and invalid cases.

Expand All @@ -12,7 +12,40 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => {
});

// Case 2: Right angle
test(`should return "Right angle" when (angle = 90)`, () => {
// Test various Right angles
expect(getAngleType(90)).toEqual("Right angle");
});
// Case 3: Obtuse angles
test(`should return "Obtuse angle" when (90 < angle < 180)`, () => {
// Test various Obtuse angle, including boundary cases
expect(getAngleType(91)).toEqual("Obtuse angle");
expect(getAngleType(145)).toEqual("Obtuse angle");
expect(getAngleType(179)).toEqual("Obtuse angle");
});
// Case 4: Straight angle
test(`should return "Straight angle" when (angle = 180)`, () => {
// Test various Straight angle
expect(getAngleType(180)).toEqual("Straight angle");
});
// Case 5: Reflex angles
test(`should return "Reflex angle" when (180 < angle < 360)`, () => {
// Test various Obtuse angle, including boundary cases
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 angles" when angle is not number`, () => {
// Test various Obtuse angle, including boundary cases
expect(() => getAngleType(" ")).toThrow("Input must be a number");
expect(() => getAngleType("Shirin")).toThrow("Input must be a number");
expect(() => getAngleType("*123")).toThrow("Input must be a number");
});

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.

According to the spec defined in 1-get-angle-type.js, the function only needs to handle arguments of type number, and any number that cannot be classified as one of the above named angles are invalid angles.

Could you update this test and the function implementation accordingly?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I've updated the function to handle numeric inputs and throw an "Invalid angle" error for numbers that don't match any of the named angle types. I've also updated the tests to cover the valid angle types and invalid numeric values.

// Case 7: Invalid numeric angles
test("should throw for invalid numeric angles", () => {
expect(() => getAngleType(-10)).toThrow("Invalid angle");
expect(() => getAngleType(0)).toThrow("Invalid angle");
expect(() => getAngleType(360)).toThrow("Invalid angle");
expect(() => getAngleType(500)).toThrow("Invalid angle");
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,44 @@ import { isProperFraction } from "../implement/2-is-proper-fraction.js";
test(`should return false when denominator is zero`, () => {
expect(isProperFraction(1, 0)).toEqual(false);
});

test(`should return true when |numerator| < |denominator|`, () => {
expect(isProperFraction(3, 5)).toEqual(true);
});

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

test(`should return true when numerator is zero and denominator is non-zero`, () => {
expect(isProperFraction(0, 2)).toEqual(true);
});

test(`should return true when numerator is negative and |numerator| < |denominator|`, () => {
expect(isProperFraction(-4, 6)).toEqual(true);
});

test(`should return false when numerator is negative and |numerator| > |denominator|`, () => {
expect(isProperFraction(-8, 6)).toEqual(false);
});

test(`should return true when denominator is negative and |numerator| < |denominator|`, () => {
expect(isProperFraction(2, -5)).toEqual(true);
});

test(`should return false when denominator is negative and |numerator| > |denominator|`, () => {
expect(isProperFraction(4, -3)).toEqual(false);
});

test(`should compare absolute values when both numbers are negative`, () => {
expect(isProperFraction(-4, -3)).toEqual(false);
expect(isProperFraction(-1, -3)).toEqual(true);
});

test(` should return true when numerator is zero and denominator is negative`, () => {
expect(isProperFraction(0, -3)).toEqual(true);
});

test(`should return false when numerator and denominator are both zero`, () => {
expect(isProperFraction(0, 0)).toEqual(false);
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,42 @@ import { getCardValue } from "../implement/3-get-card-value.js";
// 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)
test(`Should return number card for Valid digit card`, () => {
expect(getCardValue("3♠")).toEqual(3);
expect(getCardValue("7♥")).toEqual(7);
expect(getCardValue("10♦")).toEqual(10);
expect(getCardValue("9♣")).toEqual(9);
});
// Face Cards (J, Q, K)
test(`Should return 10 when given an Face card`, () => {
expect(getCardValue("J♠")).toEqual(10);
expect(getCardValue("Q♥")).toEqual(10);
expect(getCardValue("K♦")).toEqual(10);
});
// Invalid Cards
test(`should return "Input must be a String" when card is not string type`, () => {
expect(() => getCardValue(7)).toThrow("Input must be a String");
expect(() => getCardValue(3)).toThrow("Input must be a String");
});

test(`should return "Expected a number followed by a suit, but got invalid" when card is not string type`, () => {
expect(() => getCardValue("1♠")).toThrow(
"Expected a number followed by a suit, but got invalid"
);
});

test(`should return "Expected a number followed by a suit, but got invalid" when card is not string type`, () => {
expect(() => getCardValue("9X")).toThrow(
"Expected a number followed by a suit, but got invalid"
);
});
// 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

Loading