Freecodecamp: JavaScript Algorithms and Data Structures Projects: Cash Register - Not Accepting Wiki/Production Hint Code

Created on 30 Dec 2017  路  10Comments  路  Source: freeCodeCamp/freeCodeCamp



Challenge Name


Beta Challenge Link:
https://beta.freecodecamp.org/en/challenges/javascript-algorithms-and-data-structures-projects/cash-register

Wiki Link:
https://forum.freecodecamp.org/t/freecodecamp-algorithm-challenge-guide-exact-change/16012

Issue Description

The Cash Register (Exact Change in Production) challenge is not accepting the Wiki resource code for the challenge if a camper uses the Wiki for help in solving the challenge.

Unsure if the challenge was updated or if this is the original challenge from production FCC.

Please verify the challenge so it either:
1) Passes using the existing Wiki page code for this challenge
2) Update the Wiki page code for this challenge accordingly

Browser Information

Browser Name, Version:
Operating System: FireFox 57.0 (64-bit) and Chrome Version 63.0.3239.84 (Official Build) (64-bit)
Mobile, Desktop, or Tablet: Laptop Windows 10 Professional 64-bit

Your Code

`var denom = [
  { 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}
];

function checkCashRegister(price, cash, cid) {
  var change = cash - price;

  // Transform CID array into drawer object
  var register = cid.reduce(function(acc, curr) {
    acc.total += curr[1];
    acc[curr[0]] = curr[1];
    return acc;
  }, {total: 0});

  // Handle exact change
  if (register.total === change) {
    return 'Closed';
  }

  // Handle obvious insufficent funds
  if (register.total < change) {
    return 'Insufficient Funds';
  }

  // Loop through the denomination array
  var change_arr = denom.reduce(function(acc, curr) {
    var value = 0;
    // While there is still money of this type in the drawer
    // And while the denomination is larger than the change reminaing
    while (register[curr.name] > 0 && change >= curr.val) {
      change -= curr.val;
      register[curr.name] -= curr.val;
      value += curr.val;

      // Round change to the nearest hundreth deals with precision errors
      change = Math.round(change * 100) / 100;
    }
    // Add this denomination to the output only if any was used.
    if (value > 0) {
        acc.push([ curr.name, value ]);
    }
    return acc; // Return the current Change Array
  }, []); // Initial value of empty array for reduce

  // If there are no elements in change_arr or we have leftover change, return
  // the string "Insufficient Funds"
  if (change_arr.length < 1 || change > 0) {
    return "Insufficient Funds";
  }

  // Here is your change, ma'am.
  return change_arr;
}`

Screenshot


image

Most helpful comment

Hi @mstellaluna, thank you for raising this issue. I recently changed the challenge seed to address issue #16083. However, I only updated the solution in the seed file itself, not the solution on the Wiki.

@rahsheen, if you could make a new entry on the Wiki to reflect the updated challenge name, that would be great! Feel free to use the solution from the seed file listed below:

var denom = [
  { name: "ONE HUNDRED", val: 100 },
  { name: "TWENTY", val: 20 },
  { name: "TEN", val: 10 },
  { name: "FIVE", val: 5 },
  { name: "ONE", val: 1 },
  { name: "QUARTER", val: 0.25 },
  { name: "DIME", val: 0.1 },
  { name: "NICKEL", val: 0.05 },
  { name: "PENNY", val: 0.01 }
];

function checkCashRegister(price, cash, cid) {
  var output = { status: null, change: [] };
  var change = cash - price;
  var register = cid.reduce(
    function(acc, curr) {
      acc.total += curr[1];
      acc[curr[0]] = curr[1];
      return acc;
    },
    { total: 0 }
  );
  if (register.total === change) {
    output.status = "CLOSED";
    output.change = cid;
    return output;
  }
  if (register.total < change) {
    output.status = "INSUFFICIENT_FUNDS";
    return output;
  }
  var change_arr = denom.reduce(function(acc, curr) {
    var value = 0;
    while (register[curr.name] > 0 && change >= curr.val) {
      change -= curr.val;
      register[curr.name] -= curr.val;
      value += curr.val;
      change = Math.round(change * 100) / 100;
    }
    if (value > 0) {
      acc.push([curr.name, value]);
    }
    return acc;
  }, []);
  if (change_arr.length < 1 || change > 0) {
    output.status = "INSUFFICIENT_FUNDS";
    return output;
  }
  output.status = "OPEN";
  output.change = change_arr;
  return output;
}

All 10 comments

I can work on this if that's ok. Seems like a simple change of the challenge in what it should return.

