Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,27 @@
// 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";
}
return "Invalid angle";
}

// The line below allows us to load the getAngleType function into tests in other files.
// This will be useful in the "rewrite tests with jest" step.
module.exports = getAngleType;

// This helper function is written to make our assertions easier to read.
// If the actual output matches the target output, the test will pass
function assertEquals(actualOutput, targetOutput) {
Expand All @@ -33,5 +47,15 @@ function assertEquals(actualOutput, targetOutput) {

// TODO: Write tests to cover all cases, including boundary and invalid cases.
// Example: Identify Right Angles
const acute = getAngleType(45);
assertEquals(acute, "Acute angle");
const right = getAngleType(90);
assertEquals(right, "Right angle");
const obtuse = getAngleType(135);
assertEquals(obtuse, "Obtuse angle");
const straight = getAngleType(180);
assertEquals(straight, "Straight angle");
const reflex = getAngleType(270);
assertEquals(reflex, "Reflex angle");
const invalidAngle = getAngleType(-10);
assertEquals(invalidAngle, "Invalid angle");
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,16 @@
// execute the code to ensure all tests pass.

function isProperFraction(numerator, denominator) {
// TODO: Implement this function
if (numerator === 0) {
return false;
}
Comment thread
Yonatanteklemariam marked this conversation as resolved.
Outdated
if (denominator === 0) {
return false;
}
if (Number.isNaN(numerator) || Number.isNaN(denominator)) {
return false;
}
return Math.abs(numerator) < Math.abs(denominator);
}

// The line below allows us to load the isProperFraction function into tests in other files.
Expand All @@ -25,6 +34,25 @@ function assertEquals(actualOutput, targetOutput) {
`Expected ${actualOutput} to equal ${targetOutput}`
);
}
// Proper fractions
assertEquals(isProperFraction(1, 2), true);
assertEquals(isProperFraction(3, 4), true);
assertEquals(isProperFraction(-1, 5), true);
assertEquals(isProperFraction(1, -5), true);

// Improper fractions
assertEquals(isProperFraction(5, 3), false);
assertEquals(isProperFraction(10, 10), false);
assertEquals(isProperFraction(-7, 3), false);

// Zero cases
assertEquals(isProperFraction(0, 5), false);
assertEquals(isProperFraction(3, 0), false);

// NaN cases
assertEquals(isProperFraction(NaN, 5), false);
assertEquals(isProperFraction(3, NaN), false);
assertEquals(isProperFraction(NaN, NaN), false);

// TODO: Write tests to cover all cases.
// What combinations of numerators and denominators should you test?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,34 @@
// execute the code to ensure all tests pass.

function getCardValue(card) {
// TODO: Implement this function
const ranks = [
"A",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"J",
"Q",
"K",
];
const suits = ["♠", "♥", "♦", "♣"];

let rank = card.slice(0, -1); // extracting the rank from the input
let suit = card.slice(-1); // extracting the suit from the input
if (!suits.includes(suit)) {
throw new Error("Invalid suit");
}
if (!ranks.includes(rank)) {
throw new Error("Invalid rank");
}
if (rank === "A") return 11;
if (["J", "Q", "K"].includes(rank)) return 10;
return Number(rank);
}

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

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

// Face cards
assertEquals(getCardValue("J♣"), 10);
assertEquals(getCardValue("Q♦"), 10);
assertEquals(getCardValue("K♥"), 10);

// Ace
assertEquals(getCardValue("A♠"), 11);

// Handling invalid cards
try {
Expand All @@ -52,3 +91,30 @@ try {
}

// What other invalid card cases can you think of?
try {
getCardValue("");
console.error("Error NOT thrown for empty string");
} catch (e) {
console.log("Error thrown for empty string 🎉");
}

try {
getCardValue("1♠");
console.error("Error NOT thrown for invalid rank");
} catch (e) {
console.log("Error thrown for invalid rank 🎉");
}

try {
getCardValue("A");
console.error("Error NOT thrown for missing suit");
} catch (e) {
console.log("Error thrown for missing suit 🎉");
}

try {
getCardValue("A?");
console.error("Error NOT thrown for invalid suit");
} catch (e) {
console.log("Error thrown for invalid suit 🎉");
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,33 @@ 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(120)).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(250)).toEqual("Reflex angle");
expect(getAngleType(359)).toEqual("Reflex angle");
});

