-
-
Notifications
You must be signed in to change notification settings - Fork 72
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
base: main
Are you sure you want to change the base?
Changes from 5 commits
c5fa34c
a09d789
a5ecab6
380cd2b
bffd6d9
fd1dbe5
51d4637
c004901
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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"); | ||
}); |
Large diffs are not rendered by default.
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" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're calling There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
} | ||
switch (rank) { | ||
case "J": | ||
case "Q": | ||
case "K": | ||
return 10; | ||
case "A": | ||
return 11; | ||
default: | ||
throw new Error("Invalid card rank"); | ||
} | ||
} | ||
|
||
module.exports = getCardValue; |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are some really nice tests, good job! There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We actually don't need to put Almost always, writing There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" | ||
); |
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); | ||
}); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.