-
-
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
West Midlands | Alireza Seifi Shalamzari | Module-Structuring-and-Testing-Data | Week 3 #231
base: main
Are you sure you want to change the base?
Changes from 17 commits
b089720
89e68e6
70e370f
89e8ce2
1a80784
10fe203
986f549
c15e74f
274d23d
3f06364
874674b
46d9d7f
5f08647
b918d45
5d65a76
c3e8cb9
8bb658a
c986982
031f86f
543e9be
315c043
c37f61c
e7ec383
baedcfc
15ebf6e
a6d5d9e
f1b8d0d
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 |
---|---|---|
|
@@ -29,3 +29,49 @@ | |
// 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) { | ||
// Remove the suit from the card string | ||
const rank = card.slice(0, -1); | ||
|
||
// Handle Number Cards (2-9) | ||
if (rank >= "2" && rank <= "9") { | ||
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. TodoThis check is not solid enough. 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 did work on this and I found a solution! |
||
return parseInt(rank); | ||
} | ||
|
||
// Handle Face Cards (10, J, Q, K) | ||
if (rank === "10" || rank === "J" || rank === "Q" || rank === "K") { | ||
return 10; | ||
} | ||
|
||
// Handle Ace (A) | ||
if (rank === "A") { | ||
return 11; | ||
} | ||
|
||
// Handle Invalid Cards | ||
throw new Error("Invalid card rank."); | ||
} | ||
|
||
|
||
// Assertions to test the function | ||
try { | ||
console.assert(getCardValue("2♠") === 2, "Test Case 1 Failed"); | ||
console.assert(getCardValue("5♦") === 5, "Test Case 2 Failed"); | ||
console.assert(getCardValue("10♥") === 10, "Test Case 3 Failed"); | ||
console.assert(getCardValue("J♣") === 10, "Test Case 4 Failed"); | ||
console.assert(getCardValue("Q♠") === 10, "Test Case 5 Failed"); | ||
console.assert(getCardValue("K♦") === 10, "Test Case 6 Failed"); //using 10 it should pass and 20 should fail | ||
console.assert(getCardValue("A♣") === 11, "Test Case 7 Failed"); | ||
|
||
// Testing invalid card | ||
try { | ||
getCardValue("Z♠"); | ||
} catch (e) { | ||
console.assert(e.message === "Invalid card rank.", "Test Case 8 Failed"); | ||
} | ||
|
||
console.log("All test cases passed!"); | ||
} catch (error) { | ||
console.error(error.message); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,3 +32,21 @@ | |
// 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) { | ||
// Check if the denominator is zero and throw an error if it is | ||
if (denominator === 0) { | ||
throw new Error("Denominator cannot be zero"); | ||
} | ||
// Check if the numerator is less than the denominator and return true if it is | ||
// Otherwise, return false | ||
return numerator < denominator; | ||
} | ||
|
||
//Testing the function | ||
console.log(isProperFraction(2, 3)); //should return True | ||
console.log(isProperFraction(5, 2)); //should return False | ||
console.log(isProperFraction(-4, 7)); //should return 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. In mathematics, -4/7 == 4/-7, and -4/-7 == 4/7. Hint: If you compute the absolute value of both parameters inside the function first, the code can become much simpler. 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 added a part which first make absolute numbers then check them |
||
console.log(isProperFraction(3, 3)); //should return False | ||
console.log(isProperFraction(3, 0)); //should throw an error | ||
|
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.
Does these two show "Invalid angle"?
It is not easy to spot the error from just
console.log()
output. That's one reason why a test framework like Jest can help us identify cases when a return value does not match an expected value.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.
I changed the testing method to Jest