-
-
Notifications
You must be signed in to change notification settings - Fork 195
Glasgow | ITP May -25 | Mirabelle Morah | Module-Structuring-and-Testing-Data | Coursework/sprint-3 #628
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?
Glasgow | ITP May -25 | Mirabelle Morah | Module-Structuring-and-Testing-Data | Coursework/sprint-3 #628
Conversation
|
||
return ( | ||
num + | ||
(suffixes[(remainder - 20) % 10] || suffixes[remainder] || suffixes[0]) |
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.
Can you explain how this expression work?
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.
Lines 1-3 checks the kind of input passed in the parameter. If input type is not a number or integer (eg if it's a string) then it gives the error message that the input must be an integer.
Line 6: 'Const suffixes' is an array or ordinal suffixes
Line 7: and 'const remainder' performs a modulus operation (as we learned in previous tests) on whatever input is passed in the 'num' parameter. Then it gets the digit that remains after that division. Eg 123 % 100 = 1.23 -- therefore const remainder = 23
Lines 9-11: performs multiple things:
suffixes[(remainder - 20) % 10]
Subtracts 20 from remainder, then gets last digit and uses that last digit to assign the ordinal form
Eg:
Remainder = 23: (23-20) % 10 = [0.3] 3, so const suffix [3] = rd. There it will assign 23 as '23rd'
Then for '|| suffixes[remainder]' if an answer is undefined it simple uses the remainder with the direct array number: like
if remainder = 1: suffixes[1] = "st"
if remainder = 2: suffixes[2] = "nd"
etc.
The final fallback if both previous parts are undefined it goes back to || suffixes[0] which adds 'th' to numbers like 11th etc.
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.
Would the code still work if we changed
(suffixes[(remainder - 20) % 10] || suffixes[remainder] || suffixes[0])
to
(suffixes[remainder] || suffixes[(remainder - 20) % 10] || suffixes[0])
(Change the order of the first two conditions)
If not, can you find a number in the range [0, 100) which the latter would fail?
> Eg 123 % 100 = 1.23 -- therefore const remainder = 23
^^^^^^^^^^^^^^^^^^^
This unusual notation makes it looks like you are treating %
as a division operator.
You could just write 123 % 100 = 23 (all programmers should recognise what %
operator does)
Sprint-3/3-mandatory-practice/implement/get-ordinal-number.test.js
Outdated
Show resolved
Hide resolved
Changes look good. Well done! |
Learners, PR Template
Self checklist
Changelist
Provided answers to sprint 3
Questions
Nil