Rosetta Code: Count the coins

Rosetta Code: Count the coins

There are four types of common coins in [US]

  • quarters (25 cents)
  • dimes (10 cents)
  • nickels (5 cents), and
  • pennies (1 cent)

There are six ways to make change for 15 cents:

  • A dime and a nickel
  • A dime and 5 pennies
  • 3 nickels
  • 2 nickels and 5 pennies
  • A nickel and 10 pennies
  • 15 pennies

Implement a function to determine how many ways there are to make change for a dollar using these common coins (1 dollar = 100 cents)

My code is as below: I dont know where i made mistake, when i try in repl.it it looks okay but here it is not accepting my answer. Please help.

function countCoins() {
var count = 0;
var pennies = 1;
var nickles = 5;
var dimes = 10;
var quarteres = 25;

for(var q =0; q<=4; q++){
  for(var d =0; d<=10; d++){
    for(var n =0; n<=20; n++){
      for(var p =0; p<=100; p++){

    total = pennies * p + nickles * n + dimes * d + quarteres * q;

    if (total == 100){
        count++;
    }

      }
    }
  }
}

console.log(count);
return count;

}

Challenge: Count the coins

Link to the challenge:

I believe that you did not declare the variable total.

2 Likes

Oh thanks, problem solved … but why repl.it did not showed any errors :smile:

FCC’s environment uses ‘strict mode’ by default: