Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,19 @@
// execute the code to ensure all tests pass.

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

// The line below allows us to load the getAngleType function into tests in other files.
Expand All @@ -31,7 +43,30 @@ function assertEquals(actualOutput, targetOutput) {
);
}

// TODO: Write tests to cover all cases, including boundary and invalid cases.
// Example: Identify Right Angles
const right = getAngleType(90);
assertEquals(right, "Right angle");
// Invalid angles
assertEquals(getAngleType(0), "Invalid angle");
assertEquals(getAngleType(-10), "Invalid angle");
assertEquals(getAngleType(360), "Invalid angle");

// Acute angles and its boundaries
assertEquals(getAngleType(1), "Acute angle");
assertEquals(getAngleType(45), "Acute angle");
assertEquals(getAngleType(89), "Acute angle");

// Right angle
assertEquals(getAngleType(90), "Right angle");

// Obtuse angles and its boundaries
assertEquals(getAngleType(91), "Obtuse angle");
assertEquals(getAngleType(120), "Obtuse angle");
assertEquals(getAngleType(179), "Obtuse angle");

// Straight angle
assertEquals(getAngleType(180), "Straight angle");

// Reflex angles and its boundaries
assertEquals(getAngleType(181), "Reflex angle");
assertEquals(getAngleType(200), "Reflex angle");
assertEquals(getAngleType(359), "Reflex angle");


Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
// execute the code to ensure all tests pass.

function isProperFraction(numerator, denominator) {
// TODO: Implement this function
const properFraction = numerator / denominator;
return properFraction > 0 && properFraction < 1;
}

// The line below allows us to load the isProperFraction function into tests in other files.
Expand All @@ -26,8 +27,27 @@ function assertEquals(actualOutput, targetOutput) {
);
}

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

// Example: 1/2 is a proper fraction
// TEST CASES
// Normal Scenarios
assertEquals(isProperFraction(1, 2), true);
assertEquals(isProperFraction(5, 10), true);

// Boundary Values
assertEquals(isProperFraction(0, 5), false);
assertEquals(isProperFraction(1, 1), false);

// Improper Fraction
assertEquals(isProperFraction(8, 3), false)

// Negative Values
assertEquals(isProperFraction(-3, 8), false);
assertEquals(isProperFraction(-8, -3), false);
assertEquals(isProperFraction(-3, -8), true);

//Very Small
assertEquals(isProperFraction(0.0001, 999999), true);

//Very Large
assertEquals(isProperFraction(999999, 0.0001), false);

Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,48 @@
// execute the code to ensure all tests pass.

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

const faceCard = rankList.slice(-3);

const cardRank = card.slice(0, -1);
const cardSuit = card.charAt(card.length - 1);

const validCard = rankList.includes(cardRank) && suitList.includes(cardSuit);

if (!validCard) {
throw new Error("Invalid card");
}

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

if (faceCard.includes(cardRank)) {
return 10;
}

return Number(cardRank);
}

// The line below allows us to load the getCardValue function into tests in other files.
console.log(getCardValue("J♥"));

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

Expand All @@ -37,9 +75,11 @@ function assertEquals(actualOutput, targetOutput) {
);
}

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

assertEquals(getCardValue("9♠"), 9);
assertEquals(getCardValue("A♥"), 11);
assertEquals(getCardValue("K♦"), 10);
assertEquals(getCardValue("10♣"), 10);

