Repository navigation
London | 26-ITP-Sep | Rhoda Ajiroba | Sprint 3 | Implement and Rewrite Tests #1770
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 all commits
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 |
|---|---|---|
|
|
@@ -24,5 +24,21 @@ | |
| // execute the code to ensure all tests pass. | ||
|
|
||
| export function getCardValue(card) { | ||
| // TODO: Implement this function | ||
| const validCard = /^(A|[2-9]|10|J|Q|K)[♠♥♦♣]$/; | ||
|
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. This line and your program is correct. For me to understand your thinking process, can you please explain the breakdown of the expression you used (and any possible chance to simplify it)? Also, what are the advantages of using such expression over other implementations? No code change is required and just adding a reply to this comment would be good enough. Thank you. |
||
|
|
||
| if (!validCard.test(card)) { | ||
| throw new Error(`Expected a number followed by a suit, but got "${card}"`); | ||
| } | ||
|
|
||
| const rank = card.slice(0, -1); | ||
|
|
||
| if (rank === "A") { | ||
| return 11; | ||
| } | ||
|
|
||
| if (["J", "Q", "K"].includes(rank)) { | ||
| return 10; | ||
| } | ||
|
|
||
| return Number(rank); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,34 @@ | ||
| import { getAngleType } from "../implement/1-get-angle-type.js"; | ||
|
|
||
| // TODO: Write tests in Jest syntax to cover all cases/outcomes, | ||
| // including boundary and invalid cases. | ||
|
|
||
| // Case 1: Acute angles | ||
| test(`should return "Acute angle" when (0 < angle < 90)`, () => { | ||
| // Test various acute angles, including boundary cases | ||
| test("Classifies acute angles", () => { | ||
| expect(getAngleType(1)).toEqual("Acute angle"); | ||
| expect(getAngleType(45)).toEqual("Acute angle"); | ||
| expect(getAngleType(89)).toEqual("Acute angle"); | ||
| }); | ||
|
|
||
| // Case 2: Right angle | ||
| // Case 3: Obtuse angles | ||
| // Case 4: Straight angle | ||
| // Case 5: Reflex angles | ||
| // Case 6: Invalid angles | ||
| test("Classifies right angles", () => { | ||
| expect(getAngleType(90)).toEqual("Right angle"); | ||
| }); | ||
|
|
||
| test("Classifies obtuse angles", () => { | ||
| expect(getAngleType(91)).toEqual("Obtuse angle"); | ||
| expect(getAngleType(120)).toEqual("Obtuse angle"); | ||
| expect(getAngleType(179)).toEqual("Obtuse angle"); | ||
| }); | ||
|
|
||
| test("Classifies straight angles", () => { | ||
| expect(getAngleType(180)).toEqual("Straight angle"); | ||
| }); | ||
|
|
||
| test("Classifies reflex angles", () => { | ||
| expect(getAngleType(181)).toEqual("Reflex angle"); | ||
| expect(getAngleType(270)).toEqual("Reflex angle"); | ||
| expect(getAngleType(359)).toEqual("Reflex angle"); | ||
| }); | ||
|
|
||
| test("Identifies invalid angles", () => { | ||
| expect(getAngleType(0)).toEqual("Invalid angle"); | ||
| expect(getAngleType(360)).toEqual("Invalid angle"); | ||
| expect(getAngleType(-1)).toEqual("Invalid angle"); | ||
| expect(getAngleType(361)).toEqual("Invalid angle"); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,30 @@ | ||
| import { isProperFraction } from "../implement/2-is-proper-fraction.js"; | ||
|
|
||
| // TODO: Write tests in Jest syntax to cover all combinations of positives, negatives, zeros, and other categories. | ||
| test("Identifies positive proper fractions", () => { | ||
| expect(isProperFraction(1, 2)).toEqual(true); | ||
| expect(isProperFraction(3, 4)).toEqual(true); | ||
| }); | ||
|
|
||
| test("Identifies improper fractions", () => { | ||
| expect(isProperFraction(5, 3)).toEqual(false); | ||
| expect(isProperFraction(4, 4)).toEqual(false); | ||
| }); | ||
|
|
||
| test("Handles zero numerator", () => { | ||
| expect(isProperFraction(0, 5)).toEqual(true); | ||
| }); | ||
|
|
||
| test("Handles negative numerators", () => { | ||
| expect(isProperFraction(-1, 2)).toEqual(true); | ||
| expect(isProperFraction(-5, 2)).toEqual(false); | ||
| }); | ||
|
|
||
| test("Handles negative denominators", () => { | ||
| expect(isProperFraction(1, -2)).toEqual(true); | ||
| expect(isProperFraction(3, -2)).toEqual(false); | ||
| }); | ||
|
Comment on lines
+17
to
+25
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. How about both numerator and denominator are negative (e.g. -1/-2 and -2/-1)? |
||
|
|
||
| // Special case: numerator is zero | ||
| test(`should return false when denominator is zero`, () => { | ||
| test("Handles zero denominator", () => { | ||
| expect(isProperFraction(1, 0)).toEqual(false); | ||
| expect(isProperFraction(0, 0)).toEqual(false); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,30 @@ | ||
| import { getCardValue } from "../implement/3-get-card-value.js"; | ||
|
|
||
| // TODO: Write tests in Jest syntax to cover all possible outcomes. | ||
|
|
||
| // Case 1: Ace (A) | ||
| test(`Should return 11 when given an ace card`, () => { | ||
| test("Returns 11 for aces", () => { | ||
| expect(getCardValue("A♠")).toEqual(11); | ||
| expect(getCardValue("A♥")).toEqual(11); | ||
| expect(getCardValue("A♦")).toEqual(11); | ||
| expect(getCardValue("A♣")).toEqual(11); | ||
| }); | ||
|
|
||
| // Suggestion: Group the remaining test data into these categories: | ||
| // Number Cards (2-10) | ||
| // Face Cards (J, Q, K) | ||
| // Invalid Cards | ||
| test("Returns the correct value for number cards", () => { | ||
| expect(getCardValue("2♠")).toEqual(2); | ||
| expect(getCardValue("5♥")).toEqual(5); | ||
| expect(getCardValue("9♦")).toEqual(9); | ||
| expect(getCardValue("10♣")).toEqual(10); | ||
| }); | ||
|
|
||
| // To learn how to test whether a function throws an error as expected in Jest, | ||
| // please refer to the Jest documentation: | ||
| // https://jestjs.io/docs/expect#tothrowerror | ||
| test("Returns 10 for face cards", () => { | ||
| expect(getCardValue("J♠")).toEqual(10); | ||
| expect(getCardValue("Q♥")).toEqual(10); | ||
| expect(getCardValue("K♦")).toEqual(10); | ||
| }); | ||
|
|
||
| test("Throws an error for invalid cards", () => { | ||
| expect(() => getCardValue("invalid")).toThrow(); | ||
| expect(() => getCardValue("1♠")).toThrow(); | ||
| expect(() => getCardValue("11♠")).toThrow(); | ||
| expect(() => getCardValue("5X")).toThrow(); | ||
| expect(() => getCardValue("♠5")).toThrow(); | ||
| expect(() => getCardValue("5")).toThrow(); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| export function countChar(stringOfCharacters, findCharacter) { | ||
| return 5 | ||
| return stringOfCharacters.split(findCharacter).length - 1; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,23 @@ | ||
| export function getOrdinalNumber(num) { | ||
| return "1st"; | ||
| const lastTwoDigits = num % 100; | ||
|
|
||
| if (lastTwoDigits >= 11 && lastTwoDigits <= 13) { | ||
| return `${num}th`; | ||
| } | ||
|
|
||
| const lastDigit = num % 10; | ||
|
|
||
| if (lastDigit === 1) { | ||
| return `${num}st`; | ||
| } | ||
|
|
||
| if (lastDigit === 2) { | ||
| return `${num}nd`; | ||
| } | ||
|
|
||
| if (lastDigit === 3) { | ||
| return `${num}rd`; | ||
| } | ||
|
|
||
| return `${num}th`; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,13 @@ | ||
| export function repeatStr() { | ||
| // Your implementation of this function must *not* call String.prototype.repeat (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat). | ||
| // The goal is to re-implement that function, not to use it. | ||
| return "hellohellohello"; | ||
| export function repeatStr(str, count) { | ||
| if (count < 0) { | ||
| throw new Error("Count cannot be negative"); | ||
| } | ||
|
|
||
| let result = ""; | ||
|
|
||
| for (let i = 0; i < count; i++) { | ||
| result += str; | ||
| } | ||
|
|
||
| return result; | ||
| } |
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.
How about both numerator and denominator are negative (e.g. -1/-2 and -2/-1)?