Skip to content

WESTMIDLANDS | ITP-MAY-25 | AHMAD EHSAS | MODULE STRUCTURING AND TESTING DATA SPRINT-3 #656

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
65fe894
Answered comment for count update in exercise -1
ahmadehsas Jun 16, 2025
532fd66
Implement getAngleType with complete test coverage and improved test …
ahmadehsas Jul 7, 2025
0cba6bb
Completed the rest the tests and cases
ahmadehsas Jul 7, 2025
8d6eb95
Implement getCardValue function with test cases for number, face, ace…
ahmadehsas Jul 9, 2025
2479c4f
Implement getAngleType function to classify angle types
ahmadehsas Jul 9, 2025
3fafedf
Add test cases for angle types and update Sprint-1 readme
ahmadehsas Jul 10, 2025
7a5fe08
Added function(isProperFraction)
ahmadehsas Jul 10, 2025
68f5551
Identified Improper, Negative and equal fractions.
ahmadehsas Jul 10, 2025
5dd3b88
Rewrote the Card value function and tests
ahmadehsas Jul 10, 2025
62ef41d
completed the countChar function with a short clear description and t…
ahmadehsas Jul 10, 2025
1443cbf
completed the No occurrences Scenario test
ahmadehsas Jul 10, 2025
1e2318b
completed the function getOrdinalNumber.
ahmadehsas Jul 11, 2025
edea881
the test is passed for get-ordinal-number.js
ahmadehsas Jul 11, 2025
dbf4e92
Implemented the function and the test was passed
ahmadehsas Jul 11, 2025
fad2100
Edited incorrect test cases and fixed logic to ensure all tests pass …
ahmadehsas Jul 12, 2025
1154f89
Add else condition to simplify my code.
ahmadehsas Jul 20, 2025
047df5d
Add jest .toThrow method for checking if the function can throw the e…
ahmadehsas Jul 20, 2025
ba0f73e
implement the the function getOrdinalNumber that works for any vali p…
ahmadehsas Jul 20, 2025
5aeb5c0
prepare tests for all valid integers.
ahmadehsas Jul 20, 2025
550f44d
combine tests for number cards and Face cards.
ahmadehsas Jul 21, 2025
e76a59c
update the function to hundle invalid input.
ahmadehsas Jul 21, 2025
3415822
update the test code to hundle expected cases.
ahmadehsas Jul 21, 2025
bf5f7f8
Match the error message in toThrow() matcher function.
ahmadehsas Jul 21, 2025
4f1e86a
Delete Sprint-1/1-key-exercises/1-count.js
ahmadehsas Jul 21, 2025
e5699a5
Delete Sprint-1/readme.md
ahmadehsas Jul 21, 2025
63d4861
Restored deleted files from Sprint-1 to fix accidental changes
ahmadehsas Jul 23, 2025
e749f57
update the tests for all valid integers.
ahmadehsas Jul 23, 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
6 changes: 0 additions & 6 deletions Sprint-1/1-key-exercises/1-count.js

This file was deleted.

35 changes: 0 additions & 35 deletions Sprint-1/readme.md

This file was deleted.

16 changes: 13 additions & 3 deletions Sprint-3/1-key-implement/1-get-angle-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@
// Then, write the next test! :) Go through this process until all the cases are implemented

function getAngleType(angle) {
if (angle === 90) return "Right angle";
// read to the end, complete line 36, then pass your test here
if (angle === 90) return "Right angle";
if (angle < 90) return "Acute angle";
if (angle > 90 && angle < 180) return "Obtuse angle";
if (angle === 180) return "Straight angle";
if (angle > 180 && angle < 360) return "Reflex angle";

// read to the end, complete line 36, then pass your test here
}

// we're going to use this helper function to make our assertions easier to read
Expand Down Expand Up @@ -43,14 +48,19 @@ 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);
assertEquals(obtuse, "Obtuse angle");
// ====> write your test here, and then add a line to pass the test in the function above

// 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
// ====> write your test here, and then add a line to pass the test in the function above
const reflex = getAngleType(270);
assertEquals(reflex, "Reflex angle");
7 changes: 5 additions & 2 deletions Sprint-3/1-key-implement/2-is-proper-fraction.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
// write one test at a time, and make it pass, build your solution up methodically

function isProperFraction(numerator, denominator) {
if (numerator < denominator) return true;
if (Math.abs(numerator) < denominator) return true; // This version of code works correctly for proper and negative fractions.
if (Math.abs(numerator) >= Math.abs(denominator)) return false;
if (Math.abs(numerator) === Math.abs(denominator)) return false;
}

// here's our helper again
Expand Down Expand Up @@ -40,13 +42,14 @@ 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);
// ====> complete with your assertion
assertEquals(negativeFraction, true); // assertion for negative fraction.

