Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

Review my code and please tell me what should I have to change

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="cashreg.css">
</head>
<body>
    <input id="cash">
    <div id="change-due"></div>
    <button id="purchase-btn" onclick="submit()"> </button>
    <script src="script.js"></script>
</body>
</html>
/* file: styles.css */

/* file: script.js */
const cash = document.getElementById('cash')
const change = document.getElementById('change-due')
const purchase= document.getElementById('purchase-btn')

let c1 = parseFloat(cash.value).toFixed(2)
let price = 3.26;
price = price.toFixed(2)
let cid = [
    ["ONE HUNDRED", 100],
    ["TWENTY", 60],
    ["TEN", 20],
    ["FIVE", 55],
    ["ONE", 90],
    ["QUARTER", 4.25],
    ["DIME", 3.1],
    ["NICKEL", 2.05],
    ["PENNY", 1.01],
];
let arr =[100,20,10,5,1,0.25,0.1,0.05,0.01]


function submit()
{
    // console.log(cash.value - price)
    if((cash.value -price)< 0)
    {
        alert('Customer does not have enough money to purchase the item')
    }

    else if (cash.value == price)
    {   
        change.textContent = 'No change due - customer paid with exact cash'
    }
    else
    {
        
        let mon = parseFloat(c1 - price).toFixed(2);
        // console.log(mon)
        let str = 'Status: OPEN'
       for(let i=0;i<cid.length;i++)
       {
            
            if(mon >  arr[i] && cid[i][1]>0)
            {
                let diff;
                let x = (Math.floor(mon/arr[i])*arr[i]).toFixed(2);
                if((cid[i][1]-x)<0)
                {
                    mon = (mon - cid[i][1]).toFixed(2);
                    diff=cid[i][1];
                    cid[i][1] = 0;
                }
                else{
                    mon = (mon - x).toFixed(2);
                    diff = x;
                    cid[i][1] = cid[i][1]-x;
                }
                console.log(mon)
                // let diff = mon - cid[i][1] ;
                // cid[i][1] = (cid[i][1]-(mon-diff));
                // console.log(mon +" "+ " "+cid[i][1])
                str = str + " "+`${cid[i][0]}: ${diff}`
                // mon = diff
            }
            if(mon == 0)
            {
                break;
            }
       }
       if(mon == 0.00 )
       {
        change.textContent = str;
       }
       else{
        change.textContent = 'Status: INSUFFICIENT_FUNDS'
       }
    //    change.textContent = change.textContent + `Status: OPEN ${cid[i][0]}: ${cid[i][1]}`
    }
}

Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:123.0) Gecko/20100101 Firefox/123.0

Challenge Information:

Build a Cash Register Project - Build a Cash Register

what kind of review do you need? do you need help finding a bug? or are you asking for a style review?

Yes bug. My code didn’t pass all the test cases

the cash value is given when clicking the button, but this line execute only at startup of the app, you need to have the code you want executed when the button is clicked in a function executed at the right time

I changed this line but it is not passing all test cases

can you show your new code please?

Yaah Sure.
Code:

const cash = document.getElementById('cash')
const change = document.getElementById('change-due')
const purchase= document.getElementById('purchase-btn')


let price = 3.26;
price = price.toFixed(2)
let cid = [
    ["ONE HUNDRED", 100],
    ["TWENTY", 60],
    ["TEN", 20],
    ["FIVE", 55],
    ["ONE", 90],
    ["QUARTER", 4.25],
    ["DIME", 3.1],
    ["NICKEL", 2.05],
    ["PENNY", 1.01],
];
let arr =[100,20,10,5,1,0.25,0.1,0.05,0.01]


function submit(event)
{
    event.preventDefault()
    let c1 = parseFloat(cash.value).toFixed(2)
    console.log(cash.value - price)
    if(cash.value <price )
    {
        alert('Customer does not have enough money to purchase the item')
    }

    else if (cash.value == price)
    {   
        change.textContent = 'No change due - customer paid with exact cash'
    }
    else
    {
        
        let mon = parseFloat(c1 - price).toFixed(2);
        let str = 'Status: OPEN'
       for(let i=0;i<cid.length;i++)
       {
            
            if(mon >  arr[i] && cid[i][1]>0)
            {
                let diff;
                let x = (Math.floor(mon/arr[i])*arr[i]).toFixed(2);
                if((cid[i][1]-x)<0)
                {
                    mon = (mon - cid[i][1]).toFixed(2);
                    diff=cid[i][1];
                    cid[i][1] = 0;
                }
                else{
                    mon = (mon - x).toFixed(2);
                    diff = x;
                    cid[i][1] = cid[i][1]-x;
                }
                console.log(mon)
                str = str + " "+`${cid[i][0]}: ${diff}`
            }
            if(mon == 0)
            {
                break;
            }
       }
       if(mon == 0.00 )
       {
        change.textContent = str;
       }
       else{
        change.textContent = 'Status: INSUFFICIENT_FUNDS'
       }
    //    change.textContent = change.textContent + `Status: OPEN ${cid[i][0]}: ${cid[i][1]}`
    }
}

please format your code correctly

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 (').

I get this error

Uncaught TypeError: Cannot read properties of undefined (reading 'preventDefault')

I guess you should not use preventDefault. You don’t have a form element so you don’t need this.
Also please give some text to your button, it’s alsmost unclickable

const cash = document.getElementById('cash')
const change = document.getElementById('change-due')
const purchase= document.getElementById('purchase-btn')


let price = 3.26;
price = price.toFixed(2)
let cid = [
    ["ONE HUNDRED", 100],
    ["TWENTY", 60],
    ["TEN", 20],
    ["FIVE", 55],
    ["ONE", 90],
    ["QUARTER", 4.25],
    ["DIME", 3.1],
    ["NICKEL", 2.05],
    ["PENNY", 1.01],
];
let arr =[100,20,10,5,1,0.25,0.1,0.05,0.01]


function submit()
{

    let c1 = parseFloat(cash.value).toFixed(2)
    console.log(cash.value - price)
    if(cash.value <price )
    {
        alert('Customer does not have enough money to purchase the item')
    }

    else if (cash.value == price)
    {   
        change.textContent = 'No change due - customer paid with exact cash'
    }
    else
    {
        
        let mon = parseFloat(c1 - price).toFixed(2);
        let str = 'Status: OPEN'
       for(let i=0;i<cid.length;i++)
       {
            
            if(mon >  arr[i] && cid[i][1]>0)
            {
                let diff;
                let x = (Math.floor(mon/arr[i])*arr[i]).toFixed(2);
                if((cid[i][1]-x)<0)
                {
                    mon = (mon - cid[i][1]).toFixed(2);
                    diff=cid[i][1];
                    cid[i][1] = 0;
                }
                else{
                    mon = (mon - x).toFixed(2);
                    diff = x;
                    cid[i][1] = cid[i][1]-x;
                }
                console.log(mon)
                str = str + " "+`${cid[i][0]}: ${diff}`
            }
            if(mon == 0)
            {
                break;
            }
       }
       if(mon == 0.00 )
       {
        change.textContent = str;
       }
       else{
        change.textContent = 'Status: INSUFFICIENT_FUNDS'
       }
    //    change.textContent = change.textContent + `Status: OPEN ${cid[i][0]}: ${cid[i][1]}`
    }
}

i have removed the preventdefault but it is not working

this should be ./styles.css


anyway, now your code is better, some tests pass, some others don’t

I think you have a floating point error. i see you tried to deal with that by using toFixed a lot, but it would be a better solution to work with a whole number of cents

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