-
-
Notifications
You must be signed in to change notification settings - Fork 269
London | 25-ITP-Sept | Samuel Tarawally | Sprint 3 | Implement and rewrite tests #864
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
base: main
Are you sure you want to change the base?
London | 25-ITP-Sept | Samuel Tarawally | Sprint 3 | Implement and rewrite tests #864
Conversation
| const numValue = parseInt(rank); | ||
| if (numValue >= 2 && numValue <= 9) return numValue; |
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.
In JavaScript, strings that represent valid numeric literals in the language can be safely
converted to equivalent numbers or parsed into a valid integers.
Do you want to recognize these string values as valid ranks?
To find out what these strings are, you can ask AI
What kinds of string values would make
parseInt(rank)evaluate to2in JS?
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.
My understanding is that the parseInt function converts a string to an integer by parsing its leading numerical characters:
parseInt(" 7abc") // returns 7 Given this, my current code would accept "03", for example, and this clearly isn't a valid rank. A better approach I think is to use a conditional statement which explicitly checks the rank against a limited set of numeric literals.
Is my understanding correct here, @cjyuan?
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.
If we were to recognise only the 13 possible ranks, then a stricter check would be needed.
Can you figure out exactly how to implement them (as an exercise)?
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.
| const numValue = parseInt(rank); | |
| if (numValue >= 2 && numValue <= 9) return numValue; | |
| function getCardValue(card) { | |
| const rank = card.slice(0, -1); | |
| if (rank === "A") return 11; | |
| if (rank === "J" || rank === "Q" || rank === "K") return 10; | |
| const numRanks = ["2", "3", "4", "5", "6", "7", "8", "9", "10"]; | |
| if (numRanks.includes(rank)) { | |
| return Number(rank); | |
| } | |
| throw new Error("Invalid card rank"); | |
| } |
| }); | ||
|
|
||
| test("should return 10 for face cards (J, Q, K, 10)", () => { | ||
| expect(getCardValue("10♣")).toEqual(10); |
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 think 10 is considered a number card.
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.
Yes, it is. I will update the test to count 10 as a number card and change the test descriptions to reflect this.
|
Changes look good. Well done! |
Learners, PR Template
Self checklist
Changelist
implementfolder.rewrite-tests-with-jest folder.