Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,28 @@ test("Classifies right angles", () => {
const right = getAngleType(90);
assert.equal(right, "Right angle");
});

test("Classifies acute angles", () => {
const acute = getAngleType(45);
assert.equal(acute, "Acute angle");
});

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

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

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

test("Classifies invalid angles", () => {
const invalid = getAngleType(400);
assert.equal(invalid, "Invalid angle");
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,31 @@ test("Basic proper fraction", () => {
// Example: 1/2 is a proper fraction
assert.equal(isProperFraction(1, 2), true);
});

test("Improper fraction where numerator is bigger than denominator", () => {
assert.equal(isProperFraction(3, 2), false);
});

test("Proper fraction with negative denominator", () => {
assert.equal(isProperFraction(1, -2), true);
});

test("Proper fraction with negative numerator", () => {
assert.equal(isProperFraction(-2, 4), true);
});

test("Proper fraction with both negatives where numerator is smaller", () => {
assert.equal(isProperFraction(-2, -4), true);
});

test("Proper fraction with both negatives where numerator is bigger", () => {
assert.equal(isProperFraction(-3, -2), false);
});

test("Improper fraction with equal values", () => {
assert.equal(isProperFraction(2, 2), false);
});

test("Proper fraction with zero numerator", () => {
assert.equal(isProperFraction(0, 4), true);
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,64 @@ 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", () => {
// assert.equal(getCardValue("9♠"), 9);
// });

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

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

test("Arbitrary non-card string", () => {
assert.throws(() => getCardValue("invalid"), /Expected a number followed by a suit, but got "invalid"/, "Expected clear error");
test("Valid double-digit card", () => {
assert.equal(getCardValue("10♥"), 10);
});
test("Valid king card", () => {
assert.equal(getCardValue("Q♣"), 10);
});
test("Valid queen card", () => {
assert.equal(getCardValue("K♠"), 10);
});
test("Valid jack card", () => {
assert.equal(getCardValue("J♦"), 10);
});
test("Valid ace card", () => {
assert.equal(getCardValue("A♠"), 11);
});

// TODO: What other invalid card cases can you think of?

test("No suit symbol", () => {
assert.throws(
() => getCardValue("A"),
Error,
"Should throw an error for card without suit"
);
});

test("Invalid number", () => {
assert.throws(
() => getCardValue("0"),
Error,
"Should throw an error for card with invalid number"
);
});
test("Empty input", () => {
assert.throws(
() => getCardValue(""),
Error,
"Should throw an error for empty input"
);
});
test("Any other invalid input", () => {
assert.throws(
() => getCardValue("10♥♥"),
Error,
"Should throw an error for an invalid input"
);
});
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 === 90) {
return `Right angle`;
} else if (angle >= 0 && angle < 90) {
return `Acute angle`;
} else if (angle > 90 && angle < 180) {
return `Obtuse angle`;
} else if (angle === 180) {
return `Straight angle`;
} else if (angle > 180 && angle <= 360) {
return `Reflex angle`;
}
return `Invalid angle`;
}

export { getAngleType };
6 changes: 4 additions & 2 deletions implement-and-rewrite-tests/implement/2-is-proper-fraction.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
// After you have implemented the function, write tests to cover all the cases, and
// execute the code to ensure all tests pass.

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

export { isProperFraction };
36 changes: 34 additions & 2 deletions implement-and-rewrite-tests/implement/3-get-card-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,38 @@
// After you have implemented the function, write tests to cover all the cases, and
// execute the code to ensure all tests pass.

export function getCardValue(card) {
// TODO: Implement this function
function getCardValue(card) {
if (card == "") {
throw new Error(`Missing a specific number and specific suit symbol`);
}
const cardRank = card.slice(0, -1);
const suit = card.slice(-1);

if (!["♠", "♥", "♦", "♣"].includes(suit)) {
throw new Error(`Missing a specific suit symbol`);
}

if (cardRank == "A") {
return 11;
}
if (cardRank == "J" || cardRank == "K" || cardRank == "Q") {
return 10;
}
if (
cardRank == "2" ||
cardRank == "3" ||
cardRank == "4" ||
cardRank == "5" ||
cardRank == "6" ||
cardRank == "7" ||
cardRank == "8" ||
cardRank == "9" ||
cardRank == "10"
) {
return Number(cardRank);
} else {
throw new Error(`Missing a specific number and specific suit symbol`);
}
}

export { getCardValue };
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,28 @@ 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 (90 < angle < 180)`, () => {
expect(getAngleType(91)).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)`, () => {
expect(getAngleType(180)).toEqual("Straight angle");
});
// Case 5: Reflex 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 6: Invalid angles
test(`should return "Invalid angle" when (angle < 0 || angle > 360)`, () => {
expect(getAngleType(-1)).toEqual("Invalid angle");
expect(getAngleType(361)).toEqual("Invalid angle");
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,31 @@ 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 is less than denominator`, () => {
expect(isProperFraction(1, 2)).toEqual(true);
});

test(`should return false when numerator is more than denominator`, () => {
expect(isProperFraction(3, 2)).toEqual(false);
});

test(`should return true when denominator is negative`, () => {
expect(isProperFraction(1, -2)).toEqual(true);
});

test(`should return true when nominator is negative`, () => {
expect(isProperFraction(-2, 4)).toEqual(true);
});

test(`should return true with both negatives where numerator is smaller`, () => {
expect(isProperFraction(-2, -4)).toEqual(true);
});

test(`should return false with both negatives where numerator is bigger`, () => {
expect(isProperFraction(-3, -2)).toEqual(false);
});

test(`should return false with equal values`, () => {
expect(isProperFraction(2, 2)).toEqual(false);
});
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,64 @@ test(`Should return 11 when given an ace 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
// -----number cards------
test(`Should return 2 when given a 2 card`, () => {
expect(getCardValue("2♥")).toEqual(2);
});

test(`Should return 3 when given a 3 card`, () => {
expect(getCardValue("3♣")).toEqual(3);
});
test(`Should return 4 when given a 4 card`, () => {
expect(getCardValue("4♦")).toEqual(4);
});
test(`Should return 5 when given a 5 card`, () => {
expect(getCardValue("5♠")).toEqual(5);
});
test(`Should return 6 when given a 6 card`, () => {
expect(getCardValue("6♦")).toEqual(6);
});
test(`Should return 7 when given a 7 card`, () => {
expect(getCardValue("7♥")).toEqual(7);
});
test(`Should return 8 when given a 8 card`, () => {
expect(getCardValue("8♠")).toEqual(8);
});
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);
});
// -----remaining face cards-----
test(`Should return 10 when given a jack card`, () => {
expect(getCardValue("J♥")).toEqual(10);
});
test(`Should return 10 when given a king card`, () => {
expect(getCardValue("K♣")).toEqual(10);
});
test(`Should return 10 when given a queen card`, () => {
expect(getCardValue("Q♥")).toEqual(10);
});
// -----invalid cards-----
test("Should throw an error for card without suit", () => {
expect(() => getCardValue("A")).toThrow("Missing a specific suit symbol");
});

test("Should throw an error for card with invalid number", () => {
expect(() => getCardValue("0♥")).toThrow(
"Missing a specific number and specific suit symbol"
);
});

test("Should throw an error for empty input", () => {
expect(() => getCardValue("")).toThrow(
"Missing a specific number and specific suit symbol"
);
});

test("Should throw an error for an invalid input", () => {
expect(() => getCardValue("J♥♥")).toThrow(
"Missing a specific number and specific suit symbol"
);
});
Loading