Skip to content
Open
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
97d02c7
get angle case2
enjoy15 Oct 13, 2025
bf6c370
case3 update
enjoy15 Oct 13, 2025
1a47995
update case 4
enjoy15 Oct 13, 2025
3be3bfb
update case 5
enjoy15 Oct 13, 2025
a7b7c2f
2-fraction/case2
enjoy15 Oct 13, 2025
e110722
2-is-proper-fraction update case
enjoy15 Oct 13, 2025
716f047
10 card case
enjoy15 Oct 13, 2025
138843b
10 card case
enjoy15 Oct 13, 2025
4879aaa
Ace card
enjoy15 Oct 13, 2025
d34d343
undefined case
enjoy15 Oct 13, 2025
4d61885
1 rewrite test with jest
enjoy15 Oct 13, 2025
989855e
2-is proper-fraction test with jest
enjoy15 Oct 13, 2025
56625ec
3-get card value test with jest
enjoy15 Oct 13, 2025
47e385f
2-practice-tdd/count test
enjoy15 Oct 14, 2025
45dc764
2-practice-tdd/repeat test
enjoy15 Oct 14, 2025
00e6c6d
2-practice-tdd/get ordinal number test
enjoy15 Oct 14, 2025
35b6637
Merge branch 'coursework/sprint-3-practice-tdd' into coursework/sprin…
enjoy15 Oct 15, 2025
eb1d90f
Remove checking for angle > 90
enjoy15 Oct 25, 2025
2f42a85
convert negative numbers to positive before comparing
enjoy15 Oct 25, 2025
6db1bca
Change test result to true for -2/3
enjoy15 Oct 25, 2025
8f61c0f
Change function getCardValue to use Number instead
enjoy15 Oct 25, 2025
0e834a6
Add more tests
enjoy15 Oct 25, 2025
f14ec50
Group tests together
enjoy15 Oct 25, 2025
24bc0d7
Update comment ".. to numbers ending in 0, 4-9."
enjoy15 Oct 26, 2025
338eadc
Amend Function call with negative parameters
enjoy15 Oct 26, 2025
36e1138
validate string value before converting its value
enjoy15 Oct 26, 2025
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 @@ -12,7 +12,19 @@ function getAngleType(angle) {
return "Right angle";
}
// Run the tests, work out what Case 2 is testing, and implement the required code here.
if (angle < 90) {
return "Acute angle";
}
// Then keep going for the other cases, one at a time.
if (angle > 90 && angle < 180) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Checking angle > 90 is optional because previous if-statements (together with the return statements) can guarantee angle is always more than 90.

Copy link
Author

Choose a reason for hiding this comment

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

Removed checking angle > 90

return "Obtuse angle";
}
if (angle === 180) {
return "Straight angle";
}
if (angle > 180 && angle < 360) {
return "Reflex angle";
}
}

// The line below allows us to load the getAngleType function into tests in other files.
Expand Down Expand Up @@ -50,14 +62,16 @@ assertEquals(acute, "Acute angle");
// When the angle is greater than 90 degrees and less than 180 degrees,
// Then the function should return "Obtuse angle"
const obtuse = getAngleType(120);
// ====> write your test here, and then add a line to pass the test in the function above
assertEquals(obtuse, "Obtuse angle");

// Case 4: Identify Straight Angles:
// When the angle is exactly 180 degrees,
// Then the function should return "Straight angle"
// ====> write your test here, and then add a line to pass the test in the function above
const straight = getAngleType(180);
assertEquals(straight, "Straight angle");

// Case 5: Identify Reflex Angles:
// When the angle is greater than 180 degrees and less than 360 degrees,
// Then the function should return "Reflex angle"
// ====> write your test here, and then add a line to pass the test in the function above
const reflex = getAngleType(250);
assertEquals(reflex, "Reflex angle");
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,16 @@
// write one test at a time, and make it pass, build your solution up methodically

function isProperFraction(numerator, denominator) {
// First check: both numerator and denominator should be positive
if (numerator < 0 || denominator < 0) {
return false;
}

// Second check: numerator should be less than denominator
if (numerator < denominator) {
return true;
} else {
return false;
}
}

