Help on Minimal Number of Coins algorithm

I’m currentoy working on the Minimal Number of Coins algorithm and I’m a bit stuck. (The algorithm being…given three different coins, what is the lowest amount of coins one can use to buy a product costing price
I tried to use recursion at the end but it didn’t work and I think I’m a bit over my head here.
What I’d like is somebody to tell me where I’m going wrong, the wrongness of my approach (and why?). I’d really appreciate it!

My code is below.

function minimalNumberOfCoins(coins: number[], price: number): number {
let count=0;
let ten = coins[2]+coins[2];
let two = coins[1]+coins[1];
let one = coins[0]+coins[0];
let amount: any[] = [];
amount.push(one, two, ten);

for(let i = 0; i < amount.length; i++){
    if(amount[i]< price){
        if(i === 0){
            return count
        } else {

i do not know the answer but somethink that you could do is get a good nights sleep and try again in the morning.

what’s the challenge? what are the requirements? what’s passed in as argument? what is already defined outside of the function? what’s the desired output? this infos are missing to help you efficiently

this is not the way to use recursion… you need to actually call the function for it to be recursion, like this, nothing happens.

is this a syntax I don’t know or you put that in the wrong place?
if you are using freecodecamp, maybe change to a different editor that will also signal syntax errors… (like! and if you add 'use strict' quote included as first line, even better for syntax errors, more will be told to you)

I solved the problem, but I’m not exactly sure why. I’m going to post the PROBLEM along with my CODE and my THEORY as to why. If someone could clarify or correct my THEORY, then I would appreciate it.

Three coin currency amounts are listed [1,2, 10]. These can be used to pay for one item totaling 28. The goal is to write an algorithm that returns the smallest number of coins that can be used to purchase the item. The answer is 6, which would be two 10 coins and 4 2 coins.


function minimalNumberOfCoins(coins: number[], price: number): number {
//count is what is desired so instantiate that first
    let count=0;
    //instantiate variable to hold formula for decrementing price
let math: number;
//run while loop that terminates when price reaches 0
while(price !== 0){
for(let i = 0; i < coins.length; ++i){
    //for loop iterating through different currencies a& executing math function on them
    math = price - coins[i]
    //for each math function +1 to count
    if(price > math){
        //if price is greater than the result of math arithmetic, then execute math again
        //+1 to count when executing math function
//return count value after while depreciates price to 0
return count

I believe this is because the for loop allows the computer to test each of the currency amounts and return the one that utilizes the least number of coins (visualizes as count in this problem.
This theory is very vacuous because I don’t know why the computer would choose the currency amounts producing the smallest number for the correct solution. Can someone explain(if the above theory is correct) why this is so?