Skip to content

London | 25-ITP-May | Hendrine Zeraua | Sprint 3 | Structuring and Testing Data #661

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 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
6661734
Implement getAngleType function with unit tests for all angle cases
rarityXtreme Jul 15, 2025
ec17037
Add isProperFraction function and assertions for valid, invalid, and …
rarityXtreme Jul 15, 2025
d009f8c
Implement getCardValue function with tests
rarityXtreme Jul 15, 2025
9d54264
added completed getAngleType function from Key-implement exercise
rarityXtreme Jul 16, 2025
9f0f339
Add Jest tests for getAngleType function
rarityXtreme Jul 16, 2025
313cab7
add isProperFraction from key- Implement
rarityXtreme Jul 16, 2025
511f37e
Add edge case tests for isProperFraction function
rarityXtreme Jul 16, 2025
0dbe430
add function getCardValue from key-implement
rarityXtreme Jul 16, 2025
3c1595e
Add tests for getCardValue including edge cases and errors
rarityXtreme Jul 16, 2025
4f1c3fc
Implement countChar function
rarityXtreme Jul 16, 2025
ded4991
add test cases for countChar function
rarityXtreme Jul 16, 2025
18f5c89
implement getOrdinalNumber with suffix rules
rarityXtreme Jul 16, 2025
9865786
add test cases for getOrdinalNumber including exceptions
rarityXtreme Jul 16, 2025
d8530a7
implement repeat function with validation
rarityXtreme Jul 16, 2025
5a453cc
add test cases for repeat function including edge cases
rarityXtreme Jul 16, 2025
74254e9
implement credit card validator function
rarityXtreme Jul 16, 2025
a93be32
add Jest tests for credit card validator
rarityXtreme Jul 16, 2025
ea002b3
add while loop character finder with explanatory comments
rarityXtreme Jul 16, 2025
ceb0b9d
add passwordValidator function with full rule checks
rarityXtreme Jul 16, 2025
63ac7a2
add test cases for passwordValidator function
rarityXtreme Jul 16, 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
24 changes: 24 additions & 0 deletions Sprint-3/1-key-implement/1-get-angle-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,27 @@
// Write the code to pass the test
// Then, write the next test! :) Go through this process until all the cases are implemented

// getAngleType function:
// This function takes an angle in degrees and returns the type of angle.
// I implemented 5 cases: Right, Acute, Obtuse, Straight, and Reflex angles.
// I used conditional statements (if) to check the angle's value range.

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
}

// 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

