Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the ways array with 1 at index 0.
DSA Javascript
const ways = new Array(amount + 1).fill(0); ways[[1]] = 1;
Attempts:
3 left
💡 Hint
Common Mistakes
Setting ways[1] = 1 instead of ways[0] = 1
Initializing all elements to 1
Forgetting to initialize ways array
✗ Incorrect
The number of ways to make amount 0 is always 1 (using no coins), so ways[0] = 1.
2fill in blank
mediumComplete the code to loop through each coin in the coins array.
DSA Javascript
for (let [1] = 0; [1] < coins.length; [1]++) { const coin = coins[[1]]; // rest of the code }
Attempts:
3 left
💡 Hint
Common Mistakes
Using coin as the loop variable which conflicts with the coin value
Using amount as loop variable which is unrelated
Using index but not consistent in the loop condition
✗ Incorrect
The variable i is commonly used as the index in loops to iterate over arrays.
3fill in blank
hardFix the error in the inner loop to update the ways array correctly.
DSA Javascript
for (let j = coin; j <= amount; j++) { ways[j] = ways[j] [1] ways[j - coin]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction which reduces the count incorrectly
Using multiplication or division which are not correct for counting ways
✗ Incorrect
We add ways[j - coin] to ways[j] because each coin can contribute to the total ways to make amount j.
4fill in blank
hardFill both blanks to complete the function header and return statement.
DSA Javascript
function coinChange([1], [2]) { // function body return ways[amount]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect parameter names like total or values
Swapping the order of parameters
✗ Incorrect
The function takes coins array and amount as parameters and returns ways[amount].
5fill in blank
hardFill all three blanks to complete the inner loop and update ways array correctly.
DSA Javascript
for (let [1] = [2]; [1] <= [3]; [1]++) { ways[j] += ways[j - coin]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using i instead of j as loop variable
Starting loop at 0 instead of coin
Ending loop before amount
✗ Incorrect
The loop variable is j starting from coin up to amount to update ways array.
