London | 25-ITP-Sept | Samuel Tarawally | Sprint 3 | Implement and rewrite tests#864
London | 25-ITP-Sept | Samuel Tarawally | Sprint 3 | Implement and rewrite tests#864Tarawally wants to merge 9 commits intoCodeYourFuture:mainfrom
Conversation
| const numValue = parseInt(rank); | ||
| if (numValue >= 2 && numValue <= 9) return numValue; |
There was a problem hiding this comment.
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.
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.
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.
| 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.
I think 10 is considered a number card.
There was a problem hiding this comment.
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.