Skip to content
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

LONDON | Pooriya Ketabi | Module-Structuring-and-Testing-Data | Sprint 3 #230

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
9 changes: 9 additions & 0 deletions CYF/ordinal-testing-example/get-ordinal-number.test.js
Copy link
Member

Choose a reason for hiding this comment

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

Ideally this directory wouldn't be included in your coursework PR, as this was just your local work during the prep, rather than the exercises you're submitting for review.

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for pointing that out.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function getOrdinalNumber() {
return "1st";
}

test("converts 1 to an ordinal number", function() {
expect(getOrdinalNumber(1)).toEqual("1st");
expect(getOrdinalNumber(11)).toEqual("11th");
expect(getOrdinalNumber(21)).toEqual("21st");
});
3,649 changes: 3,649 additions & 0 deletions CYF/ordinal-testing-example/package-lock.json

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions CYF/ordinal-testing-example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "ordinal-testing-example",
"description": "An example application showing how to write tests using the jest framework",
"scripts": {
"test": "jest"
},
"devDependencies": {
"jest": "^29.7.0"
}
}
19 changes: 19 additions & 0 deletions Sprint-3/implement/get-angle-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,22 @@
// Identify Reflex Angles:
// When the angle is greater than 180 degrees and less than 360 degrees,
// Then the function should return "Reflex angle"


function getAngleType(angle) {
if (angle === 90) {
return "Right angle";
} else if (angle < 90) {
return "Acute angle";
} else if (angle > 90 && 180 > angle) {
return "Obtuse angle";
} else if (angle === 180) {
return "Straight angle";
} else if (angle > 180 && 360 > angle) {
return "Reflex angle";
} else {
return "Invalid Input"
}
}

console.log(getAngleType(361));
19 changes: 19 additions & 0 deletions Sprint-3/implement/get-card-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,22 @@
// 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 getCardValue(card) {
let rank = card.slice(0, -1);
if (parseInt(rank) >= 2 && parseInt(rank) <= 10) {
return parseInt(rank);
Copy link
Member

Choose a reason for hiding this comment

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

You're calling parseInt on the same input here three times - can you think how you can avoid needing to repeat this? Why is doing the same thing three times not ideal?

Copy link
Author

Choose a reason for hiding this comment

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

It's not ideal, because of performance and readability (debugging). Therefore it's better to store it in a variable and then use the variable instead.
Thank you for mentioning it.

}
switch (rank) {
case "J":
case "Q":
case "K":
return 10;
case "A":
return 11;
default:
throw new Error("Invalid card rank");
}
}

module.exports = getCardValue;
20 changes: 20 additions & 0 deletions Sprint-3/implement/get-card-value.test.js
Copy link
Member

Choose a reason for hiding this comment

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

These are some really nice tests, good job!

Copy link
Author

Choose a reason for hiding this comment

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

Thank you. I'm glad you found it nice.

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const getCardValue = require("./get-card-value");
test("should return the correct value for number cards (2-10)", () => {
expect(getCardValue("2♠")).toBe(2);
expect(getCardValue("9♥")).toBe(9);
expect(getCardValue("10♦")).toBe(10);
});

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

test("should return 11 for an Ace (A)", () => {
expect(getCardValue("A♠")).toBe(11);
});

test("should throw an error for an invalid card rank", () => {
expect(() => getCardValue("Z♠")).toThrow("Invalid card rank");
});
43 changes: 43 additions & 0 deletions Sprint-3/implement/is-proper-fraction.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,46 @@
// 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.
// These acceptance criteria cover a range of scenarios to ensure that the isProperFraction function handles both proper and improper fractions correctly and handles potential errors such as a zero denominator.

function isProperFraction(numerator, denominator) {
if (denominator === 0) {
throw new Error("Denominator cannot be zero");
}
return Math.abs(numerator) < Math.abs(denominator);
}

// ---------------------------------------- Tests: ----------------------------------------
// -------- Proper Fraction --------
console.assert(
isProperFraction(2, 3) === true,
Copy link
Member

Choose a reason for hiding this comment

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

We actually don't need to put === true or === false in assertions. What we need is an expression which evaluates to a boolean (true or false), and isProperFraction already returns a boolean, so we can remove the === true (and write !isProperFraction(...) where we want to check something is false.

Almost always, writing something === true or something === false should be replaced by just something or !something :)

Copy link
Author

Choose a reason for hiding this comment

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

I truly appreciate your detailed explanations. Your guidance has been really helpful, and I’ve made the necessary changes accordingly.

"Test failed: 2/3 should be a proper fraction"
);

// -------- Improper Fraction --------
console.assert(
isProperFraction(5, 2) === false,
"Test failed: 5/2 should not be a proper fraction"
);

// -------- Zero Denominator --------
try {
isProperFraction(3, 0);
console.error("Test failed: Denominator is zero, but no error was thrown");
} catch (e) {
console.assert(
e.message === "Denominator cannot be zero",
"Test failed: Error message is incorrect"
);
}

// -------- Negative Fraction --------
console.assert(
isProperFraction(-4, 7) === true,
"Test failed: -4/7 should be a proper fraction"
);

// -------- Equal Numerator and Denominator --------
console.assert(
isProperFraction(3, 3) === false,
"Test failed: 3/3 should not be a proper fraction"
);
13 changes: 13 additions & 0 deletions Sprint-3/implement/is-valid-triangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,16 @@
// Then it should return true because the input forms a valid triangle.

// This specification outlines the behavior of the isValidTriangle function for different input scenarios, ensuring it properly checks for invalid side lengths and whether they form a valid triangle according to the Triangle Inequality Theorem.

function isValidTriangle(a, b, c) {
if (a <= 0 || b <= 0 || c <= 0) {
return false;
}
if (a + b > c && a + c > b && b + c > a) {
return true;
} else {
return false;
}
}

module.exports = isValidTriangle;
28 changes: 28 additions & 0 deletions Sprint-3/implement/is-valid-triangle.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const isValidTriangle = require("./is-valid-triangle");

test("should return true for a valid triangle", () => {
expect(isValidTriangle(3, 4, 5)).toBe(true);
expect(isValidTriangle(5, 5, 5)).toBe(true);
expect(isValidTriangle(8, 10, 6)).toBe(true);
});

test("should return true for sides 2, 2, 3", () => {
expect(isValidTriangle(2, 2, 3)).toBe(true);
});

test("should return false (violates inequality)", () => {
expect(isValidTriangle(1, 2, 3)).toBe(false);
});

test("should return false as one side length is 0", () => {
expect(isValidTriangle(0, 5, 7)).toBe(false);
});

test("should return false as one side has negative length", () => {
expect(isValidTriangle(3, -4, 5)).toBe(false);
});

test("should return false (violates inequality)", () => {
expect(isValidTriangle(5, 1, 1)).toBe(false);
expect(isValidTriangle(1, 1, 2)).toBe(false);
});
Loading