JSFiddle - React, Tailwind, and code Playground

by SASSACB

JavaScript

function drawer() { 
var price=0;
var cash = 430.32;
var cid = [["centimos",0.04], ["5 centimos",0.05], ["10 centimos", 1], ["20 centimos", 0.8], ["50 centimos",0.5],["ONE", 29], ["2 euros", 8],["5 euros", 20], ["TEN", 100], ["TWENTY", 1000], ["ONE HUNDRED", 0]]// cid == denominations array
  price *= 100; cash *= 100; // Convert to pennies
  // Really we're just trying to get the coins for cash-price
  var vals = [1,5,10,20,50,100,200,500,1000,2000,10000]; // How much each denomation is worth (in pennies)
  var cidHave = cid.map(x=>Math.round(x[1]*100)); // Convert each denomination we have into pennies (e.g. $1.05 in nickels to 105)
  var cidAmts = cidHave.map((x,i)=>Math.floor(x/vals[i])); // e.g. 105 pennies worth in nickels to 21 nickels or 20000 pennies worth in quarters to 80 quarters
  var best = [[0,0,0,0,0,0,0,0,0,0,0]]; // Start with $0.00 = 0 of each coin
  var bestScoreList = [0]; // # of coins for $0.00 is 0
  for (var i = 1; i <= cash-price; i++) { // For $0.00 to amt
    best[i] = [...Array(cid.length)].map(x=>0); // Empty array of denominations
    var bestScore = i+1; // Start just above the max amount in coins (all pennies + 1)
    for (var c = 0; c < cid.length; c++) { // For each denomination
      if (vals[c] > i || best[i-vals[c]][c] >= cidAmts[c]) continue; // If denomination is too high or we don't have enough, continue
      var bestIndex = i-vals[c]; // Index in "best" of the lower coin we're accessing. (e.g. dime and $0.25 would give $0.15 or index 15)
      var score = 0;
      if (bestScoreList[bestIndex] != 0 || bestIndex == 0) { // Make sure we don't have bestScore[bestIndex] being 0 coins AND bestIndex not being $0.00
        score = bestScoreList[bestIndex]+1; // We're adding a coin... this denomation
      }
      if (score < bestScore) { // Better score than another denomation? Heck yeah, change it!
        bestScore = score;
        best[i] = best[bestIndex].slice(0);
        best[i][c] += 1;
      }
    }
    bestScoreList[i]...