-
-
Notifications
You must be signed in to change notification settings - Fork 261
Birmingham | 25-ITP-Sept | Khor Biel | Sprint 3 | coursework/sprint-3-practice-tdd #801
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
Changes from 1 commit
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,21 +1,11 @@ | ||
| function countChar(stringOfCharacters, findCharacter) { | ||
| // Ensure valid inputs | ||
| if ( | ||
| typeof stringOfCharacters !== "string" || | ||
| typeof findCharacter !== "string" | ||
| ) { | ||
| return 0; | ||
| } | ||
|
|
||
| // Count occurrences | ||
| let count = 0; | ||
| for (let char of stringOfCharacters) { | ||
| if (char === findCharacter) { | ||
| count++; | ||
| let total = 0; | ||
| for (let i=0; i < stringOfCharacters.length; i++){ | ||
| if (findCharacter == stringOfCharacters[i]){ | ||
| total++; | ||
| } | ||
| } | ||
|
|
||
|
||
| return count; | ||
| } | ||
| return total; | ||
| } | ||
|
|
||
| module.exports = countChar; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,28 +1,24 @@ | ||
| function getOrdinalNumber(num) { | ||
| // Ensure the input is a valid number | ||
| if (typeof num !== "number" || isNaN(num)) { | ||
| return ""; | ||
| } | ||
|
|
||
|
||
| const lastTwoDigits = num % 100; | ||
| const lastDigit = num % 10; | ||
|
|
||
| let result; | ||
|
|
||
| // Handle special cases: 11th, 12th, 13th | ||
| if (lastTwoDigits >= 11 && lastTwoDigits <= 13) { | ||
| return `${num}th`; | ||
| if (num % 100 == 11 || num % 100 == 12 || num % 100 == 13){ | ||
| result = num.toString() + "th"; | ||
cjyuan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // Handle normal ordinal endings | ||
| switch (lastDigit) { | ||
| case 1: | ||
| return `${num}st`; | ||
| case 2: | ||
| return `${num}nd`; | ||
| case 3: | ||
| return `${num}rd`; | ||
| default: | ||
| return `${num}th`; | ||
| else if (num % 10 == 1){ | ||
| result = num.toString() +"st"; | ||
| } | ||
| else if (num % 10 == 2){ | ||
| result = num.toString() + "nd"; | ||
| } | ||
| } | ||
| else if (num % 10 == 3){ | ||
| result = num.toString() + "rd"; | ||
| } | ||
| else { | ||
| result = num.toString() + "th"; | ||
| } | ||
|
|
||
| return result | ||
| } | ||
|
|
||
| module.exports = getOrdinalNumber; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,16 @@ | ||
| function repeat(str, count) { | ||
| // Validate inputs | ||
| if (typeof str !== "string") { | ||
| throw new Error("First argument must be a string"); | ||
| } | ||
|
|
||
| if (typeof count !== "number" || count < 0) { | ||
| throw new Error("Count must be a non-negative number"); | ||
| } | ||
| if (count < 0) { | ||
| return null; | ||
| } else if (count == 0) { | ||
| return ""; | ||
| } | ||
| let result = ""; | ||
| for (let i = 0; i < count; i++) { | ||
| result += str; | ||
| } | ||
| return result; | ||
|
|
||
| // Repeat string count times | ||
| 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.
Why remove the check instead of strengthening it?