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 @@ -3,10 +3,35 @@ 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 acute angles", () => {
assert.equal(getAngleType(45), "Acute angle");
assert.equal(getAngleType(1), "Acute angle");
assert.equal(getAngleType(89), "Acute angle");
});

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

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

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

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

test("Identifies invalid angles", () => {
assert.equal(getAngleType(0), "Invalid angle");
assert.equal(getAngleType(360), "Invalid angle");
assert.equal(getAngleType(-1), "Invalid angle");
assert.equal(getAngleType(361), "Invalid angle");
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,31 @@ 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
test("Identifies basic proper fractions", () => {
assert.equal(isProperFraction(1, 2), true);
assert.equal(isProperFraction(3, 4), true);
});

test("Identifies improper fractions", () => {
assert.equal(isProperFraction(5, 3), false);
assert.equal(isProperFraction(4, 4), false);
});

test("Handles zero numerator", () => {
assert.equal(isProperFraction(0, 5), true);
});

test("Handles negative numerators", () => {
assert.equal(isProperFraction(-1, 2), true);
assert.equal(isProperFraction(-5, 2), false);
});

test("Handles negative denominators", () => {
assert.equal(isProperFraction(1, -2), true);
assert.equal(isProperFraction(3, -2), false);
});
Comment on lines +20 to +28

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

How about both numerator and denominator are negative (e.g. -1/-2 and -2/-1)?


test("Handles zero denominator", () => {
assert.equal(isProperFraction(1, 0), false);
assert.equal(isProperFraction(0, 0), false);
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,35 @@ 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("Returns the correct value for number cards", () => {
assert.equal(getCardValue("2♠"), 2);
assert.equal(getCardValue("5♥"), 5);
assert.equal(getCardValue("9♦"), 9);
assert.equal(getCardValue("10♣"), 10);
});

test("Returns 11 for aces", () => {
assert.equal(getCardValue("A♠"), 11);
});

test("Valid single-digit card", () => {
assert.equal(getCardValue("9♠"), 9);
test("Returns 10 for face cards", () => {
assert.equal(getCardValue("J♠"), 10);
assert.equal(getCardValue("Q♥"), 10);
assert.equal(getCardValue("K♦"), 10);
});

test("Arbitrary non-card string", () => {
assert.throws(() => getCardValue("invalid"), /Expected a number followed by a suit, but got "invalid"/, "Expected clear error");
test("Accepts all four suits", () => {
assert.equal(getCardValue("5♠"), 5);
assert.equal(getCardValue("5♥"), 5);
assert.equal(getCardValue("5♦"), 5);
assert.equal(getCardValue("5♣"), 5);
});

// TODO: What other invalid card cases can you think of?
test("Rejects invalid cards", () => {
assert.throws(() => getCardValue("invalid"));
assert.throws(() => getCardValue("1♠"));
assert.throws(() => getCardValue("11♠"));
assert.throws(() => getCardValue("5X"));
assert.throws(() => getCardValue("♠5"));
assert.throws(() => getCardValue("5"));
});
22 changes: 21 additions & 1 deletion implement-and-rewrite-tests/implement/1-get-angle-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,25 @@
// execute the code to ensure all tests pass.

export function getAngleType(angle) {
// TODO: Implement this function
if (angle <= 0 || angle >= 360) {
return "Invalid angle";
}

if (angle < 90) {
return "Acute angle";
}

if (angle === 90) {
return "Right angle";
}

if (angle < 180) {
return "Obtuse angle";
}

if (angle === 180) {
return "Straight angle";
}

return "Reflex angle";
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
// execute the code to ensure all tests pass.

export function isProperFraction(numerator, denominator) {
// TODO: Implement this function
return denominator !== 0 && Math.abs(numerator) < Math.abs(denominator);
}
18 changes: 17 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 @@ -24,5 +24,21 @@
// execute the code to ensure all tests pass.

export function getCardValue(card) {
// TODO: Implement this function
const validCard = /^(A|[2-9]|10|J|Q|K)[♠♥♦♣]$/;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This line and your program is correct. For me to understand your thinking process, can you please explain the breakdown of the expression you used (and any possible chance to simplify it)? Also, what are the advantages of using such expression over other implementations? No code change is required and just adding a reply to this comment would be good enough. Thank you.


if (!validCard.test(card)) {
throw new Error(`Expected a number followed by a suit, but got "${card}"`);
}

const rank = card.slice(0, -1);

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

if (["J", "Q", "K"].includes(rank)) {
return 10;
}

return Number(rank);
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
import { getAngleType } from "../implement/1-get-angle-type.js";

// TODO: Write tests in Jest syntax to cover all cases/outcomes,
// including boundary and invalid cases.

// Case 1: Acute angles
test(`should return "Acute angle" when (0 < angle < 90)`, () => {
// Test various acute angles, including boundary cases
test("Classifies acute angles", () => {
expect(getAngleType(1)).toEqual("Acute angle");
expect(getAngleType(45)).toEqual("Acute angle");
expect(getAngleType(89)).toEqual("Acute angle");
});

// Case 2: Right angle
// Case 3: Obtuse angles
// Case 4: Straight angle
// Case 5: Reflex angles
// Case 6: Invalid angles
test("Classifies right angles", () => {
expect(getAngleType(90)).toEqual("Right angle");
});

test("Classifies obtuse angles", () => {
expect(getAngleType(91)).toEqual("Obtuse angle");
expect(getAngleType(120)).toEqual("Obtuse angle");
expect(getAngleType(179)).toEqual("Obtuse angle");
});

test("Classifies straight angles", () => {
expect(getAngleType(180)).toEqual("Straight angle");
});

test("Classifies reflex angles", () => {
expect(getAngleType(181)).toEqual("Reflex angle");
expect(getAngleType(270)).toEqual("Reflex angle");
expect(getAngleType(359)).toEqual("Reflex angle");
});

test("Identifies invalid angles", () => {
expect(getAngleType(0)).toEqual("Invalid angle");
expect(getAngleType(360)).toEqual("Invalid angle");
expect(getAngleType(-1)).toEqual("Invalid angle");
expect(getAngleType(361)).toEqual("Invalid angle");
});
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
import { isProperFraction } from "../implement/2-is-proper-fraction.js";

// TODO: Write tests in Jest syntax to cover all combinations of positives, negatives, zeros, and other categories.
test("Identifies positive proper fractions", () => {
expect(isProperFraction(1, 2)).toEqual(true);
expect(isProperFraction(3, 4)).toEqual(true);
});

test("Identifies improper fractions", () => {
expect(isProperFraction(5, 3)).toEqual(false);
expect(isProperFraction(4, 4)).toEqual(false);
});

test("Handles zero numerator", () => {
expect(isProperFraction(0, 5)).toEqual(true);
});

test("Handles negative numerators", () => {
expect(isProperFraction(-1, 2)).toEqual(true);
expect(isProperFraction(-5, 2)).toEqual(false);
});

test("Handles negative denominators", () => {
expect(isProperFraction(1, -2)).toEqual(true);
expect(isProperFraction(3, -2)).toEqual(false);
});
Comment on lines +17 to +25

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

How about both numerator and denominator are negative (e.g. -1/-2 and -2/-1)?


// Special case: numerator is zero
test(`should return false when denominator is zero`, () => {
test("Handles zero denominator", () => {
expect(isProperFraction(1, 0)).toEqual(false);
expect(isProperFraction(0, 0)).toEqual(false);
});
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
import { getCardValue } from "../implement/3-get-card-value.js";

// TODO: Write tests in Jest syntax to cover all possible outcomes.

// Case 1: Ace (A)
test(`Should return 11 when given an ace card`, () => {
test("Returns 11 for aces", () => {
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)
// Face Cards (J, Q, K)
// Invalid Cards
test("Returns the correct value for number cards", () => {
expect(getCardValue("2♠")).toEqual(2);
expect(getCardValue("5♥")).toEqual(5);
expect(getCardValue("9♦")).toEqual(9);
expect(getCardValue("10♣")).toEqual(10);
});

// 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
test("Returns 10 for face cards", () => {
expect(getCardValue("J♠")).toEqual(10);
expect(getCardValue("Q♥")).toEqual(10);
expect(getCardValue("K♦")).toEqual(10);
});

test("Throws an error for invalid cards", () => {
expect(() => getCardValue("invalid")).toThrow();
expect(() => getCardValue("1♠")).toThrow();
expect(() => getCardValue("11♠")).toThrow();
expect(() => getCardValue("5X")).toThrow();
expect(() => getCardValue("♠5")).toThrow();
expect(() => getCardValue("5")).toThrow();
});
2 changes: 1 addition & 1 deletion practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export function countChar(stringOfCharacters, findCharacter) {
return 5
return stringOfCharacters.split(findCharacter).length - 1;
}
19 changes: 14 additions & 5 deletions practice-tdd/count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,17 @@ test("should count multiple occurrences of a character", () => {
expect(count).toEqual(5);
});

// Scenario: No Occurrences
// Given the input string `str`,
// And a character `char` that does not exist within `str`.
// When the function is called with these inputs,
// Then it should return 0, indicating that no occurrences of `char` were found.
test("should return 0 when the character does not exist", () => {
const str = "hello";
const char = "z";

const count = countChar(str, char);

expect(count).toEqual(0);
});
test("should count a single occurrence", () => {
expect(countChar("hello", "h")).toEqual(1);
});
test("should return 0 for an empty string", () => {
expect(countChar("", "a")).toEqual(0);
});
22 changes: 21 additions & 1 deletion practice-tdd/get-ordinal-number.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
export function getOrdinalNumber(num) {
return "1st";
const lastTwoDigits = num % 100;

if (lastTwoDigits >= 11 && lastTwoDigits <= 13) {
return `${num}th`;
}

const lastDigit = num % 10;

if (lastDigit === 1) {
return `${num}st`;
}

if (lastDigit === 2) {
return `${num}nd`;
}

if (lastDigit === 3) {
return `${num}rd`;
}

return `${num}th`;
}
25 changes: 25 additions & 0 deletions practice-tdd/get-ordinal-number.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,28 @@ test("should append 'st' for numbers ending with 1, except those ending with 11"
expect(getOrdinalNumber(21)).toEqual("21st");
expect(getOrdinalNumber(131)).toEqual("131st");
});

test("should append 'nd' for numbers ending in 2, except those ending in 12", () => {
expect(getOrdinalNumber(2)).toEqual("2nd");
expect(getOrdinalNumber(22)).toEqual("22nd");
expect(getOrdinalNumber(112)).toEqual("112th");
});

test("should append 'rd' for numbers ending in 3, except those ending in 13", () => {
expect(getOrdinalNumber(3)).toEqual("3rd");
expect(getOrdinalNumber(23)).toEqual("23rd");
expect(getOrdinalNumber(113)).toEqual("113th");
});

test("should append 'th' for other numbers", () => {
expect(getOrdinalNumber(4)).toEqual("4th");
expect(getOrdinalNumber(5)).toEqual("5th");
expect(getOrdinalNumber(10)).toEqual("10th");
expect(getOrdinalNumber(20)).toEqual("20th");
});

test("should append 'th' for numbers ending in 11, 12 and 13", () => {
expect(getOrdinalNumber(11)).toEqual("11th");
expect(getOrdinalNumber(12)).toEqual("12th");
expect(getOrdinalNumber(13)).toEqual("13th");
});
16 changes: 12 additions & 4 deletions practice-tdd/repeat-str.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
export function repeatStr() {
// Your implementation of this function must *not* call String.prototype.repeat (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat).
// The goal is to re-implement that function, not to use it.
return "hellohellohello";
export function repeatStr(str, count) {
if (count < 0) {
throw new Error("Count cannot be negative");
}

let result = "";

for (let i = 0; i < count; i++) {
result += str;
}

return result;
}
Loading
Loading