// 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); // assertion for equal numerator and denominator.
// ====> complete with your assertion

// Stretch:
Expand Down
34 changes: 32 additions & 2 deletions Sprint-3/1-key-implement/3-get-card-value.js
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) {
if (rank === "A") return 11;
const rank = card.slice(0, -1); // get the rank (before the suit symbol)

if (!isNaN(rank)) {
return Number(rank); // Number card
}

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

if (rank === "A") return 11; // Ace

// Anything else is invalid
throw new Error("Invalid card rank");
}

// if the rank is not a number or a face card, throw an error(invalid card rank).

// You need to write assertions for your function to check it works in different cases
// we're going to use this helper function to make our assertions easier to read
// if the actual output matches the target output, the test will pass
Expand All @@ -33,13 +48,23 @@ 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♥");
// ====> write your test here, and then add a line to pass the test in the function above
assertEquals(fiveofHearts, 5);

const tenofDiamonds = getCardValue("10♦");
assertEquals(tenofDiamonds, 10);

// 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 jackofClubs = getCardValue("J♣");
assertEquals(jackofClubs, 10);

const queenofHearts = getCardValue("Q♥");
assertEquals(queenofHearts, 10);

const kingofSpades = getCardValue("K♠");
assertEquals(kingofSpades, 10);
// Handle Ace (A):
// Given a card with a rank of "A",
// When the function is called with an Ace,
Expand All @@ -49,3 +74,8 @@ const fiveofHearts = getCardValue("5♥");
// 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."
try {
getCardValue("Z♠");
} catch (error) {
console.log("Caught error:", error.message); // Should say "Invalid card rank"
}
17 changes: 7 additions & 10 deletions Sprint-3/2-mandatory-rewrite/1-get-angle-type.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
function getAngleType(angle) {
if (angle === 90) return "Right angle";
// replace with your completed function from key-implement
if (angle < 90) return "Acute 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";
}








// Don't get bogged down in this detail
// Jest uses CommonJS module syntax by default as it's quite old
// We will upgrade our approach to ES6 modules in the next course module, so for now
// We will upgrade our approach to ES6 modules in the next course module, so for now
// we have just written the CommonJS module.exports syntax for you
module.exports = getAngleType;
module.exports = getAngleType;
29 changes: 16 additions & 13 deletions Sprint-3/2-mandatory-rewrite/1-get-angle-type.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,22 @@ test("should identify right angle (90°)", () => {

// REPLACE the comments with the tests
// make your test descriptions as clear and readable as possible
test("should identify acute angle (< 90°)", () => {
expect(getAngleType(45)).toEqual("Acute angle");
});
// Case 2: identify Acute Angles, When the angle is less than 90 degrees, than the function should return "Acute angle".

// Case 2: Identify Acute Angles:
// When the angle is less than 90 degrees,
// Then the function should return "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(120)).toEqual("Obtuse angle");
});
// Case 3: identify Obtuse Angles, When the angle is greater than 90 degrees and less than 180 degrees, than the function should return "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 4: identify Straight Angles, When the angle is exactly 180 degrees, than the function should return "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");
});
// Case 5: identify Reflex Angles, When the angle is greater than 180 degrees and less than 360 degrees, the function should return "Reflex Angle".
12 changes: 9 additions & 3 deletions Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
function isProperFraction(numerator, denominator) {
if (numerator < denominator) return true;
// add your completed function from key-implement here
if (denominator === 0) {
return false;
} else if (Math.abs(numerator) < Math.abs(denominator)) {
return true;
} else {
return false;
}
}

module.exports = isProperFraction;
// This version of code works correctly for proper and negative fractions.
module.exports = isProperFraction;
12 changes: 12 additions & 0 deletions Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,19 @@ test("should return true for a proper fraction", () => {
});

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

// Case 3: Identify Negative Fractions:
test("should return true for negative fractions", () => {
const negativeFraction = isProperFraction(-4, 7);
expect(negativeFraction).toEqual(true);
});

