1- // Implement a function isProperFraction.
2- // When given two numbers, a numerator and a denominator, it should return true
3- // if the given numbers form a proper fraction, and false otherwise.
1+ // Implement a function isProperFraction,
2+ // when given two numbers, a numerator and a denominator, it should return true if
3+ // the given numbers form a proper fraction, and false otherwise.
44
55// Assumption: The parameters are valid numbers (not NaN or Infinity).
66
7- // Definition:
8- // A proper fraction is a fraction where:
9- // - the denominator is not zero
10- // - both numbers are non-negative
11- // - the numerator is smaller than the denominator
7+ // Note: If you are unfamiliar with proper fractions, please look up its mathematical definition.
128
139// Acceptance criteria:
14- // After implementing the function, write tests to cover all cases
15- // and run the code to ensure all tests pass.
10+ // After you have implemented the function, write tests to cover all the cases, and
11+ // execute the code to ensure all tests pass.
1612
1713function isProperFraction ( numerator , denominator ) {
18- // A fraction with denominator 0 is invalid
14+ // A fraction with denominator 0 is invalid.
1915 if ( denominator === 0 ) {
2016 return false ;
2117 }
2218
23- // Negative values are not allowed
19+ // For this implementation, proper fractions are positive fractions
20+ // where the numerator is smaller than the denominator.
2421 if ( numerator < 0 || denominator < 0 ) {
2522 return false ;
2623 }
2724
28- // A proper fraction must have numerator smaller than denominator
2925 if ( numerator < denominator ) {
3026 return true ;
3127 }
3228
33- // All other cases are not proper fractions
3429 return false ;
3530}
3631
3732// The line below allows us to load the isProperFraction function into tests in other files.
3833// This will be useful in the "rewrite tests with jest" step.
3934module . exports = isProperFraction ;
4035
41- // Helper function for simple assertions in this file
36+ // Here's our helper again
4237function assertEquals ( actualOutput , targetOutput ) {
4338 console . assert (
4439 actualOutput === targetOutput ,
4540 `Expected ${ actualOutput } to equal ${ targetOutput } `
4641 ) ;
4742}
4843
49- // Tests to cover different combinations of numerators and denominators
44+ // TODO: Write tests to cover all cases.
45+ // What combinations of numerators and denominators should you test?
5046
5147// Example: 1/2 is a proper fraction
5248assertEquals ( isProperFraction ( 1 , 2 ) , true ) ;
5349
54- // Proper fractions (numerator smaller than denominator)
50+ // Proper fractions
5551assertEquals ( isProperFraction ( 3 , 5 ) , true ) ;
56- assertEquals ( isProperFraction ( 2 , 7 ) , true ) ;
52+ assertEquals ( isProperFraction ( 0 , 5 ) , true ) ;
5753
58- // Improper fractions (numerator greater than or equal to denominator)
54+ // Improper fractions
5955assertEquals ( isProperFraction ( 5 , 5 ) , false ) ;
6056assertEquals ( isProperFraction ( 7 , 3 ) , false ) ;
6157
62- // Negative numbers should return false
63- assertEquals ( isProperFraction ( - 2 , 7 ) , false ) ;
64- assertEquals ( isProperFraction ( 2 , - 7 ) , false ) ;
65-
66- // Zero numerator is allowed if denominator is positive
67- assertEquals ( isProperFraction ( 0 , 5 ) , true ) ;
68-
69- // Invalid fraction (denominator is zero)
58+ // Invalid fractions
7059assertEquals ( isProperFraction ( 2 , 0 ) , false ) ;
60+ assertEquals ( isProperFraction ( - 1 , 2 ) , false ) ;
61+ assertEquals ( isProperFraction ( 1 , - 2 ) , false ) ;
0 commit comments