Estoy aprendiendo Javascript a través de FreeCodeCamp. Se las arregló para completar todo el curso, pero una prueba está fallando en el desafío final.
Esta es una caja registradora Javascript que acepta 3 argumentos: Precio, Efectivo, Efectivo en el cajón de la caja. Debe calcular el cambio a devolver, y devolverlo como un objeto, con el estado y los valores de cambio. Todas las pruebas pasan y devuelven el valor correcto, excepto las siguientes:
checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])actualmente está regresando
{ status: 'INSUFFICIENT_FUNDS', change: [] }debería volver
return {status: "CLOSED", change: [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]}.Creo que el problema es devolver ["PENNY", 0.5]. O algo que ver con la variable costDiff.
Por favor vea mi código abajo:
function checkCashRegister(price, cash, cid) { let currencies = [ ["ONE HUNDRED", 10000], ["TWENTY", 2000], ["TEN", 1000], ["FIVE", 500], ["ONE", 100], ["QUARTER", 25], ["DIME", 10], ["NICKEL", 5], ["PENNY", 1] ]; let change = []; let status = ""; // How much change is needed let costDiff = cash * 100 - price * 100; let costDiffCopy = costDiff; // What money types and amount is in the till let tillCash = cid.filter(money =>money[1] > 0).reverse(); // How much total money is in the till let tillTotal = 0; for (let i =0; i < tillCash.length; i++){ let cashSum = tillCash[i][1]*100; tillTotal += cashSum; let changeAmount = 0; while(costDiff >= currencies[i][1] && cashSum > 0){ costDiff -= currencies[i][1]; cashSum -= currencies[i][1]; changeAmount += currencies[i][1]; } if (changeAmount !== 0){ change.push([tillCash[i][0],changeAmount / 100]); } }; if (costDiff > 0){ status = "INSUFFICIENT_FUNDS"; change = []; } else if (costDiff == 0 && costDiffCopy == tillTotal){ status = "CLOSED"; change = cid; } else { status = "OPEN"; } let result = {"status": status, "change": change}; return result; }He estado realmente perplejo en esto. Muchas gracias de antemano.