Cash Register help

function calculateChange(change, cid) {
  
  let arr = [];
  
  let globalChange = change;
  let twenties = cid.filter(elem => elem[0] === "TWENTY").flat();
  if(globalChange > twenties[1] && globalChange > 20)
  {
    globalChange = globalChange.toFixed(2) - twenties[1];

    arr.push(["TWENTY", twenties[1]])
  }

  let tens = cid.filter(elem => elem[0] === "TEN").flat();
  //console.log(globalChange.toFixed(2));
  if(globalChange > tens[1] && globalChange > 10){
    globalChange = globalChange.toFixed(2) - tens[1].toFixed(1);
    arr.push(["TEN", tens[1]])
  }
  //console.log(arr);
  let fives = cid.filter(elem => elem[0] === "FIVE").flat();
  //console.log(globalChange.toFixed(2));
  if(globalChange < fives[1] && globalChange > 5) {
    let residue = globalChange % 5;
    //console.log(residue.toFixed(2))
    let fivesTotal = globalChange - residue;
    //console.log(fivesTotal)
    arr.push(["FIVE", fivesTotal])
    globalChange = residue.toFixed(2);
  }
  //console.log(globalChange);
  let ones = cid.filter(elem => elem[0] === "ONE").flat();
  //console.log(ones);
  if(globalChange < ones[1] && globalChange > 1){
    let residue = globalChange % 1;
    //console.log(residue);
    let oneTotal = globalChange - residue;
    //console.log(oneTotal);
    arr.push(["ONE", oneTotal]);
    globalChange = globalChange - oneTotal;
  }
  let quarters = cid.filter(elem => elem[0] === "QUARTER").flat();
  //console.log(arr);
  if(quarters[1] > globalChange) {
    //console.log(quarters[1]);
    let residue = globalChange % .25;
    //console.log(residue);
    let quarterTotal = globalChange - residue;
    arr.push(["QUARTER", quarterTotal.toFixed(1)])
    globalChange = residue;
  }
  console.log(arr);
  let dimes = cid.filter(elem => elem[0] === "DIME").flat();
  if(dimes[1] > globalChange && globalChange != 0) {
    let residue = globalChange.toFixed(2) % .1;
    //console.log(residue.toFixed(2));
    let dimesTotal = globalChange - residue;
    globalChange = (globalChange.toFixed(2) - dimesTotal.toFixed(2));
    arr.push(["DIME", dimesTotal]);
  }
  console.log(globalChange.toFixed(2));
  
  let penny = cid.filter(elem => elem[0] === "PENNY").flat()
    let localChange = (change % 0.05);
    localChange = parseFloat(localChange.toFixed(2));
    if(localChange < penny[1] && globalChange > 0) {
      globalChange = globalChange - localChange;
      globalChange = parseFloat(globalChange.toFixed(1));
      arr.push(["PENNY", localChange]);
    }
  //console.log(arr[0][0]);
  return arr;
}
function checkCashRegister(price, cash, cid) {
  let total = 0.0;
  for(let i = 0 ; i < cid.length; i++) {
    //console.log(cid[i]);
    total += cid[i][1];
  }
  console.log("Total en caja: " + total);
  let change = cash - price;
  console.log("Total cambio: " + change);
  if(change < total) {
    let arr = calculateChange(change, cid);
    //console.log(arr);
    if(arr.length === 0) {
      return {status: "INSUFFICIENT_FUNDS", change: arr}
    }
    return { status: "OPEN", change: arr}
  }else if( change == total) {
    let arr = calculateChange(change, cid);
    return {status: "CLOSED", change: arr}
  } else {
    let arr = [];
    return {status: "INSUFFICIENT_FUNDS", change: arr}
  }
}

console.log(checkCashRegister(3.26, 100, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]))

Please update your title and post a question.


If you have a question about a specific challenge as it relates to your written code for that challenge need some help, click the Ask for Help button. This button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.


I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

console.log(checkCashRegister(3.26, 100, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]))
{ status: 'OPEN',
  change: 
   [ [ 'TWENTY', 60 ],
     [ 'TEN', 20 ],
     [ 'FIVE', 15 ],
     [ 'ONE', 1 ],
     [ 'QUARTER', '0.5' ],
     [ 'DIME', 0.2 ],
     [ 'PENNY', 0.04 ] ] }

Why is the QUARTER value a string?

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