Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

270
Visualizações
Unable to return correct change in JavaScript Cash Register

I am learning Javascript through FreeCodeCamp. Managed to complete the whole course, but one test is failing on the final challenge.

This is a Javascript Cash Register that accepts 3 arguments: Price, Cash, Cash in the drawer of the till. It should calculate the change to be returned, and returning this as an object, with the status and change values. All tests pass and return the correct value, apart from the below:

checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]) 

Is currently returning

{ status: 'INSUFFICIENT_FUNDS', change: [] }

should return

return {status: "CLOSED", change: [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]}.

I believe the issue is with returning ["PENNY", 0.5]. Or something to do with the costDiff variable.

Please see my code below:

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;
}

I have been really stumped on this. Thank you so much in advance.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda