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
@@ -1,12 +1,31 @@
import { getAngleType } from "../implement/1-get-angle-type.js";
import assert from "node:assert";
import test from "node:test";

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("Classifies right angles", () => {
const right = getAngleType(90);
assert.equal(right, "Right angle");
test("Classifies acute angle", () => {
assert.equal(getAngleType(45), "Acute angle");
});

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

test("Classifies obtuse angle", () => {
assert.equal(getAngleType(120), "Obtuse angle");
});

test("Classifies straight angle", () => {
assert.equal(getAngleType(180), "Straight angle");
});

test("Classifies reflex angle", () => {
assert.equal(getAngleType(200), "Reflex angle");
});

test("Classifies boundary and invalid angles", () => {
assert.equal(getAngleType(365), "Invalid angle");
assert.equal(getAngleType(-10), "Invalid angle");
});
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { isProperFraction } from "../implement/2-is-proper-fraction.js";
import assert from "node:assert";
import test from "node:test";

import { isProperFraction } from "../implement/2-is-proper-fraction.js";

// TODO: Write tests to cover all cases.
// What combinations of numerators and denominators should you test?

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

test("Basic proper fraction", () => {
assert.equal(isProperFraction(3, 2), "false");
});
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
import { getCardValue } from "../implement/3-get-card-value.js";
import assert from "node:assert";
import test from "node:test";

import { getCardValue } from "../implement/3-get-card-value.js";

// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.

test("Valid single-digit card", () => {
test("Valid number card", () => {
assert.equal(getCardValue("9♠"), 9);
});

test("Valid face card", () => {
assert.equal(getCardValue("K♦"), 10);
});

test("Valid ace card", () => {
assert.equal(getCardValue("A♥"), 11);
});

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

// TODO: What other invalid card cases can you think of?
17 changes: 15 additions & 2 deletions implement-and-rewrite-tests/implement/1-get-angle-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@
// After you have implemented the function, write tests to cover all the cases, and
// execute the code to ensure all tests pass.

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

export { getAngleType };
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@
// execute the code to ensure all tests pass.

export function isProperFraction(numerator, denominator) {
// TODO: Implement this function
if (numerator < denominator) {
return "true";
}
return "false";
}
40 changes: 39 additions & 1 deletion implement-and-rewrite-tests/implement/3-get-card-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,45 @@
// Acceptance criteria:
// After you have implemented the function, write tests to cover all the cases, and
// execute the code to ensure all tests pass.
//const rank = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]
//const suit = ["♠", "♥", "♦", "♣"];
//console.log(suit[1]);
//const card = `${number}${suit}`

//if the user input the rank only, the output is throwing an error.
//expect the format is 2 digit and follow the suit.
//validate input example: 3♠.
//Required the user input the card must include a suit. extract the final character from the input.

export function getCardValue(card) {
// TODO: Implement this function
const rank = card.slice(0, -1);
const suit = card.slice(-1);
const validRank = [
"A",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"J",
"Q",
"K",
].includes(rank);
const validSuit = ["♠", "♥", "♦", "♣"].includes(suit);

if (!validRank || !validSuit || card.length > 3 || card.length < 2) {
throw new Error(`Expected a number followed by a suit, but got "invalid"`);
}

if (rank === "A") {
return 11;
} else if (["J", "Q", "K"].includes(rank)) {
return 10;
} else {
return Number(rank);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,29 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => {
});

// 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 (angle < 180 && angle > 90)`, () => {
expect(getAngleType(100)).toEqual("Obtuse angle");
expect(getAngleType(120)).toEqual("Obtuse angle");
expect(getAngleType(175)).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
test(`should return "Reflex angle" when (angle > 180 && angle < 360)`, () => {
expect(getAngleType(190)).toEqual("Reflex angle");
});

// Case 6: Invalid angles
test(`should return "Invalid angle" when (angel > 360 || angle < 1)`, () => {
expect(getAngleType(-10)).toEqual("Invalid angle");
expect(getAngleType(365)).toEqual("Invalid angle");
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,17 @@ import { isProperFraction } from "../implement/2-is-proper-fraction.js";

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

test(`should return true when denominator is more than numerator`, () => {
expect(isProperFraction(1, 4)).toEqual("true");
});

test(`should return false when denominator is less than numerator`, () => {
expect(isProperFraction(4, 1)).toEqual("false");
});

test(`should true when denominator and numerator are negative`, () => {
expect(isProperFraction(-5, -2)).toEqual("true");
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,22 @@ test(`Should return 11 when given an ace card`, () => {

// Suggestion: Group the remaining test data into these categories:
// Number Cards (2-10)
test(`Should return 7 when given an seven card`, () => {
expect(getCardValue("7♥")).toEqual(7);
});
// Face Cards (J, Q, K)
test(`Should return 10 when given J/Q/K`, () => {
expect(getCardValue("J♦")).toEqual(10);
expect(getCardValue("Q♣")).toEqual(10);
expect(getCardValue("K♠")).toEqual(10);
});
// Invalid Cards
test(`Expected a number followed by a suit, but got invalid`, () => {
expect(() => getCardValue("invalid")).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
Loading