generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 195
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
mirabellemorah
wants to merge
4
commits into
CodeYourFuture:main
Choose a base branch
from
mirabellemorah:coursework/sprint-3
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Glasgow | ITP May -25 | Mirabelle Morah | Module-Structuring-and-Testing-Data | Coursework/sprint-3 #628
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,15 @@ | ||
function getAngleType(angle) { | ||
if (angle === 90) return "Right angle"; | ||
// replace with your completed function from key-implement | ||
|
||
// read to the end, complete line 36, then pass your test here | ||
else if (angle < 90) return "Acute angle"; | ||
else if (angle > 90 && angle < 180) return "Obtuse angle"; | ||
else if (angle === 180) return "Straight angle"; | ||
else if (angle > 180 && angle < 360) return "Reflex angle"; | ||
else return "Invalid angle"; | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
// Don't get bogged down in this detail | ||
// Jest uses CommonJS module syntax by default as it's quite old | ||
// We will upgrade our approach to ES6 modules in the next course module, so for now | ||
// We will upgrade our approach to ES6 modules in the next course module, so for now | ||
// we have just written the CommonJS module.exports syntax for you | ||
module.exports = getAngleType; | ||
module.exports = getAngleType; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
function isProperFraction(numerator, denominator) { | ||
if (numerator < denominator) return true; | ||
// add your completed function from key-implement here | ||
if (denominator === 0) return false; | ||
return Math.abs(numerator) < Math.abs(denominator); | ||
} | ||
|
||
module.exports = isProperFraction; | ||
module.exports = isProperFraction; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,14 @@ | ||
function getCardValue(card) { | ||
// replace with your code from key-implement | ||
return 11; | ||
const rank = card.slice(0, -1); | ||
if (rank === "A") return 11; | ||
|
||
if (["K", "Q", "J", "10"].includes(rank)) return 10; | ||
|
||
if (["2", "3", "4", "5", "6", "7", "8", "9"].includes(rank)) { | ||
return Number(rank); | ||
} | ||
|
||
throw new Error("Invalid card rank"); | ||
} | ||
module.exports = getCardValue; | ||
|
||
module.exports = getCardValue; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,46 @@ | ||
const getCardValue = require("./3-get-card-value"); | ||
|
||
test("should return 11 for Ace of Spades", () => { | ||
const aceofSpades = getCardValue("A♠"); | ||
expect(aceofSpades).toEqual(11); | ||
}); | ||
expect(getCardValue("A♠")).toEqual(11); | ||
expect(getCardValue("A♥")).toEqual(11); | ||
}); | ||
|
||
// Case 2: Handle Number Cards (2-10): | ||
|
||
test("should return correct value for number cards (2-10)", () => { | ||
for (let i = 2; i <= 10; i++) { | ||
expect(getCardValue(`${i}♠`)).toEqual(i); | ||
expect(getCardValue(`${i}♥`)).toEqual(i); | ||
} | ||
}); | ||
|
||
// Alternate long method | ||
|
||
// test("should return the value of number cards (2-10)", () => { | ||
// expect(getCardValue("2♠")).toEqual(2); | ||
// expect(getCardValue("3♠")).toEqual(3); | ||
// expect(getCardValue("4♠")).toEqual(4); | ||
// expect(getCardValue("5♠")).toEqual(5); | ||
// expect(getCardValue("6♠")).toEqual(6); | ||
// expect(getCardValue("7♠")).toEqual(7); | ||
// expect(getCardValue("8♠")).toEqual(8); | ||
// expect(getCardValue("9♠")).toEqual(9); | ||
// expect(getCardValue("10♠")).toEqual(10); | ||
// }); | ||
|
||
// Case 3: Handle Face Cards (J, Q, K): | ||
// Case 4: Handle Ace (A): | ||
// Case 5: Handle Invalid Cards: | ||
test("should return 10 or face cards (J, Q, K)", () => { | ||
["J", "Q", "K"].forEach((rank) => { | ||
expect(getCardValue(`${rank}♣`)).toEqual(10); | ||
expect(getCardValue(`${rank}♦`)).toEqual(10); | ||
}); | ||
}); | ||
|
||
|
||
// Case 4: Handle Invalid Cards: | ||
test("should throw an error for invalid card", () => { | ||
expect(() => getCardValue("Invalid")).toThrow("Invalid card rank"); | ||
expect(() => getCardValue("0x02♠")).toThrow("Invalid card rank"); | ||
expect(() => getCardValue("2.1♠")).toThrow("Invalid card rank"); | ||
expect(() => getCardValue("")).toThrow("Invalid card rank"); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
function countChar(stringOfCharacters, findCharacter) { | ||
return 5 | ||
let count = 0; | ||
for (let i = 0; i < stringOfCharacters.length; i++) { | ||
if (stringOfCharacters[i] === findCharacter) { | ||
count++; | ||
} | ||
} | ||
return count; | ||
} | ||
|
||
module.exports = countChar; | ||
module.exports = countChar; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 12 additions & 2 deletions
14
Sprint-3/3-mandatory-practice/implement/get-ordinal-number.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,15 @@ | ||
function getOrdinalNumber(num) { | ||
return "1st"; | ||
if (typeof num !== "number" || !Number.isInteger(num)) { | ||
throw new Error("Input must be an integer."); | ||
} | ||
|
||
const suffixes = ["th", "st", "nd", "rd"]; | ||
const remainder = num % 100; | ||
|
||
return ( | ||
num + | ||
(suffixes[(remainder - 20) % 10] || suffixes[remainder] || suffixes[0]) | ||
); | ||
} | ||
|
||
module.exports = getOrdinalNumber; | ||
module.exports = getOrdinalNumber; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Can you explain how this expression work?
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.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
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?
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)