0

I'm trying to find out the possible coins of given amount according to **denominators **as arguments. I iterate over the the denominators and did comparisons. but the output is not expected as i want.

function amountTocoins(amount, denominations){
    let possibleCoins = [];  
    for(let i=0; i<denominations.length; i++){
        
        let coin = denominations[i];
        while(amount >= coin){     
            possibleCoins.push(coin);  
            amount -= coin;    
        }
        if(amount === 0){  
            break;
        }
    }
    return possibleCoins;
}
console.log(amountTocoins(46, [25, 10, 5, 2, 1]));

Output should be [25, 10, 10] which is equal to denominator array [25, 10, 5, 2, 1]. because last iteration ends with 1.and we have no more element for comparison.

As run the code, it gives me [25, 10, 10, 1] as output. why ?

Majid Ali
  • 1
  • 2
  • Because you have used `>=`, which also considers `1` and pushes as possible coin – Shri Hari L Jun 15 '23 at 13:14
  • How is the output not as you expected? If you wanted to make 46c, you'd need 1x25c, 2x10c, and 1x1c, given that those coins are in the `denominations` parameter. If the output should be `[25, 10, 10]`, then surely the input would be `amountTocoins(45, [25, 10, 5, 2, 1])`? – Harrison Jun 15 '23 at 13:17
  • Welcome to Stack Overflow! This is a good opportunity for you to start familiarizing yourself with [using a debugger](https://stackoverflow.com/q/25385173/328193). When you step through the code in a debugger, which operation first produces an unexpected result? What were the values used in that operation? What was the result? What result was expected? Why? To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David Jun 15 '23 at 13:17
  • Thanks Harrison . I focused and understand the logic. – Majid Ali Jun 15 '23 at 18:59

1 Answers1

-2

The reason why the output is [25, 10, 10, 1] instead of [25, 10, 10] is because the while loop inside the for loop executes one more time after the last element of the denominations array is processed. This happens because the condition amount >= coin is still true when the last iteration ends with a coin of value 1. Therefore, the loop executes one more time, pushing 1 into the array.

To fix this, you can add an extra condition to check if the current coin is equal to 1 and if so, break out of the loop. Here's an updated version of the function:

function amountTocoins(amount, denominations){
    let possibleCoins = [];  
    for(let i=0; i<denominations.length; i++){
        
        let coin = denominations[i];
        while(amount >= coin){     
            possibleCoins.push(coin);  
            amount -= coin;    
        }
        if(amount === 0 || coin === 1){  
            break;
        }
    }
    return possibleCoins;
}

With this modification, the function should return [25, 10, 10] for the input (46, [25, 10, 5, 2, 1]).

  • Several of your answers appear likely to have been entirely or partially written by AI (e.g., ChatGPT). Please be aware that [posting of AI-generated content is banned here](//meta.stackoverflow.com/q/421831). If you used an AI tool to assist with any answer, I would encourage you to delete it. Thanks! – NotTheDr01ds Jun 15 '23 at 13:54
  • **Readers should review this answer carefully and critically, as AI-generated information often contains fundamental errors and misinformation.** If you observe quality issues and/or have reason to believe that this answer was generated by AI, please leave feedback accordingly. The moderation team can use your help to identify quality issues. – NotTheDr01ds Jun 15 '23 at 13:54
  • I wouldn't say this was generated by an AI but yes, the answer is so simple cuz all he did was add `|| coin === 1` to the condition of the question. Shouldn't be considered an answer, this could had been a comment – Chris G Jun 15 '23 at 13:57