Help with CashRegister project

Help with CashRegister project
0

#1

I’m currently doing the cash register project and it took me a good amount of time to get where I am now. Only to find out my code doesn’t work with the website but when I test the code on my cmder it seems to work fine.
Here is my code and if anyone can help me get by this problem ill be super grateful, going to learn more in udemy for now.

let denomValue = [
    { name: 'ONE HUNDRED', val: 100.00 },
    { name: 'TWENTY', val: 20.00 },
    { name: 'TEN', val: 10.00 },
    { name: 'FIVE', val: 5.00 },
    { name: 'ONE', val: 1.00 },
    { name: 'QUARTER', val: 0.25 },
    { name: 'DIME', val: 0.10 },
    { name: 'NICKEL', val: 0.05 },
    { name: 'PENNY', val: 0.01 }
];
   
let checkCashRegister = function (price, cash, cid) {
  
    let change = cash - price

    // transform cid into one object (number) to work with

    let cashDrawer = cid.reduce(function(acc,curr){
        acc.total += curr[1];
        acc[curr[0]] = curr[1];
        return acc;
    }, { total: 0 });

    // Check for cash first
    // made status equal three different things depending on the if
    if (cashDrawer < change) {
        status = 'INSUFFICIENT_FUNDS'
    } else if (cashDrawer === change) {
        status = 'CLOSED'
    } else {
        status = 'OPEN'
    }
  // make a loop to go through the denom and add value from Greatest to smallest.
    let result = denomValue.reduce(function (acc, curr) {
        let value = 0
        while (cashDrawer[curr.name] > 0 && change >= curr.val) {
            change -= curr.val
            cashDrawer[curr.name] -= curr.val
            value += curr.val
            // math round to deal with run off numbers.
            change = Math.round(change * 100) / 100
        } 
        // this if statement is basically saying if we added to value print down what we added. with a push statement
        if (value > 0) {
            acc.push(['["' + curr.name + '"', value + ']'])
        }
        return acc
    }, []);
    // returned all into a simple template string
    return console.log((`{status: "${status}", change: [${result}]}`))
 }

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

Cashregister project completed and doesn't work NEED HELP
#2
let denomValue = [
    { name: 'ONE HUNDRED', val: 100.00 },
    { name: 'TWENTY', val: 20.00 },
    { name: 'TEN', val: 10.00 },
    { name: 'FIVE', val: 5.00 },
    { name: 'ONE', val: 1.00 },
    { name: 'QUARTER', val: 0.25 },
    { name: 'DIME', val: 0.10 },
    { name: 'NICKEL', val: 0.05 },
    { name: 'PENNY', val: 0.01 }
];

let checkCashRegister = function (price, cash, cid) {

    let change = cash - price

    // transform cid into one object (number) to work with

    let cashDrawer = cid.reduce(function(acc,curr){
        acc.total += curr[1];
        acc[curr[0]] = curr[1];
        return acc;
    }, { total: 0 });

    // Check for cash first
    // made status equal three different things depending on the if
    if (cashDrawer.total < change) {
        status = 'INSUFFICIENT_FUNDS'
    } else if (cashDrawer.total === change) {
        status = 'CLOSED'
    } else {
        status = 'OPEN'
    } 
    // make a loop to go through the denom and add value from Greatest to smallest.
    let result = denomValue.reduce(function (acc, curr) {
        let value = 0
        while (cashDrawer[curr.name] > 0 && change >= curr.val) {
            change -= curr.val
            cashDrawer[curr.name] -= curr.val
            value += curr.val
            // math round to deal with run off numbers.
            change = Math.round(change * 100) / 100
        } 
        // this if statement is basically saying if we added to value print down what we added. with a push statement
        if (value > 0) {
            acc.push(['["' + curr.name + '"', value.toFixed(2) + ']'])
        }
        return acc
        
    }, []);
    // returned all into a simple template string
   `{status: "${status}", change: [${result}]}`
 }

checkCashRegister(2.50, 10, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]])
checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])

#3

I’ve edited your post 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 easier to read.

Note: Backticks are not single quotes.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


#4

Thanks, I was actually trying to do this because I figured the format didn’t look appropriate. My solution, as you fixed this for me , was to post this code on a online editor and give the link in the post. Thanks again.


#5

Heres the link if anyone wants it.