Build a Cash Register Project - Test is failed but if I click run test again then it’s success

Tell us what’s happening:

Test fail but if I click run test again then it’s success
When I run test the first time, It’s failed at testcase - 19, when I click run test again the failed testcase became testcase 13, finally when I run test again, It’s successful…

Does it mean that my solution worked? But I feel like there are something wrong and not happy with this result. :cry:

I read other related topic, and move all my global variable into the eventListener. But it’s still same problem.

Please help me :< I spent 16h for this one… Thanks so much!

Your code so far

<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=<device-width>, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <main>
        <input id="cash" type="number" >
        <div id="change-due"></div>
        <button id="purchase-btn">Purchase</button>
    </main>
    <script src="./script.js"></script>
</body>
</html>
/* file: script.js */
let price = 19.5;
let cid = [
  ['PENNY', 1.01],
  ['NICKEL', 2.05],
  ['DIME', 3.1],
  ['QUARTER', 4.25],
  ['ONE', 90],
  ['FIVE', 55],
  ['TEN', 20],
  ['TWENTY', 60],
  ['ONE HUNDRED', 100]
];
// let cid = [ ['PENNY', 0], ['NICKEL', 0], ['DIME', 0], ['QUARTER', 0], ['ONE', 0], ['FIVE', 0], ['TEN', 0], ['TWENTY', 0], ['ONE HUNDRED', 0] ];

const cashElement = document.getElementById("cash");
const purchaseBtn = document.getElementById("purchase-btn");
const changeDueElement = document.getElementById("change-due")


const checkCustomerCash = (myPrice) => {
    if(cashElement.value === ""){
        console.log("empty");
        return "";
    }
    else if(Number(cashElement.value,10) < myPrice){
        console.log("Customer does not have enough money to purchase the item");
        return 1;
    }
    else if( Number(cashElement.value,10) === myPrice){
        console.log("No change due - customer paid with exact cash");
        return 2;
    }
    else{
        console.log("else");
        return "";
    }
        
}

const calTotal = (arr) => {
    return arr.reduce((acc,item)=>acc+item, 0);
}

const resetResult = (result) => {
    result.forEach((res)=> {
        res[1] = 0;
    })
}
const cleanOutput = () => {
    changeDueElement.innerHTML = "";
}

const exchangeMoney = (cashInput, my_cid, myPrice, currency) => {
    let result = [
        ['PENNY', 0],
        ['NICKEL', ],
        ['DIME', 0],
        ['QUARTER', 0],
        ['ONE', 0],
        ['FIVE', 0],
        ['TEN', 0],
        ['TWENTY', 0],
        ['ONE HUNDRED', 0]
    ];
    let cashNow = cashInput - myPrice;
    let myCid = [...my_cid];
    let myCurrency = [...currency];
    myCurrency.reverse().forEach((cur)=>{
        const quotient = Math.floor(cashNow/cur[1]);
        console.log(cashNow, '/',cur[1]," = quotient is: ", quotient);
        const resId = result.findIndex((item)=>item[0] === cur[0]);
        const cidId = myCid.findIndex((item)=> item[0] === cur[0]);
        if(myCid[cidId][1]>=quotient*cur[1]){
            result[resId][1] = quotient*cur[1];
            myCid[cidId][1] = myCid[cidId][1] - quotient*cur[1];
            console.log(myCid[cidId][1], ' = ',myCid[cidId][1], " - ",  quotient, '*', cur[1]);
            cashNow = parseFloat(cashNow - quotient*cur[1]).toFixed(2);
            //console.log("in if - cashNow = parseFloat(",cashNow," % ", cur[1], ").toFixed(2) = ", cashNow);
        }
        else {
            result[resId][1] = myCid[cidId][1];
            myCid[cidId][1] = 0;
            cashNow = parseFloat(cashNow - result[resId][1]).toFixed(2);
            //console.log("in else - cashNow = parseFloat(",cashNow," - ", result[resId][1], ").toFixed(2) = ", cashNow);
        }
    })
    
    return result;
}


