|
1 | | -// You wil need to implement a function isProperFraction |
2 | | -// You need to write assertions for your function to check it works in different cases |
3 | | - |
| 1 | +// Implement a function isProperFraction |
| 2 | +// Write assertions for your function to check it works in different cases |
4 | 3 | // Terms: |
5 | 4 | // Fractions: https://www.bbc.co.uk/bitesize/topics/zt9n6g8/articles/zjxpp4j |
6 | 5 | // Written here like this: 1/2 == Numerator/Denominator |
| 6 | +// the first test and first case is written for you |
| 7 | +// complete the rest of the tests and cases |
| 8 | +// write one test at a time, and make it pass, build your solution up methodically |
| 9 | + |
| 10 | +function isProperFraction(numerator, denominator) { |
| 11 | + if (numerator < denominator) return true; |
| 12 | +} |
| 13 | + |
| 14 | +// here's our helper again |
| 15 | +function assertEquals(actualOutput, targetOutput) { |
| 16 | + console.assert( |
| 17 | + actualOutput === targetOutput, |
| 18 | + `Expected ${actualOutput} to equal ${targetOutput}` |
| 19 | + ); |
| 20 | +} |
7 | 21 |
|
8 | 22 | // Acceptance criteria: |
9 | 23 |
|
10 | 24 | // Proper Fraction check: |
11 | 25 | // Input: numerator = 2, denominator = 3 |
12 | 26 | // target output: true |
13 | 27 | // Explanation: The fraction 2/3 is a proper fraction, where the numerator is less than the denominator. The function should return true. |
| 28 | +const properFraction = isProperFraction(2, 3); |
| 29 | +assertEquals(properFraction, true); |
14 | 30 |
|
15 | 31 | // Improper Fraction check: |
16 | 32 | // Input: numerator = 5, denominator = 2 |
17 | 33 | // target output: false |
18 | 34 | // Explanation: The fraction 5/2 is an improper fraction, where the numerator is greater than or equal to the denominator. The function should return false. |
19 | | - |
20 | | -// Zero Denominator check: |
21 | | -// Input: numerator = 3, denominator = 0 |
22 | | -// No target output: Error (Denominator cannot be zero) |
23 | | -// Explanation: The function should throw an error when the denominator is zero, as it's not a valid fraction. |
| 35 | +const improperFraction = isProperFraction(5, 2); |
| 36 | +assertEquals(improperFraction, false); |
24 | 37 |
|
25 | 38 | // Negative Fraction check: |
26 | 39 | // Input: numerator = -4, denominator = 7 |
27 | 40 | // target output: true |
28 | 41 | // Explanation: The fraction -4/7 is a proper fraction because the absolute value of the numerator (4) is less than the denominator (7). The function should return true. |
| 42 | +const negativeFraction = isProperFraction(-4, 7); |
| 43 | +// ====> complete with your assertion |
29 | 44 |
|
30 | 45 | // Equal Numerator and Denominator check: |
31 | 46 | // Input: numerator = 3, denominator = 3 |
32 | 47 | // target output: false |
33 | 48 | // Explanation: The fraction 3/3 is not a proper fraction because the numerator is equal to the denominator. The function should return false. |
34 | | -// These acceptance criteria cover a range of scenarios to ensure that the isProperFraction function handles both proper and improper fractions correctly and handles potential errors such as a zero denominator. |
| 49 | +const equalFraction = isProperFraction(3, 3); |
| 50 | +// ====> complete with your assertion |
| 51 | + |
| 52 | +// Stretch: |
| 53 | +// What other scenarios could you test for? |
0 commit comments