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.
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.
44
55// Assumption: The parameters are valid numbers (not NaN or Infinity).
66
7- // Note: If you are unfamiliar with proper fractions, please look up its mathematical definition.
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
812
913// Acceptance criteria:
10- // After you have implemented the function, write tests to cover all the cases, and
11- // execute the code to ensure all tests pass.
14+ // After implementing the function, write tests to cover all cases
15+ // and run the code to ensure all tests pass.
1216
1317function isProperFraction ( numerator , denominator ) {
1418 // A fraction with denominator 0 is invalid
1519 if ( denominator === 0 ) {
1620 return false ;
1721 }
1822
19- // A proper fraction has absolute numerator smaller than absolute denominator
20- if ( Math . abs ( numerator ) < Math . abs ( denominator ) ) {
23+ // Negative values are not allowed
24+ if ( numerator < 0 || denominator < 0 ) {
25+ return false ;
26+ }
27+
28+ // A proper fraction must have numerator smaller than denominator
29+ if ( numerator < denominator ) {
2130 return true ;
2231 }
2332
33+ // All other cases are not proper fractions
2434 return false ;
2535}
2636
2737// The line below allows us to load the isProperFraction function into tests in other files.
2838// This will be useful in the "rewrite tests with jest" step.
2939module . exports = isProperFraction ;
3040
31- // Here's our helper again
41+ // Helper function for simple assertions in this file
3242function assertEquals ( actualOutput , targetOutput ) {
3343 console . assert (
3444 actualOutput === targetOutput ,
3545 `Expected ${ actualOutput } to equal ${ targetOutput } `
3646 ) ;
3747}
3848
39- // TODO: Write tests to cover all cases.
40- // What combinations of numerators and denominators should you test?
49+ // Tests to cover different combinations of numerators and denominators
4150
4251// Example: 1/2 is a proper fraction
4352assertEquals ( isProperFraction ( 1 , 2 ) , true ) ;
4453
45- // Proper fractions
54+ // Proper fractions (numerator smaller than denominator)
4655assertEquals ( isProperFraction ( 3 , 5 ) , true ) ;
47- assertEquals ( isProperFraction ( - 2 , 7 ) , true ) ;
48- assertEquals ( isProperFraction ( 2 , - 7 ) , true ) ;
56+ assertEquals ( isProperFraction ( 2 , 7 ) , true ) ;
4957
50- // Improper fractions
58+ // Improper fractions (numerator greater than or equal to denominator)
5159assertEquals ( isProperFraction ( 5 , 5 ) , false ) ;
5260assertEquals ( isProperFraction ( 7 , 3 ) , false ) ;
53- assertEquals ( isProperFraction ( - 8 , 4 ) , false ) ;
5461
55- // Zero numerator
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
5667assertEquals ( isProperFraction ( 0 , 5 ) , true ) ;
5768
58- // Invalid fraction (denominator 0 )
59- assertEquals ( isProperFraction ( 2 , 0 ) , false ) ;
69+ // Invalid fraction (denominator is zero )
70+ assertEquals ( isProperFraction ( 2 , 0 ) , false ) ;
0 commit comments