Skip to content

Glasgow | ITP May -25 | Mirabelle Morah | Module-Structuring-and-Testing-Data | Coursework/sprint-3 #628

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 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
17 changes: 13 additions & 4 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";
// read to the end, complete line 36, then pass your test here
else if (angle < 90) return "Acute angle";
else if (angle > 90 && angle < 180) return "Obtuse angle";
else if (angle === 180) return "Straight angle";
else if (angle > 180 && angle < 360) return "Reflex angle";
else return "Invalid angle";
}

// we're going to use this helper function to make our assertions easier to read
Expand Down Expand Up @@ -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);
// ====> 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
// ====> 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");
15 changes: 12 additions & 3 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,10 @@
// 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;
if (numerator < denominator) return true;
if (numerator >= denominator) return false;

}

// here's our helper again
Expand Down Expand Up @@ -40,14 +43,20 @@ 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);

// 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);
// ====> complete with your assertion
assertEquals(equalFraction, false);

// Stretch:
// What other scenarios could you test for?
// Zero Denominator check:
// Input: numerator = 1, denominator = 0
// target output: false
// Explanation: The fraction 1/0 is undefined because division by zero is not allowed. The function should return false.
const zeroDenominator = isProperFraction(1, 0);
assertEquals(zeroDenominator, false);
36 changes: 34 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,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;
const rank = card.slice(0, -1);
if (rank === "A") return 11;

if (["K", "Q", "J", "10"].includes(rank)) return 10;
const num = Number(rank);
if (num >= 2 && num <= 9) return num;

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

// You need to write assertions for your function to check it works in different cases
Expand All @@ -33,19 +40,44 @@ 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);

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

const jackofClubs = getCardValue("J♣");
assertEquals(jackofClubs, 10);

const queenofSpades = getCardValue("Q♠");
assertEquals(queenofSpades, 10);

const kingofHearts = getCardValue("K♥");
assertEquals(kingofHearts, 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."

try {
getCardValue("1♠");
console.assert(false, "Expected an error to be thrown for invalid card");
} catch (e) {
console.assert(
e.message === "Invalid card rank",
`Expected error message "Invalid card rank", got "${e.message}"`
);
}
19 changes: 8 additions & 11 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

// read to the end, complete line 36, then pass your test here
else if (angle < 90) return "Acute angle";
else if (angle > 90 && angle < 180) return "Obtuse angle";
else if (angle === 180) return "Straight angle";
else if (angle > 180 && angle < 360) return "Reflex angle";
else 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;
16 changes: 16 additions & 0 deletions Sprint-3/2-mandatory-rewrite/1-get-angle-type.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,30 @@ test("should identify right angle (90°)", () => {
// When the angle is less than 90 degrees,
// Then the function should return "Acute angle"

test("should identify acute angle (45°)", () => {
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 (135°)", () => {
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 (270°)", () => {
expect(getAngleType(270)).toEqual("Reflex angle");
});
7 changes: 3 additions & 4 deletions Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
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;
module.exports = isProperFraction;
14 changes: 14 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,21 @@ 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 false for a negative fraction", () => {
expect(isProperFraction(-4, 7)).toEqual(true);
});

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

// Case 4: Identify Equal Numerator and Denominator:
test("should return false for equal numerator and denominator", () => {
expect(isProperFraction(3, 3)).toEqual(false);
});
15 changes: 12 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,14 @@
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", "3", "4", "5", "6", "7", "8", "9"].includes(rank)) {
return Number(rank);
}

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

module.exports = getCardValue;
45 changes: 40 additions & 5 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,46 @@
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);
expect(getCardValue("A♥")).toEqual(11);
});

// Case 2: Handle Number Cards (2-10):

test("should return correct value for number cards (2-10)", () => {
for (let i = 2; i <= 10; i++) {
expect(getCardValue(`${i}♠`)).toEqual(i);
expect(getCardValue(`${i}♥`)).toEqual(i);
}
});

// Alternate long method

// test("should return the value of number cards (2-10)", () => {
// expect(getCardValue("2♠")).toEqual(2);
// expect(getCardValue("3♠")).toEqual(3);
// expect(getCardValue("4♠")).toEqual(4);
// expect(getCardValue("5♠")).toEqual(5);
// expect(getCardValue("6♠")).toEqual(6);
// expect(getCardValue("7♠")).toEqual(7);
// expect(getCardValue("8♠")).toEqual(8);
// expect(getCardValue("9♠")).toEqual(9);
// expect(getCardValue("10♠")).toEqual(10);
// });

// Case 3: Handle Face Cards (J, Q, K):
// Case 4: Handle Ace (A):
// Case 5: Handle Invalid Cards:
test("should return 10 or face cards (J, Q, K)", () => {
["J", "Q", "K"].forEach((rank) => {
expect(getCardValue(`${rank}♣`)).toEqual(10);
expect(getCardValue(`${rank}♦`)).toEqual(10);
});
});


