-
-
Notifications
You must be signed in to change notification settings - Fork 337
Sheffield | 26-ITP-jan | Richard Frimpong | Sprint 3 | Implement and Rewrite #1152
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 6 commits
c66e7d4
2c53190
433a848
32baac9
f7e112c
3f0d84e
3965407
79c63da
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,33 +1,70 @@ | ||
| // Implement a function isProperFraction, | ||
| // when given two numbers, a numerator and a denominator, it should return true if | ||
| // the given numbers form a proper fraction, and false otherwise. | ||
| // Implement a function isProperFraction. | ||
| // When given two numbers, a numerator and a denominator, it should return true | ||
| // if the given numbers form a proper fraction, and false otherwise. | ||
|
|
||
| // Assumption: The parameters are valid numbers (not NaN or Infinity). | ||
|
|
||
| // Note: If you are unfamiliar with proper fractions, please look up its mathematical definition. | ||
| // Definition: | ||
| // A proper fraction is a fraction where: | ||
| // - the denominator is not zero | ||
| // - both numbers are non-negative | ||
| // - the numerator is smaller than the denominator | ||
|
|
||
| // Acceptance criteria: | ||
| // After you have implemented the function, write tests to cover all the cases, and | ||
| // execute the code to ensure all tests pass. | ||
| // After implementing the function, write tests to cover all cases | ||
| // and run the code to ensure all tests pass. | ||
|
|
||
| function isProperFraction(numerator, denominator) { | ||
| // TODO: Implement this function | ||
| // A fraction with denominator 0 is invalid | ||
| if (denominator === 0) { | ||
| return false; | ||
| } | ||
|
|
||
| // Negative values are not allowed | ||
| if (numerator < 0 || denominator < 0) { | ||
| return false; | ||
| } | ||
|
|
||
| // A proper fraction must have numerator smaller than denominator | ||
| if (numerator < denominator) { | ||
| return true; | ||
| } | ||
|
|
||
| // All other cases are not proper fractions | ||
| return false; | ||
| } | ||
|
|
||
| // The line below allows us to load the isProperFraction function into tests in other files. | ||
| // This will be useful in the "rewrite tests with jest" step. | ||
| module.exports = isProperFraction; | ||
|
|
||
| // Here's our helper again | ||
| // Helper function for simple assertions in this file | ||
| function assertEquals(actualOutput, targetOutput) { | ||
| console.assert( | ||
| actualOutput === targetOutput, | ||
| `Expected ${actualOutput} to equal ${targetOutput}` | ||
| ); | ||
| } | ||
|
|
||
| // TODO: Write tests to cover all cases. | ||
| // What combinations of numerators and denominators should you test? | ||
| // Tests to cover different combinations of numerators and denominators | ||
|
|
||
| // Example: 1/2 is a proper fraction | ||
| assertEquals(isProperFraction(1, 2), true); | ||
|
|
||
| // Proper fractions (numerator smaller than denominator) | ||
| assertEquals(isProperFraction(3, 5), true); | ||
| assertEquals(isProperFraction(2, 7), true); | ||
|
|
||
| // Improper fractions (numerator greater than or equal to denominator) | ||
| assertEquals(isProperFraction(5, 5), false); | ||
| assertEquals(isProperFraction(7, 3), false); | ||
|
|
||
| // Negative numbers should return false | ||
| assertEquals(isProperFraction(-2, 7), false); | ||
| assertEquals(isProperFraction(2, -7), false); | ||
|
|
||
| // Zero numerator is allowed if denominator is positive | ||
| assertEquals(isProperFraction(0, 5), true); | ||
|
|
||
| // Invalid fraction (denominator is zero) | ||
| assertEquals(isProperFraction(2, 0), false); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,7 +14,32 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => { | |
| }); | ||
|
|
||
| // Case 2: Right angle | ||
| test(`should return "Right angle" when angle is exactly 90`, () => { | ||
| expect(getAngleType(90)).toEqual("Right angle"); | ||
| }); | ||
|
|
||
| // Case 3: Obtuse angles | ||
| test(`should return "Obtuse angle" when (90 < angle < 180)`, () => { | ||
| expect(getAngleType(91)).toEqual("Obtuse angle"); | ||
| expect(getAngleType(120)).toEqual("Obtuse angle"); | ||
| expect(getAngleType(179)).toEqual("Obtuse angle"); | ||
| }); | ||
|
|
||
| // Case 4: Straight angle | ||
| test(`should return "Straight angle" when angle is exactly 180`, () => { | ||
| expect(getAngleType(180)).toEqual("Straight angle"); | ||
| }); | ||
|
|
||
| // Case 5: Reflex angles | ||
| test(`should return "Reflex angle" when (180 < angle < 360)`, () => { | ||
| expect(getAngleType(181)).toEqual("Reflex angle"); | ||
| expect(getAngleType(270)).toEqual("Reflex angle"); | ||
| expect(getAngleType(359)).toEqual("Reflex angle"); | ||
| }); | ||
|
|
||
| // Case 6: Invalid angles | ||
| test(`should return "Invalid angle" for angles outside valid range`, () => { | ||
| expect(getAngleType(0)).toEqual("Invalid angle"); | ||
| expect(getAngleType(360)).toEqual("Invalid angle"); | ||
| expect(getAngleType(-10)).toEqual("Invalid angle"); | ||
| }); | ||
|
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. You could make this description more informative by indicating what this "valid range" is.
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. I removed the unsupported definition wording in I also updated the Jest description in I pushed the changes to the same branch. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,9 +2,39 @@ | |
| // We will use the same function, but write tests for it using Jest in this file. | ||
| const isProperFraction = require("../implement/2-is-proper-fraction"); | ||
|
|
||
| // TODO: Write tests in Jest syntax to cover all combinations of positives, negatives, zeros, and other categories. | ||
| // These tests cover different combinations of values such as: | ||
| // positive numbers, negative numbers, zeros, and improper fractions. | ||
|
|
||
| // Special case: numerator is zero | ||
| test(`should return false when denominator is zero`, () => { | ||
| expect(isProperFraction(1, 0)).toEqual(false); | ||
| describe("isProperFraction", () => { | ||
| // Special case: denominator is zero | ||
| test("should return false when denominator is zero", () => { | ||
| expect(isProperFraction(1, 0)).toEqual(false); | ||
| }); | ||
|
|
||
| // Special case: numerator is zero | ||
| test("should return true when numerator is zero and denominator is positive", () => { | ||
| expect(isProperFraction(0, 5)).toEqual(true); | ||
| }); | ||
|
|
||
| // Proper fraction: numerator is positive and less than denominator | ||
| test("should return true for a proper fraction", () => { | ||
| expect(isProperFraction(1, 2)).toEqual(true); | ||
| expect(isProperFraction(3, 4)).toEqual(true); | ||
| }); | ||
|
Comment on lines
+20
to
+23
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. Would be more informative to indicate the conditions This way, the person implementing the function don't have to lookup the definition of "proper fraction" (which they may find a different definition). |
||
|
|
||
| // Improper fraction: numerator is greater than or equal to denominator | ||
| test("should return false for an improper fraction", () => { | ||
| expect(isProperFraction(5, 4)).toEqual(false); | ||
| expect(isProperFraction(4, 4)).toEqual(false); | ||
| }); | ||
|
|
||
| // Negative numerator | ||
| test("should return false when numerator is negative", () => { | ||
| expect(isProperFraction(-1, 2)).toEqual(false); | ||
| }); | ||
|
|
||
| // Negative denominator | ||
| test("should return false when denominator is negative", () => { | ||
| expect(isProperFraction(1, -2)).toEqual(false); | ||
| }); | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.