London | 26-ITP-Jan | Abdul Moiz | Sprint 3 | implement-and-rewrite#915
London | 26-ITP-Jan | Abdul Moiz | Sprint 3 | implement-and-rewrite#915A-Moiz wants to merge 11 commits intoCodeYourFuture:mainfrom
Conversation
…proper-fraction.js
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
| if (denominator === 0) { | ||
| return false; | ||
| } else if (Math.abs(numerator) < Math.abs(denominator)) { | ||
| return true; | ||
| } else { | ||
| return false; | ||
| } |
There was a problem hiding this comment.
This works.
Optional challenge: Simplify the code.
| function getCardValue(card) { | ||
| // TODO: Implement this function | ||
| const rank = card.slice(0, card.length - 1); |
There was a problem hiding this comment.
The requirements has changed a bit.
To ensure card is valid, you should also check if the last character is a valid suit character.
| } else if (Number(rank) >= 2 && Number(rank) < 10) { | ||
| return Number(rank); |
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? If not, you will need to enforce stricter check.
To find out what these strings are, you can ask AI
What kinds of string values would make
Number(rank)evaluate to2in JS?
There was a problem hiding this comment.
I added an if statement beforehand to check typeof for the variable. If it fails it will throw an error.
| test(`should return true for proper fraction with positive numerator and denominator`, () => { | ||
| expect(isProperFraction(1, 2)).toEqual(true); | ||
| }); | ||
|
|
||
| test(`should return false for improper fraction with positive numerator and denominator`, () => { | ||
| expect(isProperFraction(3, 2)).toEqual(false); | ||
| }); | ||
|
|
There was a problem hiding this comment.
What about negative numbers and different combinations of positive/negative/zero?
Think also what edge case to check?
There was a problem hiding this comment.
Added extra tests for negative numerators/denominators and all are passing.
| } else { | ||
| return "Invalid card rank"; | ||
| } |
There was a problem hiding this comment.
The new requirement is:
// When the card string is invalid (not following the above format), the function should
// throw an error.
Throwing an error is not the same as returning an error message.
Could you look up "How to throw an error in JS" and update your code accordingly?
There was a problem hiding this comment.
Modified function and test case to throw error.
| expect(getCardValue("11♥")).toEqual("Invalid card rank"); | ||
| expect(getCardValue("Z♦")).toEqual("Invalid card rank"); | ||
| }); | ||
|
|
There was a problem hiding this comment.
-
Could also include invalid cases that involves invalid suit character.
-
To test if a function can throw an error as expected, we could use
.toThrow(). You can find out more about how to use.toThrow()here: https://jestjs.io/docs/expect#tothrowerror (Note: Pay close attention to the syntax of the example)
|
|
||
| function isProperFraction(numerator, denominator) { | ||
| // TODO: Implement this function | ||
| return Math.abs(numerator) < Math.abs(denominator) ? true : false; |
There was a problem hiding this comment.
When an expression evaluates to a Boolean value, we can also just return the value of the expression:
return Math.abs(numerator) < Math.abs(denominator);
| const rankNum = Number(rank); | ||
| if (!Number.isNaN(rankNum) && rankNum >= 2 && rankNum < 10) { | ||
| return rankNum; | ||
| } |
There was a problem hiding this comment.
I added an if statement beforehand to check typeof for the variable. If it fails it will throw an error.
I don't see any statement involving typeof.
Currently, a card value like ""0x02♠" or "3.1416♠" can still pass the check on line 37.
There was a problem hiding this comment.
Apologies, I got it mixed up with a different task. It is resolved now.
| test(`should return false for negative numerator`, () => { | ||
| expect(isProperFraction(-5, 2)).toEqual(false); | ||
| }); | ||
|
|
||
| test(`should return false for negative numerator`, () => { | ||
| expect(isProperFraction(-5, 2)).toEqual(false); | ||
| }); |
cjyuan
left a comment
There was a problem hiding this comment.
Changes look good. Well done.
| !Number.isNaN(rankNum) && | ||
| Number.isInteger(rankNum) && | ||
| rankNum >= 2 && | ||
| rankNum < 10 && | ||
| rank === rankNum.toString() |
There was a problem hiding this comment.
This works, but an approach similar to that at line 29 is probably cleaner.
Learners, PR Template
Self checklist
Changelist
Completed all the exercises in implement-and-rewrite directory
Questions
N/A