-
-
Notifications
You must be signed in to change notification settings - Fork 240
Glasgow | 25-ITP-SEP | Mohammed Abdoon | Sprint 3 | Coursework/2-practice-tdd #746
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?
Changes from 6 commits
1e5cad7
a42937d
9600443
c0d9240
f6314c3
ad2180d
94348cb
b46e59d
f55b3a2
f06c4f9
25b1868
06a987e
4b42131
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,10 @@ | ||
| function countChar(stringOfCharacters, findCharacter) { | ||
| return 5 | ||
| let output = 0; | ||
| for( let i=0; i <= stringOfCharacters.length; i++ ) { | ||
| if(stringOfCharacters[i] === findCharacter) | ||
| output++; | ||
| } | ||
| return output; | ||
| } | ||
|
|
||
| module.exports = countChar; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,28 @@ | ||
| function getOrdinalNumber(num) { | ||
| return "1st"; | ||
| // | ||
| if (typeof num !== "number" || !Number.isInteger(num) || num <= 0) { | ||
|
||
| return "Input must be a positive integer."; | ||
| } | ||
|
|
||
| const lastTwoDigits = num % 100; | ||
| const lastDigit = num % 10; | ||
|
|
||
| if (lastTwoDigits >= 11 && lastTwoDigits <= 13) { | ||
| return `${num}th`; | ||
| } | ||
|
|
||
| switch (lastDigit) { | ||
| case 1: | ||
| return `${num}st`; | ||
| case 2: | ||
| return `${num}nd`; | ||
| case 3: | ||
| return `${num}rd`; | ||
| default: | ||
| return `${num}th`; | ||
| } | ||
| return num; | ||
| } | ||
|
|
||
| console.log(getOrdinalNumber(21)); // "1st" | ||
| module.exports = getOrdinalNumber; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,4 +10,54 @@ const getOrdinalNumber = require("./get-ordinal-number"); | |
|
|
||
| test("should return '1st' for 1", () => { | ||
| expect(getOrdinalNumber(1)).toEqual("1st"); | ||
| expect(getOrdinalNumber(21)).toEqual("21st"); | ||
| expect(getOrdinalNumber(101)).toEqual("101st"); | ||
| }); | ||
|
|
||
| // Case 2: Identify the ordinal number for 1 | ||
| // except those ending in 12 | ||
| test("append 'nd' to numbers ending in 2, except those ending in 12", () => { | ||
| expect( getOrdinalNumber(2) ).toEqual("2nd"); | ||
| expect( getOrdinalNumber(22) ).toEqual("22nd"); | ||
| expect( getOrdinalNumber(132) ).toEqual("132nd"); | ||
| }); | ||
|
|
||
| // Case 3: Identify the ordinal numbers for 3 to 12 | ||
| test("should return correct ordinal suffixes for numbers 3 to 12", () => { | ||
| expect( getOrdinalNumber(3) ).toEqual("3rd"); | ||
| expect( getOrdinalNumber(4) ).toEqual("4th"); | ||
| expect( getOrdinalNumber(5) ).toEqual("5th"); | ||
| expect( getOrdinalNumber(6) ).toEqual("6th"); | ||
| expect( getOrdinalNumber(7) ).toEqual("7th"); | ||
| expect( getOrdinalNumber(8) ).toEqual("8th"); | ||
| expect( getOrdinalNumber(9) ).toEqual("9th"); | ||
| expect( getOrdinalNumber(10) ).toEqual("10th"); | ||
| expect( getOrdinalNumber(11) ).toEqual("11th"); | ||
| expect( getOrdinalNumber(12) ).toEqual("12th"); | ||
| }); | ||
| // Case 4: Identify the ordinal numbers for numbers above 12 | ||
| test("should return correct ordinal suffixes for numbers above 12", () => { | ||
| expect( getOrdinalNumber(13) ).toEqual("13th"); | ||
| expect( getOrdinalNumber(19) ).toEqual("19th"); | ||
| expect( getOrdinalNumber(23) ).toEqual("23rd"); | ||
| expect( getOrdinalNumber(34) ).toEqual("34th"); | ||
| expect( getOrdinalNumber(45) ).toEqual("45th"); | ||
| expect( getOrdinalNumber(56) ).toEqual("56th"); | ||
| expect( getOrdinalNumber(67) ).toEqual("67th"); | ||
| }); | ||
|
|
||
| // Case 5: Identify the ordinal numbers for numbers like 20 30 40 etc | ||
| test("should return correct ordinal suffixes for multiples of ten", () => { | ||
| expect( getOrdinalNumber(20) ).toEqual("20th"); | ||
| expect( getOrdinalNumber(30) ).toEqual("30th"); | ||
| expect( getOrdinalNumber(70) ).toEqual("70th"); | ||
| expect( getOrdinalNumber(50) ).toEqual("50th"); | ||
| }); | ||
|
|
||
| // Case 6: dealing with big numbers | ||
| test("should return correct ordinal suffixes for big numbers", () => { | ||
| expect( getOrdinalNumber(111) ).toEqual("111th"); | ||
| expect( getOrdinalNumber(4712) ).toEqual("4712th"); | ||
| expect( getOrdinalNumber(10003) ).toEqual("10003rd"); | ||
| expect( getOrdinalNumber(10012) ).toEqual("10012th"); | ||
| }); | ||
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,8 @@ | ||
| function repeat() { | ||
| return "hellohellohello"; | ||
| function repeat(str, count) { | ||
| if (typeof count !== "number" || isNaN(count) || count < 0) { | ||
|
||
| throw new Error("Count must be a valid number"); | ||
| } | ||
| return str.repeat(count); | ||
| } | ||
|
|
||
| module.exports = repeat; | ||
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.
The function should work for any positive integers.
Can you lookup the rules for representing ordinal numbers?
You should prepare tests in
get-ordinal-number.test.jsfile first before implementinggetOrdinalNumber().For example, we can prepare a test for numbers 2, 22, 132, etc. as
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.
Thank you for the feedback. I set the tests for the function and modified the function getOrdinalNumber() to meet the requirements and to handle all possible cases.