-
-
Notifications
You must be signed in to change notification settings - Fork 273
Sheffield | 25-ITP-SEP | Declan Williams | Sprint 3 | Coursework #878
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 19 commits
32eef26
0709cbe
def5669
487694e
5fb6f95
876b8a1
801d5d8
7e6166b
dbf085f
a09a992
5f2aa2c
22f8e4a
f3f8b47
f08ec15
234e8b6
6a021e3
7455253
5e1196f
cc80d7e
ca08ca9
7dc44ec
5e720c8
252bfb9
3b7e322
809e11b
0b2a485
377b7db
096d45c
1200ba7
014b90e
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 |
|---|---|---|
|
|
@@ -8,15 +8,25 @@ | |
| // write one test at a time, and make it pass, build your solution up methodically | ||
| // just make one change at a time -- don't rush -- programmers are deep and careful thinkers | ||
| function getCardValue(card) { | ||
| if (rank === "A") { | ||
| return 11; | ||
| } | ||
| var rank = card.slice(0, -1); // get the rank of the card by removing the last character. (the suit is the last character) | ||
|
||
| if (rank === "A") return 11; // this checks for Aces | ||
| // Handle Number Cards (2-9) | ||
| if (rank === "2") return 2; // this checks for the twos | ||
| if (rank === "3") return 3; // this checks for the threes | ||
| if (rank === "4") return 4; // this checks for the fours | ||
| if (rank === "5") return 5; // this should check for fives | ||
| if (rank === "6") return 6; // this checks for the sixes | ||
| if (rank === "7") return 7; // this checks for the sevens | ||
| if (rank === "8") return 8; // this checks for the eights | ||
| if (rank === "9") return 9; // this checks for the nines | ||
| // Handle Face Cards (J, Q, K) And 10's | ||
| if (rank === "J") return 10; // this checks for Jacks | ||
| if (rank === "Q") return 10; // this checks for Queens | ||
| if (rank === "K") return 10; // this checks for Kings | ||
| if (rank === "10") return 10; // this checks for Tens | ||
| // if none of the above its an invalid card and throw an error | ||
|
||
| throw new Error("Invalid card rank."); // this will throw an error if the card is not a valid rank | ||
| } | ||
|
|
||
| // The line below allows us to load the getCardValue function into tests in other files. | ||
| // This will be useful in the "rewrite tests with jest" step. | ||
| module.exports = getCardValue; | ||
|
|
||
| // You need to write assertions for your function to check it works in different cases | ||
| // we're going to use this helper function to make our assertions easier to read | ||
| // if the actual output matches the target output, the test will pass | ||
|
|
@@ -38,20 +48,37 @@ assertEquals(aceofSpades, 11); | |
| // Given a card with a rank between "2" and "9", | ||
| // When the function is called with such a card, | ||
| // Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5). | ||
| const fiveofHearts = getCardValue("5♥"); | ||
| // ====> write your test here, and then add a line to pass the test in the function above | ||
| const fiveofHearts = getCardValue("5♥"); | ||
| const sixofDiamonds = getCardValue("6♦"); | ||
| const sevenofClubs = getCardValue("7♣"); | ||
| const eightofSpades = getCardValue("8♠"); | ||
| assertEquals(fiveofHearts, 5); | ||
| assertEquals(sixofDiamonds, 6); | ||
| assertEquals(sevenofClubs, 7); | ||
| assertEquals(eightofSpades, 8); | ||
|
|
||
| // Handle Face Cards (J, Q, K): | ||
| // Given a card with a rank of "10," "J," "Q," or "K", | ||
| // When the function is called with such a card, | ||
| // Then it should return the value 10, as these cards are worth 10 points each in blackjack. | ||
| const jackOfDiamonds = getCardValue("J♦"); | ||
| const queenOfClubs = getCardValue("Q♣"); | ||
| const kingOfSpades = getCardValue("K♠"); | ||
| assertEquals(jackOfDiamonds, 10); | ||
| assertEquals(queenOfClubs, 10); | ||
| assertEquals(kingOfSpades, 10); | ||
|
|
||
| // Handle Ace (A): | ||
| // Given a card with a rank of "A", | ||
| // When the function is called with an Ace, | ||
| // Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack. | ||
|
|
||
| // Handle Invalid Cards: | ||
| // Given a card with an invalid rank (neither a number nor a recognized face card), | ||
| // When the function is called with such a card, | ||
| // Then it should throw an error indicating "Invalid card rank." | ||
| try { | ||
| getCardValue("z♠"); // this should throw an error of "Invalid card rank." | ||
| console.log("Test failed: Expected an error for invalid card rank."); | ||
| } catch(error){ | ||
| assertEquals(error.message, "Invalid card rank."); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,18 @@ | ||
| function countChar(stringOfCharacters, findCharacter) { | ||
| return 5 | ||
| // start a count of 0 | ||
| let count = 0; | ||
|
|
||
| // check each of the characters in the string one by one. | ||
| for (let i = 0; i < stringOfCharacters.length; i++) { | ||
| // checks if the current characters matches the one were looking for in the string. | ||
| if (stringOfCharacters[i] === findCharacter) | ||
| // if it does, we increment the count by 1. | ||
| count = count + 1; | ||
| } | ||
|
|
||
| return count; | ||
| } | ||
| console.log(countChar("aaaaa", "a")); // 5 | ||
| console.log(countChar("hello", "l")); // 2 | ||
|
|
||
| module.exports = countChar; | ||
| module.exports = countChar; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,26 @@ | ||
| function getOrdinalNumber(num) { | ||
| return "1st"; | ||
| const lastTwoDigits = num % 100; // gets the last two digits of the number because some like 11, 12, 13 are special cases. | ||
| const lastDigit= num % 10; // gets the last digit to decide if its going to be "St, Nd, Rd" | ||
|
|
||
| // handles special cases like "11,12,13" to always end in the "Th" | ||
| if (lastTwoDigits === 11 || lastTwoDigits === 12 || lastTwoDigits === 13){ | ||
| return num + "St"; | ||
| } | ||
| // will return "St" if the number ends in 1. | ||
| if (lastDigit === 1){ | ||
| return num + "st"; | ||
| } | ||
| // will return "Nd" if the number ends in 2. | ||
| if (lastDigit === 2){ | ||
| return num + "nd"; | ||
| } | ||
| // will return "Rd" if the number ends in 3. | ||
| if (lastDigit === 3){ | ||
| return num + "rd"; | ||
| } | ||
|
|
||
| // will return all numbers that end in 4, 5, 6, 7, 8, 9 with "Th". | ||
| return num + "th"; | ||
| } | ||
|
|
||
| module.exports = getOrdinalNumber; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,3 +11,30 @@ const getOrdinalNumber = require("./get-ordinal-number"); | |
| test("should return '1st' for 1", () => { | ||
| expect(getOrdinalNumber(1)).toEqual("1st"); | ||
| }); | ||
| // Case 2: Identify the ordinal number for 2 | ||
| // When the number is 2, | ||
| // The function should then return "2nd". | ||
|
|
||
| test("Should return `2nd` for 2", () => { | ||
| expect(getOrdinalNumber(2)).toEqual("2nd"); | ||
| }); | ||
|
|
||
| // Case 3: Identify the ordinal number for 3 | ||
| // When the number is 3, | ||
| // The Function should the return "3rd" | ||
|
|
||
| test("Should return `3rd` for 3", () => { | ||
| expect(getOrdinalNumber(3)).toEqual("3rd"); | ||
| }); | ||
|
Comment on lines
16
to
34
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. To ensure thorough testing, we need broad scenarios that cover all possible cases. For example, we can prepare a test for numbers 2, 22, 132, etc. as Can you update the tests in this script to make them more comprehensive? That is, create few test categories that can cover all possible cases?
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. have updated them to have more tests to cover more possible cases |
||
| // Case 4: identify the special ordinal numbers for 11, 12, 13 | ||
| // When the number is 11, 12, 13, | ||
| // The function should return "11th, 12th, 13th" | ||
|
|
||
| test ("should return `11th, 12th, 13th` for special ordinal numbers ending on these", () => { | ||
| expect(getOrdinalNumber(11)).toEqual("11th"); | ||
| expect(getOrdinalNumber(12)).toEqual("12th"); | ||
| expect(getOrdinalNumber(13)).toEqual("13th"); | ||
| expect(getOrdinalNumber(111)).toEqual("111th"); | ||
| expect(getOrdinalNumber(112)).toEqual("112th"); | ||
| expect(getOrdinalNumber(113)).toEqual("113th"); | ||
| }); | ||
cjyuan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,19 @@ | ||
| function repeatStr() { | ||
| return "hellohellohello"; | ||
| function repeatStr(str, count) { | ||
| // check if "count" is a negative number if so will throw new error | ||
| if (count < 0) { | ||
| throw new error("Count must be a positive number"); | ||
|
|
||
| //check if the is equal to zero | ||
| if (count === 0){ | ||
| return ""; // returns empty string if count is equal to zero | ||
| } | ||
| //check if the count is equals 1. | ||
| if (count === 1) { | ||
| //returns just the string as its not needed to be repeated | ||
| return str; | ||
| } | ||
| return str.repeat(count); // if the count is above two it repeat "count" number of times. | ||
| } | ||
| } | ||
|
|
||
| module.exports = repeatStr; |
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.
According to the definition of proper fraction in mathematics:
isProperFraction(-4, 3)should returnfalseisProperFraction(-2, 5)should returntrueisProperFraction(-2, 0)should returnfalseisProperFraction(-1, 1)should returnfalseisProperFraction(-2, -3)should returntrueCan you look up the definition of proper fraction and update your function accordingly?