Skip to content

West Midland | ITP-May-2025 | Saleh Yousef | Module-Structuring-and-Testing-Data | sprint 3 #601

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 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
3856c49
new file: prep/Variables
SalehOmar-Y Jun 8, 2025
6e565c2
new file
SalehOmar-Y Jun 8, 2025
ca705a4
Implement angle type identification in getAngleType function
Jun 23, 2025
5c55f19
Add assertions for negative fractions and equal numerator/denominator…
Jun 23, 2025
c62f6d2
Refactor getCardValue function to correctly return card values and up…
Jun 23, 2025
00efebb
Refactor isProperFraction function for clarity and accuracy; enhance …
Jun 23, 2025
5bb2de5
completed 2-mandatory-rewrite
Jun 23, 2025
8e56555
Implement countChar function to accurately count character occurrence…
Jun 24, 2025
9e470f2
repeat.test.js completed
Jun 24, 2025
4882807
Update 1-get-angle-type.js
SalehOmar-Y Jul 6, 2025
a8e8735
Update get-ordinal-number.test.js
SalehOmar-Y Jul 6, 2025
c041ee4
Update repeat.test.js
SalehOmar-Y Jul 6, 2025
dc54439
Update 3-get-card-value.test.js
SalehOmar-Y Jul 6, 2025
f6db2fa
Update 3-get-card-value.test.js
SalehOmar-Y Jul 6, 2025
af76630
Update count.test.js
SalehOmar-Y Jul 6, 2025
f349caa
Update count.test.js
SalehOmar-Y Jul 6, 2025
2f6b738
Update 3-get-card-value.js
SalehOmar-Y Jul 6, 2025
ab2284b
Update 1-get-angle-type.test.js
SalehOmar-Y Jul 6, 2025
826d8c1
Update repeat.test.js
SalehOmar-Y Jul 6, 2025
4f3c922
Update get-ordinal-number.test.js
SalehOmar-Y Jul 6, 2025
d39457e
Update count.test.js
SalehOmar-Y Jul 6, 2025
aba4df6
Update 3-get-card-value.test.js
SalehOmar-Y Jul 6, 2025
4ce219b
Refactor code structure for improved readability and maintainability
Jul 6, 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
1 change: 1 addition & 0 deletions Project-CLI-Treasure-Hunt
Submodule Project-CLI-Treasure-Hunt added at 7cc7f5
13 changes: 11 additions & 2 deletions Sprint-3/1-key-implement/1-get-angle-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

function getAngleType(angle) {
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
}

