File tree Expand file tree Collapse file tree 1 file changed +8
-9
lines changed
Expand file tree Collapse file tree 1 file changed +8
-9
lines changed Original file line number Diff line number Diff line change 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- */
123export 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
You can’t perform that action at this time.
0 commit comments