
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Summing up to amount with fewest coins in JavaScript
Problem
We are required to write a JavaScript function that takes in arr, arr, as the first argument. This array basically specifies the different types of coin denominations we have.
The second argument to the function is a number, amount, which specifies the amount we want to add up to. Our function should simply return the minimum number of coins required to add up to that amount.
If we can, in no way, reach amount, we should return -1.
For example, if the input to the function is −
const arr = [1, 2, 5]; const amount = 17;
Then the output should be −
const output = 4;
Output Explanation:
Because the amount can be achieved using 3 coins of 5 and 1 coin of 2.
Example
The code for this will be −
const arr = [1, 2, 5]; const amount = 17; const minCoins = (arr = [], amount = 1) => { const changes = []; changes[0] = 0; while(changes.length <= amount){ let change = Math.pow(2, 31) - 1; for (let i = 0; i < arr.length; i++) { if (changes.length - arr[i] < 0){ continue; }; change = Math.min(change, 1 + changes[changes.length - arr[i]]); }; changes.push(change); }; return changes[amount] == Math.pow(2, 31) - 1 ? -1 : changes[amount]; }; console.log(minCoins(arr, amount));
Output
And the output in the console will be −
4
- Related Articles
- Summing up unique array values in JavaScript
- Summing up digits and finding nearest prime in JavaScript
- Modified version of summing an array with recursion in JavaScript
- Summing up all the digits of a number until the sum is one digit in JavaScript
- Tiling a Rectangle with the Fewest Squares in C++
- Converting to hex and summing the numeral part in JavaScript
- Summing numbers from a string - JavaScript
- JavaScript - summing numbers from strings nested in array
- Find out the minimum number of coins required to pay total amount in C++
- C++ program to check the coins forms x amount of rupees or not
- Summing array of string numbers using JavaScript
- Summing cubes of natural numbers within a range in JavaScript
- Summing numbers present in a string separated by spaces using JavaScript
- Summing all the unique values of an array - JavaScript
- Program to find number of coins needed to make the changes with given set of coins in Python

Advertisements