Expand All @@ -33,6 +37,7 @@ function assertEquals(actualOutput, targetOutput) {
const right = getAngleType(90);
assertEquals(right, "Right angle");


// Case 2: Identify Acute Angles:
// When the angle is less than 90 degrees,
// Then the function should return "Acute angle"
Expand All @@ -43,14 +48,18 @@ 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(330);
assertEquals(reflex, "Reflex angle");
8 changes: 8 additions & 0 deletions Sprint-3/1-key-implement/2-is-proper-fraction.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,22 @@ 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(properFraction, 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(improperFraction, false);
// ====> complete with your assertion

// Stretch:
// What other scenarios could you test for?
// negative denominators, zero denominators, zero numerators.
// Negative Denominator check:
// Input: numerator = 0, denominator = -3
// target output: true
const negativeDenominator = isProperFraction(0, -3);
assertEquals(negativeDenominator, true);
18 changes: 13 additions & 5 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,14 @@
// 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;
if (card === "A") return 11;
if (["K", "Q", "J", "10"].includes(card)) return 10;
const num = Number(card);
if (num >= 2 && num <= 9 && card === num.toString()) return num;
throw new 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 @@ -25,26 +30,29 @@ function assertEquals(actualOutput, targetOutput) {
// Given a card string in the format "A♠" (representing a card in blackjack - the last character will always be an emoji for a suit, and all characters before will be a number 2-10, or one letter of J, Q, K, A),
// When the function getCardValue is called with this card string as input,
// Then it should return the numerical card value
const aceofSpades = getCardValue("A");
const aceofSpades = getCardValue("A");
assertEquals(aceofSpades, 11);

// Handle Number Cards (2-10):
// Given a card with a rank between "2" and "9",
// 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♥");
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 tenOfDiamonds = getCardValue("10");
assertEquals(tenOfDiamonds, 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 handleAce = getCardValue("A");
assertEquals(handleAce, 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,
Expand Down
16 changes: 7 additions & 9 deletions Sprint-3/2-mandatory-rewrite/1-get-angle-type.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
function getAngleType(angle) {
if (angle === 90) return "Right angle";
// replace with your completed function from key-implement

if (angle <= 0 || angle >= 360) return "Invalid angle";
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";
}








// 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 have just written the CommonJS module.exports syntax for you
module.exports = getAngleType;
module.exports = getAngleType;
22 changes: 13 additions & 9 deletions Sprint-3/2-mandatory-rewrite/1-get-angle-type.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@ test("should identify right angle (90°)", () => {
// make your test descriptions as clear and readable as possible

// Case 2: Identify Acute Angles:
// When the angle is less than 90 degrees,
// Then the function should return "Acute angle"
test.each([1, 45, 89.999])("Should identify acute angle (%f°)", (angle) => {
expect(getAngleType(angle)).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.each([91, 120, 179.999])("Should identify obtuse angle (%f°)", (angle) => {
expect(getAngleType(angle)).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.each([181, 300, 359.999])("Should identify reflex angle (%f°)", (angle) => {
expect(getAngleType(angle)).toEqual("Reflex angle");
});
35 changes: 33 additions & 2 deletions Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,37 @@
function isProperFraction(numerator, denominator) {
if (numerator < denominator) return true;
// add your completed function from key-implement here
if (denominator === 0) throw new Error("Denominator cannot be zero.");
return Math.abs(numerator) < Math.abs(denominator);
}

// here's our helper again
function assertEquals(actualOutput, targetOutput) {
console.assert(
actualOutput === targetOutput,
`Expected ${actualOutput} to equal ${targetOutput}`
);
}

// Proper Fraction check:
const properFraction = isProperFraction(2, 3);
assertEquals(properFraction, true);

// Improper Fraction check:
const improperFraction = isProperFraction(5, 2);
assertEquals(improperFraction, false);

// Negative Fraction check:
const negativeFraction = isProperFraction(-4, 7);
assertEquals(negativeFraction, true);

// Equal Numerator and Denominator check:
const equalFraction = isProperFraction(3, 3);
assertEquals(equalFraction, false);

// Negative Denominator check:
const negativeDenominator = isProperFraction(0, -3);
assertEquals(negativeDenominator, true);




module.exports = isProperFraction;
11 changes: 9 additions & 2 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,14 @@ test("should return true for a proper fraction", () => {
});

// Case 2: Identify Improper Fractions:

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

test("should return true for a negative proper fraction", () => {
expect(isProperFraction(-4, 7)).toEqual(true);
});
// Case 4: Identify Equal Numerator and Denominator:
test("should return false for equal numerator and denominator", () => {
expect(isProperFraction(3, 3)).toEqual(false);
});
11 changes: 8 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,10 @@
function getCardValue(card) {
// replace with your code from key-implement
return 11;
const rank = card.slice(0, -1);

if (rank === "A") return 11;
if (["K", "Q", "J", "10"].includes(rank)) return 10;
if (/^[2-9]$/.test(rank)) return Number (rank);

throw new Error("Invalid card rank.");
}
module.exports = getCardValue;
module.exports = getCardValue;
40 changes: 36 additions & 4 deletions Sprint-3/2-mandatory-rewrite/3-get-card-value.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,43 @@
const getCardValue = require("./3-get-card-value");

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

// Case 2: Handle Number Cards (2-10):
test("should return correct numeric value for number cards", () => {
expect(getCardValue("5♥")).toEqual(5);
expect(getCardValue("3♣")).toEqual(3);
expect(getCardValue("4♦")).toEqual(4);


});
test('returns 10 for Ten of Diamonds', () => {
expect(getCardValue('10♦')).toEqual(10);
});

// Case 3: Handle Face Cards (J, Q, K):
// Case 4: Handle Ace (A):
// Case 5: Handle Invalid Cards:
test.each(["K♣", "Q♦", "J♥"])(
'returns 10 for %s',
(card) => {
expect(getCardValue(card)).toEqual(10);
});

// Case 4: Handle Invalid Cards:
test("should throw an error for invalid card rank", () => {
expect(() => getCardValue("Z♣")).toThrow("Invalid card rank.");
});
test("should throw an error for invalid card rank", () => {
expect(() => getCardValue("11♠")).toThrow("Invalid card rank.");
});


test("should throw an error for hex literal rank", () => {
expect(() => getCardValue("0x02♠")).toThrow("Invalid card rank.");
});
test("should throw an error for float rank", () => {
expect(() => getCardValue("2.1♠")).toThrow("Invalid card rank.");
});
test("should throw an error for underscore rank", () => {
expect(() => getCardValue("00_02♠")).toThrow("Invalid card rank.");
});
11 changes: 8 additions & 3 deletions Sprint-3/3-mandatory-practice/implement/count.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
function countChar(stringOfCharacters, findCharacter) {
return 5
let count = 0;
for (let char of stringOfCharacters) {
if (char === findCharacter) {
count++;
}
}
return count;
}

module.exports = countChar;
module.exports = countChar;
14 changes: 14 additions & 0 deletions Sprint-3/3-mandatory-practice/implement/count.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// implement a function countChar that counts the number of times a character occurs in a string
const countChar = require("./count");

// Given a string str and a single character char to search for,
// When the countChar function is called with these inputs,
// Then it should:
Expand All @@ -22,3 +23,16 @@ 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 count no occurrences of a character", () => {
const str = "ssss";
const char = "h";
const count = countChar(str, char);
expect(count).toEqual(0);});

// Scenario: Case sensitivity
test("should respect case sensitivity when counting characters", () => {
const str = "AbcA";
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(0);
});
10 changes: 9 additions & 1 deletion Sprint-3/3-mandatory-practice/implement/get-ordinal-number.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
function getOrdinalNumber(num) {
return "1st";
const lastTwo = num % 100;
if (lastTwo >= 11 && lastTwo <= 13) {
return num + "th";
}
const last = num % 10;
if (last === 1) return num + "st";
if (last === 2) return num + "nd";
if (last === 3) return num + "rd";
return num + "th";
}

module.exports = getOrdinalNumber;
29 changes: 24 additions & 5 deletions Sprint-3/3-mandatory-practice/implement/get-ordinal-number.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,29 @@ const getOrdinalNumber = require("./get-ordinal-number");
// continue testing and implementing getOrdinalNumber for additional cases
// Write your tests using Jest - remember to run your tests often for continual feedback

// Case 1: Identify the ordinal number for 1
// When the number is 1,
// Then the function should return "1st"
// Case 1: Identify numbers that end with 1 but are not 11

test("should return '1st' for 1", () => {
expect(getOrdinalNumber(1)).toEqual("1st");
test.each([1, 21, 31])("should return '%ist' for %i", (num) => {
expect(getOrdinalNumber(num)).toEqual(`${num}st`);
});

// Case 2: identify the ordinal numbers that end with 2 but are not 12

test.each([2, 22, 32])("should return '%ind' for %i", (num) => {
expect(getOrdinalNumber(num)).toEqual(`${num}nd`);
});

// Case 3: identify the ordinal numbers that end with 3 but not 13
test.each([3, 23, 33])("should return '%ird' for %i", (num) => {
expect(getOrdinalNumber(num)).toEqual(`${num}rd`);
});

// Case 4: everything else with th
test.each([4, 5, 6, 10, 14, 20, 40])("returns '%ith' for %i", (num) => {
expect(getOrdinalNumber(num)).toEqual(`${num}th`);
});

// Case 5: 11th 12th 13th (special teen case)
test.each([11, 12, 13])("should return '%ith' for %i", (num) => {
expect(getOrdinalNumber(num)).toEqual(`${num}th`);
});
5 changes: 3 additions & 2 deletions Sprint-3/3-mandatory-practice/implement/repeat.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
function repeat() {
return "hellohellohello";
function repeat(str, count) {
if (count < 0) throw new Error("Count must be non-negative");
return str.repeat(count);
}

module.exports = repeat;
Loading