// Case 4: Handle Invalid Cards:
test("should throw an error for invalid card", () => {
expect(() => getCardValue("Invalid")).toThrow("Invalid card rank");
expect(() => getCardValue("0x02♠")).toThrow("Invalid card rank");
expect(() => getCardValue("2.1♠")).toThrow("Invalid card rank");
expect(() => getCardValue("")).toThrow("Invalid card rank");
});
10 changes: 8 additions & 2 deletions Sprint-3/3-mandatory-practice/implement/count.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
function countChar(stringOfCharacters, findCharacter) {
return 5
let count = 0;
for (let i = 0; i < stringOfCharacters.length; i++) {
if (stringOfCharacters[i] === findCharacter) {
count++;
}
}
return count;
}

module.exports = countChar;
module.exports = countChar;
7 changes: 7 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,10 @@ 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 = "hello";
const char = "z";
const count = countChar(str, char);
expect(count).toEqual(0);
});
14 changes: 12 additions & 2 deletions Sprint-3/3-mandatory-practice/implement/get-ordinal-number.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
function getOrdinalNumber(num) {
return "1st";
if (typeof num !== "number" || !Number.isInteger(num)) {
throw new Error("Input must be an integer.");
}

const suffixes = ["th", "st", "nd", "rd"];
const remainder = num % 100;

return (
num +
(suffixes[(remainder - 20) % 10] || suffixes[remainder] || suffixes[0])
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 explain how this expression work?

Copy link
Author

Choose a reason for hiding this comment

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

Lines 1-3 checks the kind of input passed in the parameter. If input type is not a number or integer (eg if it's a string) then it gives the error message that the input must be an integer.

Line 6: 'Const suffixes' is an array or ordinal suffixes
Line 7: and 'const remainder' performs a modulus operation (as we learned in previous tests) on whatever input is passed in the 'num' parameter. Then it gets the digit that remains after that division. Eg 123 % 100 = 1.23 -- therefore const remainder = 23

Lines 9-11: performs multiple things:

suffixes[(remainder - 20) % 10]
Subtracts 20 from remainder, then gets last digit and uses that last digit to assign the ordinal form
Eg:

Remainder = 23: (23-20) % 10 = [0.3] 3, so const suffix [3] = rd. There it will assign 23 as '23rd'

Then for '|| suffixes[remainder]' if an answer is undefined it simple uses the remainder with the direct array number: like

if remainder = 1: suffixes[1] = "st"
if remainder = 2: suffixes[2] = "nd"

etc.

The final fallback if both previous parts are undefined it goes back to || suffixes[0] which adds 'th' to numbers like 11th etc.

Copy link
Contributor

@cjyuan cjyuan Jul 8, 2025

Choose a reason for hiding this comment

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

Would the code still work if we changed
(suffixes[(remainder - 20) % 10] || suffixes[remainder] || suffixes[0])
to
(suffixes[remainder] || suffixes[(remainder - 20) % 10] || suffixes[0])
(Change the order of the first two conditions)

If not, can you find a number in the range [0, 100) which the latter would fail?


> Eg 123 % 100 = 1.23 -- therefore const remainder = 23
  ^^^^^^^^^^^^^^^^^^^

This unusual notation makes it looks like you are treating % as a division operator.

You could just write 123 % 100 = 23 (all programmers should recognise what % operator does)

);
}

module.exports = getOrdinalNumber;
module.exports = getOrdinalNumber;
46 changes: 43 additions & 3 deletions Sprint-3/3-mandatory-practice/implement/get-ordinal-number.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,46 @@ 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("append 'st' to numbers ending in 1, except those ending in 11", () => {
expect(getOrdinalNumber(1)).toEqual("1st");
expect(getOrdinalNumber(21)).toEqual("21st");
expect(getOrdinalNumber(101)).toEqual("101st");
expect(getOrdinalNumber(111)).toEqual("111th"); // special case
});

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(102)).toEqual("102nd");
expect(getOrdinalNumber(112)).toEqual("112th"); // special case
});

test("append 'rd' to numbers ending in 3, except those ending in 13", () => {
expect(getOrdinalNumber(3)).toEqual("3rd");
expect(getOrdinalNumber(23)).toEqual("23rd");
expect(getOrdinalNumber(103)).toEqual("103rd");
expect(getOrdinalNumber(113)).toEqual("113th"); // special case
});

test("append 'th' to numbers ending in 4-9 or 0", () => {
expect(getOrdinalNumber(4)).toEqual("4th");
expect(getOrdinalNumber(10)).toEqual("10th");
expect(getOrdinalNumber(14)).toEqual("14th");
expect(getOrdinalNumber(19)).toEqual("19th");
expect(getOrdinalNumber(20)).toEqual("20th");
expect(getOrdinalNumber(100)).toEqual("100th");
});

test("append 'th' to numbers ending in 11, 12, 13", () => {
expect(getOrdinalNumber(11)).toEqual("11th");
expect(getOrdinalNumber(12)).toEqual("12th");
expect(getOrdinalNumber(13)).toEqual("13th");
expect(getOrdinalNumber(1112)).toEqual("1112th");
expect(getOrdinalNumber(1013)).toEqual("1013th");
});

test("throw error for non-integer inputs", () => {
expect(() => getOrdinalNumber("5")).toThrow("Input must be an integer.");
expect(() => getOrdinalNumber(5.5)).toThrow("Input must be an integer.");
expect(() => getOrdinalNumber(null)).toThrow("Input must be an integer.");
});
Loading