Skip to content

WESTMIDLANDS| ITP-MAY 2025| ROJA ALAGURAJAN| Module Structuring and Testing Data|Sprint-3 #670

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 3 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
36 changes: 12 additions & 24 deletions Sprint-3/1-key-implement/1-get-angle-type.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,44 @@
// Implement a function getAngleType
// Build up your function case by case, writing tests as you go
// The first test and case is written for you. The next case has a test, but no code.
// Execute this script in your terminal
// node 1-get-angle-type.js
// The assertion error will tell you what the expected output is
// Write the code to pass the test
// 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 "Acute angle";
if(angle>90 && angle<180) return "Obtuse angle";
if(angle === 180) return "Straight angle"
if (angle>180 && angle <360) return "Reflex angle"

// 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
function assertEquals(actualOutput, targetOutput) {
console.assert(
actualOutput === targetOutput,
`Expected ${actualOutput} to equal ${targetOutput}`
);
// read to the end, complete line 36, then pass your test here
}


// Acceptance criteria:

// Given an angle in degrees,
// When the function getAngleType is called with this angle,
// Then it should:

// Case 1: Identify Right Angles:
// When the angle is exactly 90 degrees,
// Then the function should return "Right angle"
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"
const acute = getAngleType(45);
assertEquals(acute, "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"
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 straightangle = getAngleType(180)
assertEquals(straightangle, "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 reflexangle = getAngleType(250)
assertEquals(reflexangle, "Reflex angle")
4 changes: 3 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 @@ -40,14 +40,16 @@ 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?
// What other scenarios could you test for?
35 changes: 30 additions & 5 deletions Sprint-3/1-key-implement/3-get-card-value.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

// This problem involves playing cards: https://en.wikipedia.org/wiki/Standard_52-card_deck

// You will need to implement a function getCardValue
Expand All @@ -7,10 +8,18 @@
// complete the rest of the tests and cases
// 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;
}

function getCardValue(card) {
const rankChar = card.slice(0, -1); // Remove the last character (suit)
const rankInt = parseInt(rankChar); // Try to convert rank to a number

if (rankChar === "A") return 11; // Ace is worth 11
else if (["K", "Q", "J"].includes(rankChar)) return 10; // Face cards worth 10
else if (rankInt >= 2 && rankInt <= 10) return rankInt; // 2–10 cards
else 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 @@ -30,22 +39,38 @@ 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,
// 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)
const sixofHearts = getCardValue("6♥");
assertEquals(sixofHearts, 6)

// ====> 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 facecards = getCardValue("Q♥")
assertEquals(facecards, 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 ace = getCardValue("A♥")
assertEquals(ace, 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."
function assertThrowsInvalidCard(fn) {
try {
fn();
console.assert(false, "Expected error for invalid card rank");
} catch (error) {
assertEquals(error.message, "Invalid card rank.");
}
}
11 changes: 7 additions & 4 deletions Sprint-3/2-mandatory-rewrite/1-get-angle-type.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
function getAngleType(angle) {
function getAngleType(angle) {
if (angle <= 0 || angle >= 360) return "Invalid 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";
}


Expand All @@ -10,7 +14,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
55 changes: 52 additions & 3 deletions Sprint-3/2-mandatory-rewrite/1-get-angle-type.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,59 @@
const getAngleType = require("./1-get-angle-type");

test("should identify right angle (90°)", () => {
/*test("should identify right angle (90°)", () => {
expect(getAngleType(90)).toEqual("Right angle");});

test("should identify acute angle (45°)", () => {
expect(getAngleType(45)).toEqual("Acute angle");});

test("should identify obtuse angle (120°)", () => {
expect(getAngleType(120)).toEqual("Obtuse angle");});

test("should identify straight angle (180°)", () => {
expect(getAngleType(180)).toEqual("Straight angle");});

test("should identify reflex angle (220°)", () => {
expect(getAngleType(220)).toEqual("Reflex angle");});

*/
Comment on lines +3 to +18
Copy link
Contributor

Choose a reason for hiding this comment

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

Any reason to keep this code? Code submitted to a PR is like the final work. As such, it is best practices to keep the code in the PR as clean as possible, including removal of unused code.

test("should identify right angle (angle === 90)", () => {
expect(getAngleType(90)).toEqual("Right angle");
});

// REPLACE the comments with the tests
test("should identify acute angle (angle < 90)", () => {
expect(getAngleType(45)).toEqual("Acute angle");
expect(getAngleType(30)).toEqual("Acute angle");
expect(getAngleType(0.1)).toEqual("Acute angle");
});

test("should identify obtuse angle (90 < angle < 180)", () => {
expect(getAngleType(120)).toEqual("Obtuse angle");
expect(getAngleType(135)).toEqual("Obtuse angle");
expect(getAngleType(179.9)).toEqual("Obtuse angle");
});

test("should identify straight angle (angle === 180)", () => {
expect(getAngleType(180)).toEqual("Straight angle");
});

test("should identify reflex angle (180 < angle < 360)", () => {
expect(getAngleType(220)).toEqual("Reflex angle");
expect(getAngleType(300)).toEqual("Reflex angle");
expect(getAngleType(359.999)).toEqual("Reflex angle");
expect(getAngleType(180.001)).toEqual("Reflex angle");
});

test("should return 'Invalid angle' for invalid inputs", () => {
expect(getAngleType(0)).toEqual("Invalid angle");
expect(getAngleType(-45)).toEqual("Invalid angle");
expect(getAngleType(360)).toEqual("Invalid angle");
expect(getAngleType(400)).toEqual("Invalid angle");
expect(getAngleType("90")).toEqual("Invalid angle");
expect(getAngleType(NaN)).toEqual("Invalid angle");
});



// make your test descriptions as clear and readable as possible

// Case 2: Identify Acute Angles:
Expand All @@ -21,4 +70,4 @@ test("should identify right angle (90°)", () => {

// 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"
// Then the function should return "Reflex angle"
6 changes: 3 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,6 @@
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);
}

module.exports = isProperFraction;
14 changes: 9 additions & 5 deletions Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ const isProperFraction = require("./2-is-proper-fraction");
test("should return true for a proper fraction", () => {
expect(isProperFraction(2, 3)).toEqual(true);
});
test("should return false for a proper fraction", () => {
expect(isProperFraction(5, 3)).toEqual(false);
});

// Case 2: Identify Improper Fractions:

// Case 3: Identify Negative Fractions:

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

if (rankChar === "A") return 11; //If rank is 'A' (Ace), return 11 points
if (["K", "Q", "J"].includes(rankChar)) return 10; //// If rank is 'K', 'Q', or 'J' (King, Queen, Jack), return 10 points

const validRanks = ["2", "3", "4", "5", "6", "7", "8", "9", "10"];
if (validRanks.includes(rankChar)) return Number(rankChar); // Define all valid numeric ranks as strings

throw new Error("Invalid card rank.");//If none of the above conditions match, the card rank is invalid, so throw an error
}
module.exports = getCardValue;


module.exports = getCardValue;

27 changes: 25 additions & 2 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,34 @@
const getCardValue = require("./3-get-card-value");

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



test("should return rank for Number Cards", () => {
const fiveofHearts = getCardValue("5♥");
expect(fiveofHearts).toEqual(5);
});
Comment on lines +10 to +13
Copy link
Contributor

Choose a reason for hiding this comment

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

"2♥" and "10♥" -- the boundary values usually worth testing.
Given that the set of possible values is small (only 9 different values), we can even use a loop to generate all possible test values.

The same principle applies to the test that deals with the face cards.


test("should return 10 for Face Cards", () => {
const facecards = getCardValue("Q♥");
expect(facecards).toEqual(10);
});

test("should return 11 for AceCard", () => {
const AceCard = getCardValue("A♥");
expect(AceCard).toEqual(11);
});
Comment on lines +20 to +23
Copy link
Contributor

Choose a reason for hiding this comment

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

This test is similar to the test at lines 3-7. (This comment was from the previous review).


test('throws error for invalid card', () => {
expect(() => getCardValue("ST")).toThrow("Invalid card rank.");
expect(() => getCardValue("0x02♠")).toThrow("Invalid card rank.");
expect(() => getCardValue("5.1♠")).toThrow("Invalid card rank.");
expect(() => getCardValue(" 5 ♠")).toThrow("Invalid card rank.");
});
// Case 2: Handle Number Cards (2-10):
// Case 3: Handle Face Cards (J, Q, K):
// Case 4: Handle Ace (A):
// Case 5: Handle Invalid Cards:
// Case 5: Handle Invalid Cards:
12 changes: 11 additions & 1 deletion 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
if (!stringOfCharacters.includes(findCharacter))
return 0;
else {
let count = 0;
for (let char of stringOfCharacters) {
if (char === findCharacter) {
count++;
}
}
return count;
}
}

module.exports = countChar;
24 changes: 20 additions & 4 deletions Sprint-3/3-mandatory-practice/implement/count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,31 @@ const countChar = require("./count");
// When the function is called with these inputs,
// Then it should correctly count overlapping occurrences of char (e.g., 'a' appears five times in 'aaaaa').

test("should count multiple occurrences of a character", () => {
const str = "aaaaa";
/*test("should count multiple occurrences of a character", () => {
const str = "aaaaaa";
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(5);
expect(count).toEqual(6);
});
test("should count multiple occurrences of a character", () => {
const str = "sftyeh";
const char = "s";
const count = countChar(str, char);
expect(count).toEqual(1);
});*/
test("should count occurrences of a character", () => {
expect(countChar("aaaaaa", "a")).toEqual(6);
expect(countChar("sftyeh", "s")).toEqual(1);
});

// Scenario: No Occurrences
// Given the input string str,
// 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 zero for no occurrences of a character", () =>{
const str1= "aaaaa";
const char1 = "s";
const count1 = countChar(str1, char1);
expect(count1).toEqual(0);

});
Loading