// assertEquals:
// This helper function is used to check if the output from getAngleType
// matches the expected value. If not, it shows an error in the terminal.
function assertEquals(actualOutput, targetOutput) {
console.assert(
actualOutput === targetOutput,
Expand All @@ -27,6 +41,10 @@ function assertEquals(actualOutput, targetOutput) {
// When the function getAngleType is called with this angle,
// Then it should:


// Tests for getAngleType:
// Each test checks one angle type and helps confirm my function is working.

// Case 1: Identify Right Angles:
// When the angle is exactly 90 degrees,
// Then the function should return "Right angle"
Expand All @@ -43,14 +61,20 @@ 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"
const straight = getAngleType(180);
assertEquals(straight, "Straight angle");
// ====> write your test here, and then add a line to pass the test in the function above

// 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"
const reflex = getAngleType(210);
assertEquals(reflex, "Reflex angle");
// ====> write your test here, and then add a line to pass the test in the function above
25 changes: 24 additions & 1 deletion 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,8 @@
// 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 (denominator === 0) return false;
return Math.abs(numerator) < Math.abs(denominator);
}

// here's our helper again
Expand Down Expand Up @@ -40,14 +41,36 @@ 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
// 0/5 → true (0 is less than any non-zero number)
const zeroNumerator = isProperFraction(0, 5);
assertEquals(zeroNumerator, true);

// Negative denominator
// 3/-4 → true
const negativeDenominator = isProperFraction(3, -4);
assertEquals(negativeDenominator, true);

// Negative improper fraction
// -5/4 → false
const negativeImproper = isProperFraction(-5, 4);
assertEquals(negativeImproper, false);

// Zero denominator (invalid)
const zeroDenominator = isProperFraction(1, 0);
assertEquals(zeroDenominator, false);
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,7 +8,27 @@
// 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);

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

const numericRank = parseInt(rank);
if (!isNaN(numericRank) && numericRank >= 2 && numericRank <= 10) {
return numericRank;
}

if (["J", "Q", "K"].includes(rank)) {
return 10;
}

throw new Error("Invalid card rank.");
}

function assertEquals(actualOutput, targetOutput) {
console.assert(
actualOutput === targetOutput,
`Expected ${actualOutput} to equal ${targetOutput}`
);
}

// You need to write assertions for your function to check it works in different cases
Expand All @@ -33,19 +53,29 @@ 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 aceofSpades = getCardValue("A♠");
assertEquals(aceofSpades, 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."
try {
getCardValue("Z♣");
console.error("Test failed: Expected error for invalid card rank");
} catch (e) {
assertEquals(e.message, "Invalid card rank.");
}
8 changes: 5 additions & 3 deletions Sprint-3/2-mandatory-rewrite/1-get-angle-type.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@

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";

}

Expand All @@ -9,8 +13,6 @@ function getAngleType(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
Expand Down
19 changes: 17 additions & 2 deletions Sprint-3/2-mandatory-rewrite/1-get-angle-type.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const getAngleType = require("./1-get-angle-type");

test("should identify right angle (90°)", () => {
expect(getAngleType(90)).toEqual("Right angle");
test('returns "Right angle" for angle equal to 90', () => {
expect(getAngleType(90)).toBe("Right angle");
});

// REPLACE the comments with the tests
Expand All @@ -10,15 +10,30 @@ 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('returns "Acute angle" for angles less than 90', () => {
expect(getAngleType(45)).toBe("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('returns "Obtuse angle" for angles between 90 and 180', () => {
expect(getAngleType(120)).toBe("Obtuse angle");
});


// Case 4: Identify Straight Angles:
// When the angle is exactly 180 degrees,
// Then the function should return "Straight angle"
test('returns "Straight angle" for angle equal to 180', () => {
expect(getAngleType(180)).toBe("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('returns "Reflex angle" for angles between 180 and 360', () => {
expect(getAngleType(270)).toBe("Reflex angle");
});
8 changes: 5 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,8 @@

function isProperFraction(numerator, denominator) {
if (numerator < denominator) return true;
// add your completed function from key-implement here
}
if (denominator === 0) return false;
return Math.abs(numerator) < Math.abs(denominator);
}


module.exports = isProperFraction;
27 changes: 27 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,34 @@ test("should return true for a proper fraction", () => {
});

// Case 2: Identify Improper Fractions:
test("returns false for improper fraction (5/2)", () => {
expect(isProperFraction(5, 2)).toBe(false);
});


// Case 3: Identify Negative Fractions:
test("returns true for negative proper fraction (-4/7)", () => {
expect(isProperFraction(-4, 7)).toBe(true);
});


// Case 4: Identify Equal Numerator and Denominator:

test("returns false for equal numerator and denominator (3/3)", () => {
expect(isProperFraction(3, 3)).toBe(false);
});
// Zero numerator
test("returns true when numerator is 0 and denominator is non-zero", () => {
expect(isProperFraction(0, 5)).toBe(true);
expect(isProperFraction(0, -5)).toBe(true);
});
// Zero denominator
test("returns false when denominator is 0", () => {
expect(isProperFraction(1, 0)).toBe(false);
expect(isProperFraction(0, 0)).toBe(false);
});
// Negative denominator
test("handles negative denominators correctly", () => {
expect(isProperFraction(2, -3)).toBe(true);
expect(isProperFraction(-4, -5)).toBe(true);
});
19 changes: 16 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,18 @@

function getCardValue(card) {
// replace with your code from key-implement
return 11;
}
const rank = card.slice(0, -1);

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

const numericRank = parseInt(rank);
if (!isNaN(numericRank) && numericRank >= 2 && numericRank <= 10) {
return numericRank;
}

if (["J", "Q", "K"].includes(rank)) {
return 10;
}

throw new Error("Invalid card rank.");
}
module.exports = getCardValue;
21 changes: 21 additions & 0 deletions Sprint-3/2-mandatory-rewrite/3-get-card-value.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,27 @@ test("should return 11 for Ace of Spades", () => {
});

// Case 2: Handle Number Cards (2-10):
test("should return numeric value for number cards", () => {
expect(getCardValue("2♦")).toEqual(2);
expect(getCardValue("10♥")).toEqual(10);
expect(getCardValue("7♣")).toEqual(7);
});
// Case 3: Handle Face Cards (J, Q, K):
test("should return 10 for Jack, Queen, and King", () => {
expect(getCardValue("J♠")).toEqual(10);
expect(getCardValue("Q♥")).toEqual(10);
expect(getCardValue("K♦")).toEqual(10);
});

// Case 4: Handle Ace (A):
test("should return 11 for Ace of Hearts", () => {
expect(getCardValue("A♥")).toEqual(11);
});
// Case 5: Handle Invalid Cards:
test("should throw an error for invalid card rank", () => {
expect(() => getCardValue("1♠")).toThrow("Invalid card rank.");
expect(() => getCardValue("Z♣")).toThrow("Invalid card rank.");
expect(() => getCardValue("11♦")).toThrow("Invalid card rank.");
expect(() => getCardValue("")).toThrow("Invalid card rank.");
});

19 changes: 15 additions & 4 deletions Sprint-3/3-mandatory-practice/implement/count.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
function countChar(stringOfCharacters, findCharacter) {
return 5
}
// The test should fail because the current function always returns 5.
// function countChar(stringOfCharacters, findCharacter) {
// return 5
// }

module.exports = countChar;
// module.exports = countChar;
// Here the placeholder(5) is replaced to implement the function to pass the tests
function countChar(stringOfCharacters, findCharacter) {
let count = 0;
for (let char of stringOfCharacters) {
if (char === findCharacter) count++;
}
return count;
}

module.exports = countChar;
8 changes: 8 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,11 @@ 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 if character does not occur in the string", () => {
const str = "hello";
const char = "z";
const count = countChar(str, char);
expect(count).toEqual(0);
});

25 changes: 21 additions & 4 deletions Sprint-3/3-mandatory-practice/implement/get-ordinal-number.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
function getOrdinalNumber(num) {
return "1st";
}
// function getOrdinalNumber(num) {
// return "1st";
// }

// module.exports = getOrdinalNumber;

module.exports = getOrdinalNumber;
function getOrdinalNumber(num) {
const lastTwo = num % 100;
const lastDigit = num % 10;

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

if (lastDigit === 1) return `${num}st`;
if (lastDigit === 2) return `${num}nd`;
if (lastDigit === 3) return `${num}rd`;

return `${num}th`;
}

module.exports = getOrdinalNumber;
Loading