Expand Down Expand Up @@ -46,14 +54,23 @@ assertEquals(improperFraction, false);
// target output: true
// Explanation: The fraction -4/7 is a proper fraction because the absolute value of the numerator (4) is less than the denominator (7). The function should return true.
const negativeFraction = isProperFraction(-4, 7);
assertEquals(negativeFraction, true);
// ====> complete with your assertion

// Equal Numerator and Denominator check:
// Input: numerator = 3, denominator = 3
// target output: false
// Explanation: The fraction 3/3 is not a proper fraction because the numerator is equal to the denominator. The function should return false.
const equalFraction = isProperFraction(3, 3);
assertEquals(equalFraction, false);
// ====> complete with your assertion

// Stretch:
// What other scenarios could you test for?
// Zero Numerator check:
// Input: numerator = 0, denominator = 5
// target output: true
// Explanation: The fraction 0/5 is a proper fraction because the numerator (0) is less than the denominator (5). The function should return true.
const zeroNumerator = isProperFraction(0, 5);
assertEquals(zeroNumerator, true);

Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,24 @@
// write one test at a time, and make it pass, build your solution up methodically
// just make one change at a time -- don't rush -- programmers are deep and careful thinkers
function getCardValue(card) {
// Extract the rank (all characters except the last one which is the suit)
const rank = card.slice(0, -1);

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

const numValue = parseInt(rank, 10);
if (numValue >= 2 && numValue <= 10) {
Copy link
Contributor

Choose a reason for hiding this comment

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

In JavaScript, strings that represent valid numeric literals in the language can be safely converted to equivalent numbers. For examples, "0x02", "2.1", or "0002".

Does your function return the value you expected from each of the following function calls?

getCardValue("0x02♠");
getCardValue("2.1♠");
getCardValue("0002♠");

Copy link
Author

Choose a reason for hiding this comment

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

With the current function
getCardValue("0x02♠") → undefined
parseInt("0x02", 10) stops at "x" and returns 0, which is outside the 2–10 range, so function returns undefined.

getCardValue("2.1♠") → 2
parseInt("2.1", 10) parses up to the dot and returns 2, which is in the 2–10 range, so function returns 2.

getCardValue("0002♠") → 2
parseInt("0002", 10) returns 2, so the function returns 2.

I changed the code so "0x02" to be interpreted as the JavaScript numeric literal 0x02 (i.e. 2), and reject non-integer ranks like "2.1", by changing the parsing to use Number() and check integerness.### ###

return numValue;
}

if (rank === "J" || rank === "Q" || rank === "K") {
return 10;
}

// If we get here, it's an invalid card - return undefined
return undefined;
}

// The line below allows us to load the getCardValue function into tests in other files.
Expand Down Expand Up @@ -39,19 +54,27 @@ assertEquals(aceofSpades, 11);
// When the function is called with such a card,
// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
const fiveofHearts = getCardValue("5♥");
assertEquals(fiveofHearts, 5);
// ====> write your test here, and then add a line to pass the test in the function above

// Handle Face Cards (J, Q, K):
// Given a card with a rank of "10," "J," "Q," or "K",
// When the function is called with such a card,
// Then it should return the value 10, as these cards are worth 10 points each in blackjack.
const kingofDiamonds = getCardValue("K♦");
assertEquals(kingofDiamonds, 10);

// Handle Ace (A):
// Given a card with a rank of "A",
// When the function is called with an Ace,
// Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack.
const aceofClubs = getCardValue("A♣");
assertEquals(aceofClubs, 11);