// Case 4: Identify Equal Numerator and Denominator:
test("should return false for equal numerator and denominator", () => {
const equalFraction = isProperFraction(3, 3);
expect(equalFraction).toEqual(false);
});
14 changes: 11 additions & 3 deletions Sprint-3/2-mandatory-rewrite/3-get-card-value.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
function getCardValue(card) {
// replace with your code from key-implement
return 11;
const rank = card.slice(0, -1); // get the value part: A, 2–10, J, Q, K

if (rank === "A") return 11;
if (["K", "Q", "J"].includes(rank)) return 10;

const number = parseInt(rank);
Copy link
Contributor

Choose a reason for hiding this comment

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

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

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

Consider looking up how parseInt() work.

Copy link
Author

Choose a reason for hiding this comment

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

Yes, the function returns the card value based on the card's rank.

Copy link
Author

Choose a reason for hiding this comment

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

Yes, the function returns a value based on the card's rank condition.
Screenshot 2025-07-20 6 08 25 PM
Screenshot 2025-07-20 6 05 31 PM
I've attached the screenshots.

Copy link
Contributor

Choose a reason for hiding this comment

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

parseInt() can return an integer but do you consider the values I suggested valid?

Copy link
Author

Choose a reason for hiding this comment

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

As parseInt() returns Integers, and the values with decimal numbers can only return an integer, not the decimal part. So technically, the values you suggested are valid in the sense that it doesn't cause an error. But it does not reflect the true value of the number. It truncates the decimal.

Copy link
Contributor

Choose a reason for hiding this comment

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

The objective of the function is to determine the value of the rank of a card.

If you were to design a card game, would you allow the card to be displayed as
"2.9999999999999999999999♠", "3AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA♠"?

if (!isNaN(number) && number >= 2 && number <= 10) return number;

throw new Error("Invalid card");
}
module.exports = getCardValue;

module.exports = getCardValue;
27 changes: 24 additions & 3 deletions Sprint-3/2-mandatory-rewrite/3-get-card-value.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
const getCardValue = require("./3-get-card-value");

test("should return 11 for Ace of Spades", () => {
const aceofSpades = getCardValue("A♠");
expect(aceofSpades).toEqual(11);
});
const aceofSpades = getCardValue("A♠");
expect(aceofSpades).toEqual(11);
});

test("should return correct values for number cards", () => {
expect(getCardValue("5♥")).toEqual(5);
expect(getCardValue("10♦")).toEqual(10);
});

test("should return for face cards J, Q, K", () => {
expect(getCardValue("J♣")).toEqual(10);
expect(getCardValue("Q♦")).toEqual(10);
expect(getCardValue("K♠")).toEqual(10);
});

test("throws an error or invalid card", () => {
expect(() => getCardValue("Z♠")).toThrow("Invalid card");
});

// try {
// getCardValue("Z♠");
//} catch (error) {
// console.log("Caught error:", error.message);
//}

// Case 2: Handle Number Cards (2-10):
// Case 3: Handle Face Cards (J, Q, K):
Expand Down
14 changes: 12 additions & 2 deletions Sprint-3/3-mandatory-practice/implement/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; // start a count with 0
for (let char of stringOfCharacters) {
// repeat for each character in the string
if (char === findCharacter) {
// check if the character matches the one we are looking for.
count++; // if it does increase the count by 1.
}
}

return count;
}
console.log(countChar("aAaAaAaAa", "a")); // example usage should return 5.

module.exports = countChar;
module.exports = countChar;
9 changes: 9 additions & 0 deletions Sprint-3/3-mandatory-practice/implement/count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,12 @@ 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 when character does not exist in the string", () => {
const str = "aaaaa";
const char = "b";
const result = countChar(str, char);
expect(result).toEqual(0);
});
// the above test cases are for the countChar function that counts the number of times a character occurs in a string.
// this function should return 0 because the character `b` does not exist in the string "aaaaa".
20 changes: 17 additions & 3 deletions Sprint-3/3-mandatory-practice/implement/get-ordinal-number.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
function getOrdinalNumber(num) {
return "1st";
function getOrdinalNumber(n) {
let ord = "th";

if (n % 10 == 1 && n % 100 != 11) {
ord = "st";
} else if (n % 10 == 2 && n % 100 != 12) {
ord = "nd";
} else if (n % 10 == 3 && n % 100 != 13) {
ord = "rd";
}

return n + ord;
}
console.log(getOrdinalNumber(1)); // The output should be "1st"

// in this function, we got the ordinal number for 1.
// we used a simple if statement to check if the number is 1 and return "1st".

module.exports = getOrdinalNumber;
module.exports = getOrdinalNumber;
Copy link
Contributor

Choose a reason for hiding this comment

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

The tests in this Jest script are incomplete. When you have fully implemented the function in Sprint-3/3-mandatory-practice/implement/get-ordinal-number.js, can you prepare tests in this script to cover all valid integers in the following way:

Group all possible input values into meaningful categories, and 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
Contributor

Choose a reason for hiding this comment

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

Can you update the tests in this script so that they cover all valid integers?

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ const getOrdinalNumber = require("./get-ordinal-number");
// When the number is 1,
// Then the function should return "1st"

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

test("append 'st' to numbers ending in 1, except those ending in 11", () => {
expect(getOrdinalNumber(1)).toEqual("1st");
expect(getOrdinalNumber(21)).toEqual("21st");
expect(getOrdinalNumber(121)).toEqual("121st");
});
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you update the tests in this script so that they cover all valid integers?

Loading