Skip to content

Commit acd5296

Browse files
authored
Update WaterJugProblem.js
1 parent fcc6bde commit acd5296

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

Diff for: Recursive/WaterJugProblem.js

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
// WaterJugProblem.js
22

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-
*/
123
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+
1312
// Base case: If the target amount is greater than the sum of both jugs, it's not possible.
1413
if (targetAmount > jug1Capacity + jug2Capacity) return false;
1514

0 commit comments

Comments
 (0)