Skip to content

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

mirabellemorah
Copy link

Learners, PR Template

Self checklist

  • I have committed my files one by one, on purpose, and for a reason
  • I have titled my PR with REGION | COHORT_NAME | FIRST_NAME LAST_NAME | PROJ_NAME
  • I have tested my changes
  • My changes follow the style guide
  • My changes meet the requirements of this task

Changelist

Provided answers to sprint 3

Questions

Nil

@mirabellemorah mirabellemorah added 📅 Sprint 3 Assigned during Sprint 3 of this module Needs Review Participant to add when requesting review labels Jun 30, 2025

return (
num +
(suffixes[(remainder - 20) % 10] || suffixes[remainder] || suffixes[0])
Copy link
Contributor

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?

Copy link
Author

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.

Copy link
Contributor

@cjyuan cjyuan Jul 8, 2025

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)

@cjyuan cjyuan added Reviewed Volunteer to add when completing a review and removed Needs Review Participant to add when requesting review labels Jul 8, 2025
@mirabellemorah mirabellemorah added the Needs Review Participant to add when requesting review label Jul 8, 2025
@cjyuan
Copy link
Contributor

cjyuan commented Jul 8, 2025

Changes look good. Well done!

@cjyuan cjyuan added Complete Volunteer to add when work is complete and review comments have been addressed and removed Needs Review Participant to add when requesting review Reviewed Volunteer to add when completing a review labels Jul 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Complete Volunteer to add when work is complete and review comments have been addressed 📅 Sprint 3 Assigned during Sprint 3 of this module
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants