LONDON-JAN-25 | ANDREI FILIPPOV | Module-Structuring-and-Testing-Data | WEEK 3#309
LONDON-JAN-25 | ANDREI FILIPPOV | Module-Structuring-and-Testing-Data | WEEK 3#309Droid-An wants to merge 12 commits intoCodeYourFuture:mainfrom
Conversation
ChrisCookOC
left a comment
There was a problem hiding this comment.
You've made good progress here, seems you are getting the hang of unit testing and using jest which is great to see :)
|
|
||
| function isProperFraction(numerator, denominator) { | ||
| if (numerator < denominator) return true; | ||
| if (Math.abs(numerator) < Math.abs(denominator)) return true; |
There was a problem hiding this comment.
This all works, using the if / else if / else if.
However, a nice thing here is that your return values are booleans, which means you should be able to simplify this down into just two checks
Can you see a way of rewriting this in a simpler manner?
Can give a hint if needed! :)
There was a problem hiding this comment.
Oh, now I see how I can simplify that. I just removed last two conditions which are unnecessary
| // Stretch: | ||
| // What other scenarios could you test for? | ||
|
|
||
| const improperNegativeFraction = isProperFraction(-5, 2); |
| // When the function is called with such a card, | ||
| // Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5). | ||
| const fiveofHearts = getCardValue("5♥"); | ||
| const fiveOfHearts = getCardValue("2♥"); |
There was a problem hiding this comment.
I assume this comes from changing it to different values to test it is working, which is a great thing to do.
Just be careful when you are committing because the name of the variable is now misleading!
There was a problem hiding this comment.
Yes, you're right. I was just short on time to check the correct names for the card values. I've updated it now.
| if (rank === "A") return 11; | ||
| else if (rank === "J" || rank === "Q" || rank === "K") return 10; | ||
| else if ( | ||
| typeof Number(rank) == "number" && |
There was a problem hiding this comment.
This works, but are you able to find any functions you can call that check if something is a number instead? If they exist it is often best to use them and avoid having to code type checks like this!
There was a problem hiding this comment.
Yes, I found the isNaN function, which seems to do exactly that.
| // 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." | ||
| const invalid = getCardValue("five of hearts"); |
There was a problem hiding this comment.
What should happens if I give you the input of "5.123♥"? Is that reflected in the program?
There was a problem hiding this comment.
Thank you for pointing this out! I hadn’t considered that case. I resolved it by adding an isInteger condition.
| function getOrdinalNumber(num) { | ||
| return "1st"; | ||
| numToString = String(num); | ||
| if (numToString[numToString.length - 1] == 1 && num != 11) { |
There was a problem hiding this comment.
You repeat this multiple times
numToString[numToString.length - 1]
Can you see the benefits of doing this array access only once? if so, how would you do it?
There was a problem hiding this comment.
Yes, I can assign that value to the lastDigit variable and use that variable throughout the code.
ChrisCookOC
left a comment
There was a problem hiding this comment.
Thanks for taking the time to make some more changes :) Looks good.
| else if (rank === "J" || rank === "Q" || rank === "K") return 10; | ||
| else if ( | ||
| !isNaN(rank) && | ||
| Number.isInteger(+(rank)) && |
There was a problem hiding this comment.
Nice finds. Something you might be able to play with if you have time. Do you need both checks? Trial and error and find out :)
There was a problem hiding this comment.
Oh, thank you for the remark. Testing showed that !isNaN is not necessary here.
Article about isInteger on MDN says:
If the value is
NaN, return false.
So isInteger do job of both checks.
Learners, PR Template
Self checklist
Changelist
Complete Sprint 3 coursework
Questions
no questions