// Case 6: Invalid angles
test(`should return "Invalid angle" for angles outside 0–360`, () => {
expect(getAngleType(0)).toEqual("Invalid angle"); // boundary
expect(getAngleType(360)).toEqual("Invalid angle"); // boundary
expect(getAngleType(-10)).toEqual("Invalid angle");
expect(getAngleType(400)).toEqual("Invalid angle");
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,40 @@ 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
// Special case: denominator is zero
test(`should return false when denominator is zero`, () => {
expect(isProperFraction(1, 0)).toEqual(false);
});

//Special case: numerator is zero
test(`should return false when numerator is zero`, () => {
expect(isProperFraction(0, 1)).toEqual(false);
});
// Proper functions with absolute numerator < absolute denominator
test(`should return true for proper positive fractions`, () => {
expect(isProperFraction(1, 2)).toEqual(true);
expect(isProperFraction(3, 4)).toEqual(true);
});
// Proper functions with negative numbers
test(`should return true for proper negative fractions`, () => {
expect(isProperFraction(-1, 2)).toEqual(true);
expect(isProperFraction(1, -3)).toEqual(true);
expect(isProperFraction(-2, -5)).toEqual(true);
});
// Improper functions with absolute numerator >= absolute denominator
test(`should return false for improper fractions`, () => {
expect(isProperFraction(5, 3)).toEqual(false);
expect(isProperFraction(10, 10)).toEqual(false);
expect(isProperFraction(-7, 3)).toEqual(false);
});
// Special cases: numerator or denominator is NAN
test(`should return false when numerator or denominator is NaN`, () => {
expect(isProperFraction(NaN, 5)).toEqual(false);
expect(isProperFraction(3, NaN)).toEqual(false);
expect(isProperFraction(NaN, NaN)).toEqual(false);
});
// Special cases: non-number strings
test(`should return false for non-numeric strings`, () => {
expect(isProperFraction("a", 5)).toEqual(false);
expect(isProperFraction(3, "b")).toEqual(false);
});
Comment on lines +39 to +43

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.

What do you mean by "non-numeric strings"? Is "3e2" a non-numeric string?

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,38 @@ test(`Should return 11 when given an ace card`, () => {
expect(getCardValue("A♠")).toEqual(11);
});

// Case 2: Number Cards (2-10)
test(`Should return correct values for number cards`, () => {
expect(getCardValue("2♠")).toEqual(2);
expect(getCardValue("7♥")).toEqual(7);
expect(getCardValue("9♦")).toEqual(9);
expect(getCardValue("10♣")).toEqual(10);
});

// Case 3: Face Cards (J, Q, K)
test(`Should return 10 for face cards J, Q, K`, () => {
expect(getCardValue("J♠")).toEqual(10);
expect(getCardValue("Q♥")).toEqual(10);
expect(getCardValue("K♦")).toEqual(10);
});

// Case 4: Invalid cards
test(`Should throw an error for invalid rank`, () => {
expect(() => getCardValue("1♠")).toThrowError("Invalid rank");
});

test(`Should throw an error for invalid suit`, () => {
expect(() => getCardValue("A?")).toThrowError("Invalid suit");
});

test(`Should throw an error for missing suit`, () => {
expect(() => getCardValue("A")).toThrowError();
});

test(`Should throw an error for empty string`, () => {
expect(() => getCardValue("")).toThrowError();
});

// Suggestion: Group the remaining test data into these categories:
// Number Cards (2-10)
// Face Cards (J, Q, K)
Expand All @@ -17,4 +49,3 @@ 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

19 changes: 19 additions & 0 deletions Sprint-3/2-practice-tdd/count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,25 @@ test("should count multiple occurrences of a character", () => {
});

// Scenario: No Occurrences
test(`should return zero the character doesn't exist in the string`, () => {
const str = "bravo";
const char = "u";
const count = countChar(str, char);
expect(count).toEqual(0);
});

// Empty String
test(`should return zero when the string is empty`, () => {
expect(countChar("", "a")).toEqual(0);
});

// Scenario: Multiple Occurrences
test("should count multiple occurrences of characters (including mixed and case-sensitive)", () => {
expect(countChar("aaaaa", "a")).toEqual(5); // simple multiple
expect(countChar("1-2-3-4-5-", "-")).toEqual(5); // mixed characters
expect(countChar("AaAa", "A")).toEqual(2); // case sensitivity
});

// Given the input string `str`,
// And a character `char` that does not exist within `str`.
// When the function is called with these inputs,
Expand Down
29 changes: 29 additions & 0 deletions Sprint-3/2-practice-tdd/get-ordinal-number.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,37 @@ const getOrdinalNumber = require("./get-ordinal-number");
// Case 1: Numbers ending with 1 (but not 11)
// When the number ends with 1, except those ending with 11,
// Then the function should return a string by appending "st" to the number.

test("should append 'st' for numbers ending with 1, except those ending with 11", () => {
expect(getOrdinalNumber(1)).toEqual("1st");
expect(getOrdinalNumber(21)).toEqual("21st");
expect(getOrdinalNumber(131)).toEqual("131st");
});

// Case 2: Numbers ending with 2 (but NOT 12)
test("should append 'nd' for numbers ending with 2, except those ending with 12", () => {
expect(getOrdinalNumber(2)).toEqual("2nd");
expect(getOrdinalNumber(22)).toEqual("22nd");
expect(getOrdinalNumber(102)).toEqual("102");
});

// Case 3: Numbers ending with 3 (but NOT 13)
test("should append 'rd' for numbers ending with 3, except those ending with 13", () => {
expect(getOrdinalNumber(3)).toEqual("3rd");
expect(getOrdinalNumber(33)).toEqual("33rd");
expect(getOrdinalNumber(103)).toEqual("103rd");
});

// Case 4: Numbers ending with 11, 12, or 13 --- 11,12,13,111,121,131,
test("should append 'th' for numbers ending with 11, 12, 13", () => {
expect(getOrdinalNumber(11)).toEqual("11th");
expect(getOrdinalNumber(12)).toEqual("12th");
expect(getOrdinalNumber(13)).toEqual("13th");
});

// Case 5: Numbers ending with larger numbers of 111, 121, 131
test("should append 'th' for numbers ending with 111, 121, 131", () => {
expect(getOrdinalNumber(111)).toEqual("111th");
expect(getOrdinalNumber(121)).toEqual("121th");
expect(getOrdinalNumber(131)).toEqual("131th");
});
4 changes: 2 additions & 2 deletions Sprint-3/2-practice-tdd/repeat-str.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
function repeatStr() {
function repeatStr(str, count) {
// 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";
return str.repeat(count);
}

module.exports = repeatStr;
Loading
Loading