// Handle Invalid Cards:
// Given a card with an invalid rank (neither a number nor a recognized face card),
// When the function is called with such a card,
// Then it should throw an error indicating "Invalid card rank."
const invalidCard = getCardValue("1♣");
assertEquals(invalidCard, undefined);

Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,27 @@ test("should identify right angle (90°)", () => {
// Case 2: Identify Acute Angles:
// When the angle is less than 90 degrees,
// Then the function should return "Acute angle"
test("should identify acute angle (<90°)", () => {
expect(getAngleType(45)).toEqual("Acute angle");
});

// Case 3: Identify Obtuse Angles:
// When the angle is greater than 90 degrees and less than 180 degrees,
// Then the function should return "Obtuse angle"
test("should identify obtuse angle (>90° and <180°)", () => {
expect(getAngleType(135)).toEqual("Obtuse angle");
});

// Case 4: Identify Straight Angles:
// When the angle is exactly 180 degrees,
// Then the function should return "Straight angle"
test("should identify straight angle (180°)", () => {
expect(getAngleType(180)).toEqual("Straight angle");
});

// Case 5: Identify Reflex Angles:
// When the angle is greater than 180 degrees and less than 360 degrees,
// Then the function should return "Reflex angle"
test("should identify reflex angle (>180° and <360°)", () => {
expect(getAngleType(270)).toEqual("Reflex angle");
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,16 @@ test("should return true for a proper fraction", () => {
});

// Case 2: Identify Improper Fractions:
test("should return false for an improper fraction", () => {
expect(isProperFraction(5, 3)).toEqual(false);
});

// Case 3: Identify Negative Fractions:
test("should return false for a negative fraction", () => {
expect(isProperFraction(-2, 3)).toEqual(false);
});
Copy link
Contributor

Choose a reason for hiding this comment

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

In math, -2/3 is considered a proper fraction.

Copy link
Author

Choose a reason for hiding this comment

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

change the test result to true for-2/3.


// Case 4: Identify Equal Numerator and Denominator:
test("should return false for equal numerator and denominator", () => {
expect(isProperFraction(3, 3)).toEqual(false);
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,28 @@ test("should return 11 for Ace of Spades", () => {
});

// Case 2: Handle Number Cards (2-10):
test("should return numeric value for number cards (2-10)", () => {
const fiveofHearts = getCardValue("5♥");
expect(fiveofHearts).toEqual(5);
});
Comment on lines +11 to +14
Copy link
Contributor

Choose a reason for hiding this comment

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

Could consider testing more sample values in each category (to make the test more comprehensive), especially the boundary cases such as "2♥" and "10♥".

Copy link
Author

Choose a reason for hiding this comment

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

Added more tests, especially the boundary cases such as "2♥" and "10♥."

// Case 3: Handle Face Cards (J, Q, K):
test("should return 10 for face cards (J, Q, K)", () => {
const jackOfDiamonds = getCardValue("J♦");
const queenOfClubs = getCardValue("Q♣");
const kingOfHearts = getCardValue("K♥");
expect(jackOfDiamonds).toEqual(10);
expect(queenOfClubs).toEqual(10);
expect(kingOfHearts).toEqual(10);
});

// Case 4: Handle Ace (A):
test("should return 11 for Ace (A)", () => {
const aceOfSpades = getCardValue("A♠");
expect(aceOfSpades).toEqual(11);
});

// Case 5: Handle Invalid Cards:
test("should return undefined for invalid cards", () => {
const invalidCard = getCardValue("1♠");
expect(invalidCard).toEqual(undefined);
});
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;

// Loop through each character in the string
for (let i = 0; i < stringOfCharacters.length; i++) {
// If the current character matches the character we're looking for
if (stringOfCharacters[i] === findCharacter) {
count++;
}
}

return count;
}

module.exports = countChar;
6 changes: 6 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,9 @@ test("should count multiple occurrences of a character", () => {
// And a character char that does not exist within the case-sensitive str,
// When the function is called with these inputs,
// Then it should return 0, indicating that no occurrences of the char were found in the case-sensitive str.
test("should return 0 for no occurrences", () => {
const str = "hello";
const char = "x";
const count = countChar(str, char);
expect(count).toEqual(0);
});
18 changes: 17 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,21 @@
function getOrdinalNumber(num) {
return "1st";
// Handle special cases for teens (11th, 12th, 13th)
if (num % 100 >= 11 && num % 100 <= 13) {
return num + "th";
}

// Handle based on last digit
const lastDigit = num % 10;

if (lastDigit === 1) {
return num + "st";
} else if (lastDigit === 2) {
return num + "nd";
} else if (lastDigit === 3) {
return num + "rd";
} else {
return num + "th";
}
}

module.exports = getOrdinalNumber;
97 changes: 97 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 @@ -11,3 +11,100 @@ const getOrdinalNumber = require("./get-ordinal-number");
test("should return '1st' for 1", () => {
expect(getOrdinalNumber(1)).toEqual("1st");
});

// Case 2: Identify the ordinal number for 2
// When the number is 2,
// Then the function should return "2nd"

test("should return '2nd' for 2", () => {
expect(getOrdinalNumber(2)).toEqual("2nd");
});
Copy link
Contributor

Choose a reason for hiding this comment

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

To ensure thorough testing, we need broad scenario coverage. Listing individual values, however, can quickly lead to an unmanageable number of test cases.
Instead of writing tests for individual numbers, consider grouping all possible input values into meaningful categories. Then, select representative samples from each category to test. This approach improves coverage and makes our tests easier to maintain.

For example, we can prepare a test for numbers 2, 22, 132, etc. as

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

Copy link
Author

Choose a reason for hiding this comment

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

Tests are now grouped together.


// Case 3: Identify the ordinal number for 3
// When the number is 3,
// Then the function should return "3rd"

test("should return '3rd' for 3", () => {
expect(getOrdinalNumber(3)).toEqual("3rd");
});

// Case 4: Identify the ordinal number for 4
// When the number is 4,
// Then the function should return "4th"

test("should return '4th' for 4", () => {
expect(getOrdinalNumber(4)).toEqual("4th");
});

// Case 5: Identify the ordinal number for 11
// When the number is 11,
// Then the function should return "11th"

test("should return '11th' for 11", () => {
expect(getOrdinalNumber(11)).toEqual("11th");
});

// Case 6: Identify the ordinal number for 12
// When the number is 12,
// Then the function should return "12th"

test("should return '12th' for 12", () => {
expect(getOrdinalNumber(12)).toEqual("12th");
});

// Case 7: Identify the ordinal number for 13
// When the number is 13,
// Then the function should return "13th"

test("should return '13th' for 13", () => {
expect(getOrdinalNumber(13)).toEqual("13th");
});

// Case 8: Identify the ordinal number for 21
// When the number is 21,
// Then the function should return "21st"

test("should return '21st' for 21", () => {
expect(getOrdinalNumber(21)).toEqual("21st");
});

// Case 9: Identify the ordinal number for 22
// When the number is 22,
// Then the function should return "22nd"

test("should return '22nd' for 22", () => {
expect(getOrdinalNumber(22)).toEqual("22nd");
});

// Case 10: Identify the ordinal number for 23
// When the number is 23,
// Then the function should return "23rd"

test("should return '23rd' for 23", () => {
expect(getOrdinalNumber(23)).toEqual("23rd");
});

// Case 11: Identify the ordinal number for 101
// When the number is 101,
// Then the function should return "101st"

test("should return '101st' for 101", () => {
expect(getOrdinalNumber(101)).toEqual("101st");
});

// Case 12: Identify the ordinal number for 111
// When the number is 111,
// Then the function should return "111th"

test("should return '111th' for 111", () => {
expect(getOrdinalNumber(111)).toEqual("111th");
});

// Case 13: Identify the ordinal number for 0
// When the number is 0,
// Then the function should return "0th"

test("should return '0th' for 0", () => {
expect(getOrdinalNumber(0)).toEqual("0th");
});

19 changes: 17 additions & 2 deletions Sprint-3/2-practice-tdd/repeat.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
function repeat() {
return "hellohellohello";
function repeat(str, count) {
// Handle edge cases
if (count < 0) {
throw new Error("Count cannot be negative");
}

if (count === 0) {
return "";
}

// Build the repeated string
let result = "";
for (let i = 0; i < count; i++) {
result += str;
}

return result;
}

module.exports = repeat;
Loading