Failing Tests on Project Euler Problem 31

Tell us what’s happening:
Describe your issue in detail here.
I’m trying to do problem 31. You are supposed to compute the number of ways to make n pence with different coins. I’m pretty sure this solution should work. It might be too slow. But when I run the tests I fail them all.

It’s the weirdest thing. I can do console.log(coinSums(50)) and the console will say: 451. But when I submit to the tests, it says coinsSums(50) is supposed to output 451. I don’t see the difference, so I’m not sure how to fix my solution.

  **Your code so far**

const denominations = [200, 100, 50, 20, 10, 5, 2, 1];
let coins = Array(8).fill(0);

function coinSums(n) {
//make the biggest denomination
for(let i = 0; i < coins.length; i++){
  while(checkValue(coins) + denominations[i] <= n){
let count = 1;
while(coins[coins.length-1] < n){
  coins = break2ndSmallestCoin(coins, n);
return count;

function break2ndSmallestCoin(coins, n){
for(let i = coins.length - 2; i >= 0; i--){
  if(coins[i] > 0){
    for(let j = i + 1; j < coins.length; j++){
      coins[j] = 0;
    for(let j = i + 1; j < coins.length; j++){
      while(checkValue(coins)+denominations[j] <= n){
    return coins;

function checkValue(coins){
return coins.reduce((sum, coins, i)=>sum + denominations[i] * coins, 0);

  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.2 Safari/605.1.15

Challenge: Problem 31: Coin sums

Link to the challenge:

These look like global variables… This ‘smells’ suspicious…

Global variables need to be carefully used. There are good uses, such as caching function results and storing constants, but building and sharing state between function calls can result in stale or incorrect data when calling the main function for additional problems.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.