-
-
Notifications
You must be signed in to change notification settings - Fork 247
West Midlands | Sept-ITP-25 | Mustaf Asani | Sprint 3 | Coursework/sprint 3 practice tdd #813
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 3 commits
c518625
51060d9
1a6131d
83c98dc
752f7ae
f04aba1
d754f5a
af1e37a
fea77f0
eac5a16
aec1685
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,13 +1,7 @@ | ||
| function countChar(stringOfCharacters, findCharacter) { | ||
| let pattern = new RegExp(findCharacter, "g"); | ||
| const totalChar = stringOfCharacters.split(findCharacter); | ||
|
|
||
| let matchingChars = stringOfCharacters.match(pattern); | ||
|
|
||
| if (matchingChars === null) { | ||
| return 0; | ||
| } | ||
|
|
||
| return matchingChars.length; | ||
| return totalChar.length - 1; | ||
| } | ||
|
|
||
| module.exports = countChar; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,33 @@ | ||
| function getOrdinalNumber(num) { | ||
| if (num === 1) { | ||
| return "1st"; | ||
| } else if (num === 2) { | ||
| return "2nd"; | ||
| } else if (num === 3) { | ||
| console.log(num); | ||
| return "3rd"; | ||
| } else if (num > 3 || num < 21) { | ||
| return `${num}th`; | ||
| const lastDigit = num.toString()[num.toString().length - 1]; | ||
|
|
||
| if (lastDigit === "1") { | ||
| if (num === 11) { | ||
| return `${num}th`; | ||
| } | ||
|
|
||
| return `${num}st`; | ||
| } else if (lastDigit === "2") { | ||
| if (num.toString().length > 1) { | ||
| const last2Digits = num.toString().slice(-2); | ||
|
|
||
| if (last2Digits === "12") { | ||
| return `${num}th`; | ||
| } | ||
| } | ||
| return `${num}nd`; | ||
| } else if (lastDigit === "3") { | ||
| if (num.toString().length > 1) { | ||
| const last2Digits = num.toString().slice(-2); | ||
|
|
||
| if (last2Digits === "13") { | ||
| return `${num}th`; | ||
| } | ||
| } | ||
| return `${num}rd`; | ||
| } | ||
|
|
||
| return `${num}th`; | ||
| } | ||
|
|
||
| module.exports = getOrdinalNumber; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,18 +8,34 @@ const getOrdinalNumber = require("./get-ordinal-number"); | |
| // When the number is 1, | ||
| // Then the function should return "1st" | ||
|
|
||
| test("should return '1st' for 1", () => { | ||
| test("should append 'st' to numbers with 1 at the end except for those ending with 11", () => { | ||
| expect(getOrdinalNumber(1)).toEqual("1st"); | ||
| expect(getOrdinalNumber(21)).toEqual("21st"); | ||
| expect(getOrdinalNumber(101)).toEqual("101st"); | ||
| expect(getOrdinalNumber(151)).toEqual("151st"); | ||
| expect(getOrdinalNumber(2061)).toEqual("2061st"); | ||
| }); | ||
|
|
||
| test("should return '2nd' for 2", () => { | ||
| test("should append 'nd' to numbers with 2 at the end except for those ending with 12", () => { | ||
| expect(getOrdinalNumber(2)).toEqual("2nd"); | ||
| expect(getOrdinalNumber(22)).toEqual("22nd"); | ||
| expect(getOrdinalNumber(342)).toEqual("342nd"); | ||
| expect(getOrdinalNumber(592)).toEqual("592nd"); | ||
| expect(getOrdinalNumber(1972)).toEqual("1972nd"); | ||
| }); | ||
|
|
||
| test("should return '3rd' for 3", () => { | ||
| test("should append 'rd' to numbers with 3 at the end except for those ending with 13", () => { | ||
| expect(getOrdinalNumber(3)).toEqual("3rd"); | ||
| expect(getOrdinalNumber(33)).toEqual("33rd"); | ||
| expect(getOrdinalNumber(353)).toEqual("353rd"); | ||
| expect(getOrdinalNumber(93)).toEqual("93rd"); | ||
| expect(getOrdinalNumber(783)).toEqual("783rd"); | ||
| }); | ||
|
|
||
| test("should return any number between 4 and 20 with a suffix of 'th' at end of number", () => { | ||
| test("should append 'th' to all other numbers which do not end in 1,2,3,11,12 or 13", () => { | ||
| expect(getOrdinalNumber(10)).toEqual("10th"); | ||
| expect(getOrdinalNumber(11)).toEqual("11th"); | ||
| expect(getOrdinalNumber(212)).toEqual("212th"); | ||
| expect(getOrdinalNumber(113)).toEqual("113th"); | ||
| expect(getOrdinalNumber(17)).toEqual("17th"); | ||
| }); | ||
|
Comment on lines
11
to
41
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These tests are quite comprehensive. If you use this test script to test your implementation, you can discover some bug in your code.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added code to catch negative and non-integer values |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,15 +4,19 @@ function repeat(str, count) { | |
| if (count === 0) { | ||
| return newStr; | ||
| } else if (count === 1) { | ||
| return (newStr += str); | ||
| return str; | ||
| } else if (count > 1) { | ||
| for (let i = 0; i < count; i++) { | ||
| newStr += str; | ||
| } | ||
| return newStr; | ||
| } else { | ||
| return "Error invalid count used, please use integers from 0 upwards."; | ||
| throw new Error( | ||
| "Error invalid count used, please use integers from 0 upwards." | ||
| ); | ||
| //return "Error invalid count used, please use integers from 0 upwards."; | ||
| } | ||
| return true; | ||
|
||
| } | ||
|
|
||
| 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.
Could consider using the more efficient approach involving the
%operator to extract the last digit and the last two digits from a number directly.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.
used the % to have less code and less function calls