purchaseBtn.addEventListener("click", ()=>{

    //resetResult();
    cleanOutput();
    const cashInput = cashElement.value;
    const myCid = cid;
    const myPrice = price;
    const currency = [
        ['PENNY', 0.01],
        ['NICKEL', 0.05],
        ['DIME', 0.1],
        ['QUARTER', 0.25],
        ['ONE', 1],
        ['FIVE', 5],
        ['TEN', 10],
        ['TWENTY', 20],
        ['ONE HUNDRED', 100]
    ];
    const check = checkCustomerCash(myPrice);
   
    if(check!=""){
        if(check===1){
            alert("Customer does not have enough money to purchase the item");
        }
        if(check===2){
            changeDueElement.innerText = "No change due - customer paid with exact cash";
        }
    }
    else{
        let result = [
            ['PENNY', 0],
            ['NICKEL', ],
            ['DIME', 0],
            ['QUARTER', 0],
            ['ONE', 0],
            ['FIVE', 0],
            ['TEN', 0],
            ['TWENTY', 0],
            ['ONE HUNDRED', 0]
        ];
        result = exchangeMoney(cashElement.value, myCid, myPrice, currency);
        console.log("NOW RES IS: ", result);
        console.log("NOW CID IS: ", myCid);

        const resArr = result.map((res)=>res[1]);
        const cidArr = myCid.map((c)=>c[1]);

        const resTotal = calTotal(resArr);
        const cidTotal = calTotal(cidArr);
        console.log("rest Total", resTotal);

        if(resTotal < cashInput - myPrice){
            changeDueElement.innerHTML = `Status: INSUFFICIENT_FUNDS`;
        }
        else if(resTotal == cashInput - myPrice && cidTotal == 0){
            changeDueElement.innerHTML = `Status: CLOSED`;
            const res = [...result];
            res.reverse().forEach((res)=>{
                    if(res[1]!=0){
                         changeDueElement.innerHTML += ` ${res[0]}: $${res[1]} `;
                    }  
                });
        }
        else {
            changeDueElement.innerHTML = `Status: OPEN`;
            const res = [...result];
            res.reverse().forEach((res)=>{
                if(res[1]!=0){
                changeDueElement.innerHTML += ` ${res[0]}: $${res[1]} `;
                }

            });
        }   
    }
})

/* file: styles.css */

Your browser information:

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

Challenge Information:

Build a Cash Register Project - Build a Cash Register

The two tests 13 and 19 use different values each time the test is run, that means that sometimes your code gives wrong results

With

cid =   [ [ 'PENNY', 0 ],
     [ 'NICKEL', 0.05 ],
     [ 'DIME', 0 ],
     [ 'QUARTER', 0.25 ],
     [ 'ONE', 0 ],
     [ 'FIVE', 5 ],
     [ 'TEN', 0 ],
     [ 'TWENTY', 0 ],
     [ 'ONE HUNDRED', 0 ] ];
  price =74.7;
  cash.value = 80;

Your output is Status: OPEN FIVE: $5 QUARTER: $0.25 NICKEL: $0.05 but it should be closed because the change due is exactly equal to what’s in the drawer

An otherset up that fails:

cid = [ [ 'PENNY', 0.13 ],
     [ 'NICKEL', 0.45 ],
     [ 'DIME', 0.3 ],
     [ 'QUARTER', 0.5 ],
     [ 'ONE', 11 ],
     [ 'FIVE', 0 ],
     [ 'TEN', 0 ],
     [ 'TWENTY', 0 ],
     [ 'ONE HUNDRED', 0 ] ];
price = 37.62;
cash.value = 50;

this one should also be closed, but the result is Status: INSUFFICIENT_FUNDS

1 Like

Thank you so much!
I changed the if condition, use parseFloat and toFixed(2) to make format of all variables looklike this a.bc (examples: 9.12, 10.22), and it’s worked!


        if(parseFloat(resTotal).toFixed(2) < parseFloat(cashInput - myPrice).toFixed(2)){
            changeDueElement.innerHTML = `Status: INSUFFICIENT_FUNDS`;
        }
        else if(parseFloat(resTotal).toFixed(2) == parseFloat(cashInput - myPrice).toFixed(2) && cidTotal == 0){
            changeDueElement.innerHTML = `Status: CLOSED`;
            const res = [...result];
            res.reverse().forEach((res)=>{
                    if(res[1]!=0){
                         changeDueElement.innerHTML += ` ${res[0]}: $${res[1]} `;
                    }  
                });
        }

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