```diff
@@ -23,12 +23,12 @@

// Handle exact change
if (register.total === change) {
- return 'Closed';
+ return { status: "CLOSED", change: cid };
}

// Handle obvious insufficent funds
if (register.total < change) {
- return 'Insufficient Funds';
+ return { status: "INSUFFICIENT_FUNDS", change: [] };
}

// Loop through the denomination array
@@ -54,11 +54,11 @@
// If there are no elements in change_arr or we have leftover change, return
// the string "Insufficient Funds"
if (change_arr.length < 1 || change > 0) {
- return "Insufficient Funds";
+ return { status: "INSUFFICIENT_FUNDS", change: [] };
}

// Here is your change, ma'am.
- return change_arr;
+ return {status: "OPEN", change: change_arr};
}

// test here

@rahsheen If you want to work on it go for it :) There doesn't appear to be anyone working on it. Happy Coding!

Hi @mstellaluna, thank you for raising this issue. I recently changed the challenge seed to address issue #16083. However, I only updated the solution in the seed file itself, not the solution on the Wiki.

@rahsheen, if you could make a new entry on the Wiki to reflect the updated challenge name, that would be great! Feel free to use the solution from the seed file listed below:

var denom = [
  { name: "ONE HUNDRED", val: 100 },
  { name: "TWENTY", val: 20 },
  { name: "TEN", val: 10 },
  { name: "FIVE", val: 5 },
  { name: "ONE", val: 1 },
  { name: "QUARTER", val: 0.25 },
  { name: "DIME", val: 0.1 },
  { name: "NICKEL", val: 0.05 },
  { name: "PENNY", val: 0.01 }
];

function checkCashRegister(price, cash, cid) {
  var output = { status: null, change: [] };
  var change = cash - price;
  var register = cid.reduce(
    function(acc, curr) {
      acc.total += curr[1];
      acc[curr[0]] = curr[1];
      return acc;
    },
    { total: 0 }
  );
  if (register.total === change) {
    output.status = "CLOSED";
    output.change = cid;
    return output;
  }
  if (register.total < change) {
    output.status = "INSUFFICIENT_FUNDS";
    return output;
  }
  var change_arr = denom.reduce(function(acc, curr) {
    var value = 0;
    while (register[curr.name] > 0 && change >= curr.val) {
      change -= curr.val;
      register[curr.name] -= curr.val;
      value += curr.val;
      change = Math.round(change * 100) / 100;
    }
    if (value > 0) {
      acc.push([curr.name, value]);
    }
    return acc;
  }, []);
  if (change_arr.length < 1 || change > 0) {
    output.status = "INSUFFICIENT_FUNDS";
    return output;
  }
  output.status = "OPEN";
  output.change = change_arr;
  return output;
}

@mstellaluna @scissorsneedfoodtoo @rahsheen Note that we are moving from using the forum Wiki to using the guide for this: https://github.com/freeCodeCamp/freeCodeCamp/issues/16388

@rahsheen, just wanted to check in. Have you started working on the guide for this challenge? If not, I could give it a go.

By all means, feel free @scissorsneedfoodtoo. :+1:

@rahsheen, thank you! I'll get started on it soon.

Finally got around to renaming and updating the guide to this challenge.

Here's the PR:
#8007

Fixed by @scissorsneedfoodtoo's PR.

function checkCashRegister(price, cash, cid) {
// figure out change amount due:
let change = cash-price
let totalCid = 0
let status = {status:'', change: []}

let cidObj = {
"PENNY": .01,
"NICKEL": .05,
"DIME": .1,
"QUARTER": .25,
"ONE": 1,
"FIVE": 5,
"TEN": 10,
"TWENTY": 20,
"ONE HUNDRED": 100}

// figure out total of cid
for (let i = 0 ; i < cid.length; i++){
totalCid += cid[i][1]
}

totalCid = Math.round(totalCid * 100) / 100;

if (change === totalCid){
status.status = "CLOSED"
status.change = cid
return status
}
else if (totalCid < change){
status.status = "INSUFFICIENT_FUNDS"
status.change = []
}

else {
for (let i = cid.length-1 ; i >= 0; i--){
if (change >= cidObj[cid[i][0]] && change >= cid[i][1]){
status.change.push(cid[i])
change -= cid[i][1]
change = Math.round(change*100)/100

    } else if (change >= cidObj[cid[i][0]] && change < cid[i][1]){
        let amount = Math.floor(change/cidObj[cid[i][0]])*cidObj[cid[i][0]]

        status.change.push([cid[i][0], amount])
        change -=  amount
        change = Math.round(change*100)/100
    }
  }

}

if (change >= .01){
status.status = "INSUFFICIENT_FUNDS"
status.change = []
} else {
status.status = "OPEN"
}

return status;
}

Was this page helpful?
0 / 5 - 0 ratings