File tree 1 file changed +8
-9
lines changed
1 file changed +8
-9
lines changed Original file line number Diff line number Diff line change 1
1
// WaterJugProblem.js
2
2
3
- /**
4
- * Function to determine if it is possible to measure exactly targetAmount
5
- * using two jugs of capacity jug1Capacity and jug2Capacity.
6
- *
7
- * @param {number } jug1Capacity - Capacity of the first jug
8
- * @param {number } jug2Capacity - Capacity of the second jug
9
- * @param {number } targetAmount - Target amount of water
10
- * @returns {boolean }
11
- */
12
3
export function canMeasureWater ( jug1Capacity , jug2Capacity , targetAmount ) {
4
+ // Input validation
5
+ if ( jug1Capacity < 0 || jug2Capacity < 0 ) {
6
+ throw new Error ( 'Invalid input: capacities must be non-negative.' ) ;
7
+ }
8
+ if ( targetAmount < 0 ) {
9
+ throw new Error ( 'Invalid input: target amount must be non-negative.' ) ;
10
+ }
11
+
13
12
// Base case: If the target amount is greater than the sum of both jugs, it's not possible.
14
13
if ( targetAmount > jug1Capacity + jug2Capacity ) return false ;
15
14
You can’t perform that action at this time.
0 commit comments