Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exact Change - returning different output in freeCodeCamp test than codepen #11257

Closed
Shooshte opened this issue Oct 18, 2016 · 3 comments
Closed
Labels
other: decayed Stale issues that need follow up from commentators. Were closed for inactivity

Comments

@Shooshte
Copy link

Shooshte commented Oct 18, 2016

Exact Change

https://www.freecodecamp.com/challenges/exact-change

Issue Description

When I run my code locally, or in codepen it returns the required output. When I run in on the challenge page it returns a different result (I am logging output to console to see the results).

The result in the freecodecamp test is:
[["PENNY",0.49000000000000027]]

The result in codepen is:
[["QUARTER", 0.5]]

Browser Information

MacOs El Capitan, Latest Google Chrome, Desktop

Your Code

function checkCashRegister(price, cash, cid) {
    var change;
    console.log(getNotes(neededMoney(price, cash), cid));
    // Here is your change, ma'am.
    return getNotes(neededMoney(price, cash), cid);
}

function neededMoney(price, cash) {
    return cash - price;
}

function getSum(money) {
    var total = 0;
    for(var unit in money) {
        total += money[unit][1];
    }
    return total;
}

function getNotes(neededChange, cid) {
    var returnChange = [];
    var neededMonies = neededChange;
    //Reverse CID array
    cid.reverse();
    //For every unit of money in drawer
    for(var money in cid) {
        //Check the type of money
        switch(cid[money][0]) {
            case "ONE HUNDRED":
                var notes = ["ONE HUNDRED", 0];
                //Check how many bills we have
                var numberOfNotes = cid[money][1] / 100;
                //If we have bills
                if(numberOfNotes >= 1) {
                    //For every bill
                    for(var x = 0; x < numberOfNotes; x++) {
                        //If we need more bills
                        if(neededMonies >= 100) {
                            neededMonies -= 100;
                            notes[1] += 100;
                        }
                    }
                    if(notes[1] > 0) {
                        returnChange.push(notes);
                    }
                }
                break;
            case "TWENTY":
                var notes = ["TWENTY", 0];
                var numberOfNotes = cid[money][1] / 20;
                if(numberOfNotes >= 1) {
                    for(var x = 0; x < numberOfNotes; x++) {
                        if(neededMonies >= 20) {
                            neededMonies -= 20;
                            notes[1] += 20;
                        }
                    }
                    if(notes[1] > 0) {
                        returnChange.push(notes);
                    }
                }
                break;
            case "TEN":
                var notes = ["TEN", 0];
                var numberOfNotes = cid[money][1] / 10;
                if(numberOfNotes >= 1) {
                    for(var x = 0; x < numberOfNotes; x++) {
                        if(neededMonies >= 10) {
                            neededMonies -= 10;
                            notes[1] += 10;
                        }
                    }
                    if(notes[1] > 0) {
                        returnChange.push(notes);
                    }
                }
                break;
            case "FIVE":
                var notes = ["FIVE", 0];
                var numberOfNotes = cid[money][1] / 5;
                if(numberOfNotes >= 1) {
                    for(var x = 0; x < numberOfNotes; x++) {
                        if(neededMonies >= 5) {
                            neededMonies -= 5;
                            notes[1] += 5;
                        }
                    }
                    if(notes[1] > 0) {
                        returnChange.push(notes);
                    }
                }
                break;
            case "ONE":
                var notes = ["ONE", 0];
                var numberOfNotes = cid[money][1] / 1;
                if(numberOfNotes >= 1) {
                    for(var x = 0; x < numberOfNotes; x++) {
                        if(neededMonies >= 1) {
                            neededMonies -= 1;
                            notes[1] += 1;
                        }
                    }
                    if(notes[1] > 0) {
                        returnChange.push(notes);
                    }
                }
                break;
            case "QUARTER":
                var notes = ["QUARTER", 0];
                var numberOfNotes = cid[money][1] / 0.25;
                if(numberOfNotes >= 1) {
                    for(var x = 0; x < numberOfNotes; x++) {
                        if(neededMonies >= 0.25) {
                            neededMonies -= 0.25;
                            notes[1] += 0.25;
                        }
                    }
                    if(notes[1] > 0) {
                        returnChange.push(notes);
                    }
                }
                break;
            case "DIME":
                var notes = ["DIME", 0];
                var numberOfNotes = cid[money][1] / 0.1;
                if(numberOfNotes >= 1) {
                    for(var x = 0; x < numberOfNotes; x++) {
                        if(neededMonies >= 0.1) {
                            neededMonies -= 0.1;
                            notes[1] += 0.1;
                        }
                    }
                    if(notes[1] > 0) {
                        returnChange.push(notes);
                    }
                }
                break;
            case "NICKEL":
                var notes = ["NICKEL", 0];
                var numberOfNotes = cid[money][1] / 0.05;
                if(numberOfNotes >= 1) {
                    for(var x = 0; x < numberOfNotes; x++) {
                        if(neededMonies >= 0.05) {
                            neededMonies -= 0.05;
                            notes[1] += 0.05;
                        }
                    }
                    if(notes[1] > 0) {
                        returnChange.push(notes);
                    }
                }
                break;
            case "PENNY":
                var notes = ["PENNY", 0];
                var numberOfNotes = cid[money][1] / 0.01;
                if(numberOfNotes >= 1) {
                    for(var x = 0; x < numberOfNotes; x++) {
                        if(neededMonies >= 0.00) {
                            neededMonies -= 0.01;
                            notes[1] += 0.01;
                        }
                    }
                    if(notes[1] > 0) {
                        returnChange.push(notes);
                    }
                }
                break;
        }
    }
    //If we don't have enough money in drawer
    if (getSum(cid) == neededChange) {
        return 'Closed';
    }
    else if(getSum(returnChange) < neededChange) {
        return 'Insufficient Funds';
    }
    else {
        return returnChange;
    }
}

checkCashRegister(19.50, 20.00, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1.00], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);

@ghost
Copy link

ghost commented Oct 18, 2016

I copy pasted your code in FreeCodeCamp editor in assignment and in codepen.

  1. FreeCodeCamp example ~ 2 examples won't work and your var notes, var numberOfNotes are repeating with var everytime.
    fcc
  2. Codepen ~ Same result as above. No errors, output is the same.
    codepen

Maybe I did something wrong in copy/paste so others feel free to try it out too. This is what I got.

@dhcodes
Copy link
Contributor

dhcodes commented Oct 19, 2016

@Shooshte This is likely caused by how JS (and possibly even FCC and codepen) handles floating point integers. You can read more here: http://floating-point-gui.de/.

While I'm not sure how to remedy it for your code, someone in the HelpJavascript may be able to help.

This has been discussed some in #8162 and #10074

@dayfiri you can see the difference if you use the 4th test.

@raisedadead raisedadead added the other: decayed Stale issues that need follow up from commentators. Were closed for inactivity label Dec 15, 2016
@Shooshte
Copy link
Author

Shooshte commented Jan 3, 2017

@dhcode

I managed to avoid the issue by multiplying the floats with 10000, and then dividing the result before printing out the output.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
other: decayed Stale issues that need follow up from commentators. Were closed for inactivity
Projects
None yet
Development

No branches or pull requests

3 participants