// Handling invalid cards
try {
Expand All @@ -49,4 +89,19 @@ try {
console.error("Error was not thrown for invalid card");
} catch (e) {}


// What other invalid card cases can you think of?
try {
getCardValue("♠9");
console.error("Error was not thrown for invalid card");
} catch (e) {}

try {
getCardValue("1♠");
console.error("Error was not thrown for invalid card");
} catch (e) {}

try {
getCardValue("♠");
console.error("Error was not thrown for invalid card");
} catch (e) {}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// We will use the same function, but write tests for it using Jest in this file.
const getAngleType = require("../implement/1-get-angle-type");

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

// Case 1: Acute angles
Expand All @@ -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 (angle > 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 (angle > 180 && angle < 360)', () => {
expect(getAngleType(181)).toEqual("Reflex angle");
expect(getAngleType(200)).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(0)).toEqual("Invalid angle");
expect(getAngleType(-10)).toEqual("Invalid angle");
expect(getAngleType(360)).toEqual("Invalid angle");
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,35 @@
// We will use the same function, but write tests for it using Jest in this file.
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.
// Write tests in Jest syntax to cover all combinations of positives, negatives, zeros, and other categories.

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

// Normal case:
test(`should return true in normal scenario`, () => {
expect(isProperFraction(5, 10)).toEqual(true);
});
// Boundary Value
test(`should return false when numerator is 0`, () => {
expect(isProperFraction(0, 5)).toEqual(false);
});
// Improper Fraction
test(`should return false when numerator is greater than denominator`, () => {
expect(isProperFraction(8, 3)).toEqual(false);
});
// Negative Value
test(`should return false when numerator is negative`, () => {
expect(isProperFraction(-3, 8)).toEqual(false);

});
// Very Small
test(`should return true with very small values`, () => {
expect(isProperFraction(0.0001, 999999)).toEqual(true);
});
// very large
test(`should return false with very large`, () => {
expect(isProperFraction(999999, 0.0001)).toEqual(false);
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// We will use the same function, but write tests for it using Jest in this file.
const getCardValue = require("../implement/3-get-card-value");

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

// Case 1: Ace (A)
test(`Should return 11 when given an ace card`, () => {
Expand All @@ -11,8 +11,24 @@ 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 should return its numeric value`, () => {
expect(getCardValue("9♠")).toEqual(9);
});
// Face Cards (J, Q, K)
test(`Should return 10`, () => {
expect(getCardValue("K♠")).toEqual(10);
});
// Invalid Cards
test(`Should throw an error`, () => {
expect(() => {
getCardValue("♠9");
}).toThrow();
});
test(`Should throw an error`, () => {
expect(() => {
getCardValue("♠");
}).toThrow();
});

// To learn how to test whether a function throws an error as expected in Jest,
// please refer to the Jest documentation:
Expand Down
12 changes: 11 additions & 1 deletion Sprint-3/2-practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
function countChar(stringOfCharacters, findCharacter) {
return 5
let count = 0;
const str = stringOfCharacters.length;

for (let i = 0; i < str; i++) {
if (stringOfCharacters[i] === findCharacter) {
count += 1;
console.log(stringOfCharacters[i]);
}
}
return count;
}
console.log(countChar("little", "t"));

module.exports = countChar;
7 changes: 7 additions & 0 deletions Sprint-3/2-practice-tdd/count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,10 @@ test("should count multiple occurrences of a character", () => {
// 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("no occurrences of character in string", () => {
const str = "little"
const char = "b"
const count = countChar(str, char);
expect(count).toEqual(0);
});
20 changes: 19 additions & 1 deletion Sprint-3/2-practice-tdd/get-ordinal-number.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
function getOrdinalNumber(num) {
return "1st";
const lastTwoDigits = num % 100;
const lastDigit = num % 10;

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

switch (lastDigit) {
case 1:
return `${num}${"st"}`;
case 2:
return `${num}${"nd"}`;
case 3:
return `${num}${"rd"}`;
default:
return `${num}${"th"}`;
}
}

console.log(getOrdinalNumber(3));

module.exports = getOrdinalNumber;
26 changes: 26 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 @@ -18,3 +18,29 @@ test("should append 'st' for numbers ending with 1, except those ending with 11"
expect(getOrdinalNumber(21)).toEqual("21st");
expect(getOrdinalNumber(131)).toEqual("131st");
});

// Case 2: Numbers ending in 2 → add nd → (2nd, 22nd, 42nd)
test("should append 'nd' for numbers ending with 2", () => {
expect(getOrdinalNumber(2)).toEqual("2nd");
expect(getOrdinalNumber(22)).toEqual("22nd");
expect(getOrdinalNumber(142)).toEqual("142nd");
});
// Case 3: Numbers ending in 3 → add rd → (3rd, 23rd, 53rd)
test("should append '3rd' for numbers ending with 3", () => {
expect(getOrdinalNumber(3)).toEqual("3rd");
expect(getOrdinalNumber(33)).toEqual("33rd");
expect(getOrdinalNumber(153)).toEqual("153rd");
});
// Case 4: All other numbers → add th → (4th, 6th, 20th, 100th)
test("should append 'th' for numbers ending with 4", () => {
expect(getOrdinalNumber(4)).toEqual("4th");
expect(getOrdinalNumber(20)).toEqual("20th");
expect(getOrdinalNumber(100)).toEqual("100th");
});
// Exceptions: Numbers ending in 11, 12, and 13 use -th (e.g., 11th, 12th, 13th).
test("should append 'th' for numbers ending with 11, 12 and 13", () => {
expect(getOrdinalNumber(11)).toEqual("11th");
expect(getOrdinalNumber(12)).toEqual("12th");
expect(getOrdinalNumber(113)).toEqual("113th");
});

7 changes: 5 additions & 2 deletions Sprint-3/2-practice-tdd/repeat-str.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
function repeatStr() {
return "hellohellohello";
function repeatStr(str, count) {
if(count < 0){
throw new Error("negative counts are not valid");
}
return str.repeat(count);
}

module.exports = repeatStr;
Loading
Loading