JavaScript Algorithms and Data Structures Projects - Cash Register

Tell us what’s happening:
the API is rendering the array with a space and the test are not passing, this is the output

[ [ ‘TWENTY’, 60 ],
[ ‘TEN’, 20 ],
[ ‘FIVE’, 15 ],
[ ‘ONE’, 1 ],
[ ‘QUARTER’, ‘0.5’ ] ]
0.04
{ status: ‘OPEN’,
change:
[ [ ‘TWENTY’, 60 ],
[ ‘TEN’, 20 ],
[ ‘FIVE’, 15 ],
[ ‘ONE’, 1 ],
[ ‘QUARTER’, ‘0.5’ ],
[ ‘DIME’, 0.2 ],
[ ‘PENNY’, 0.04 ] ] }
I was working on this a few days ago and some of the test passed and the array did not render the space, im noticing that this happens by using the push mehod on any array,

it expects

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]]) should return {status: "OPEN", change: [["TWENTY", 60], ["TEN", 20], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.5], ["DIME", 0.2], ["PENNY", 0.04]]}

Your code so far


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]]))

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36

Challenge: JavaScript Algorithms and Data Structures Projects - Cash Register

Link to the challenge:

That’s not why the tests are failing. Look at the first test you are failing. The output in the console is telling you what your function should return:

{status: "OPEN", change: [["QUARTER", 0.5]]}

Now add the following so you can see the output your function is returning for this test:

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

Is your function returning exactly what the tests are expecting?

even if I change the line that you are referring the test stills fails look

Adding the console.log wasn’t intended to fix your function. It was just to get you to look closely at your output and compare it to what the correct answer should be.

I would read what @camperextraordinaire wrote.

you’re right I needed to add
arr.push([“QUARTER”, parseFloat(quarterTotal.toFixed(1))])
and now it passes thanks for your time both of you

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