Topic describes
The test case
Subject analysis
In this case, I use Chrome browser to directly translate the topic, inevitably resulting in some translation mistakes. So next, we will first analyze the topic and determine what kind of problem the topic wants to solve.
What arguments do they receive?
The function given in this case takes a total of three arguments.
- Parameter 1: the total price of all products owned by the customer.
- Parameter 2: Amount of money given by the customer.
- Parameter 3: is a two-dimensional array. The first element in each small array is the face value and the second element is the total value of the face value in the cash register (US $).
Their thinking
1. Create a two-dimensional array of denominations and dollars.
const cashMap = [
["PENNY".0.01],
["NICKEL".0.05],
["DIME".0.1],
["QUARTER".0.25],
["ONE".1],
["FIVE".5],
["TEN".10],
["TWENTY".20],
["ONE HUNDRED".100]]Copy the code
2. Work out how much change to give
// First work out how much change you should have
const shouldGive = cash - price;
Copy the code
3. Use a pointer to the first denomination less than or equal to the change amount
// Define a traversal pointer that points to the first value less than or equal to the amount of change required
let pointer;
for (let i = 0; i < cashMap.length; i++) {
if (cashMap[i][1] > shouldGive) {
pointer = i - 1;
break;
} else if (cashMap[i][1] === shouldGive) {
pointer = i;
break; }}Copy the code
4. Use a temporary variable and a temporary pointer to save the number to change and the pointer we found in step 3.
let tempGive = shouldGive;
let Tpointer = pointer;
// Define the final array returned
let result = []
Copy the code
5. How many notes should you take out of the cash register to find out which denomination the pointer points to?
let sub = Math.floor(tempGive / cashMap[Tpointer][1])
while (Tpointer >= 0) {
if (sub * cashMap[Tpointer][1] > cid[Tpointer][1]) {
sub = cid[Tpointer][1] / cashMap[Tpointer][1];
result.push([cid[Tpointer][0], sub * cashMap[Tpointer][1]])
tempGive = (tempGive - sub * cashMap[Tpointer][1]).toFixed(2)
Tpointer--;
if (Tpointer < 0) return { status: "INSUFFICIENT_FUNDS".change: []}; sub =Math.floor(tempGive / cashMap[Tpointer][1]);
} else {
result.push([cid[Tpointer][0], sub * cashMap[Tpointer][1]])
tempGive = (tempGive - sub * cashMap[Tpointer][1]).toFixed(2)
Tpointer--;
if (Tpointer < 0) break;
sub = Math.floor(tempGive / cashMap[Tpointer][1]); }}Copy the code
6. If there is no money in the cash register after the change, the state of CLOSE is returned
const cidSum = cid.reduce((pre,item) = > pre + item[1].0)
for (let i = 0; i < result.length; i++) {
if (cidSum === shouldGive) {
let test = [];
for (let v of cashMap) {
if (v[0] === result[i][0]) {
test.push([v[0],result[i][1]])}else {
test.push([v[0].0])}}return {
status: 'CLOSED'.change: test
}
}
}
Copy the code
All the code
function checkCashRegister(price, cash, cid) {
// Create a value hash table
const cashMap = [
["PENNY".0.01],
["NICKEL".0.05],
["DIME".0.1],
["QUARTER".0.25],
["ONE".1],
["FIVE".5],
["TEN".10],
["TWENTY".20],
["ONE HUNDRED".100]]// 1: First calculate how much change you should get
const shouldGive = cash - price;
// 2. Define a traversal pointer that points to the first denomination less than or equal to the amount of change required
let pointer;
for (let i = 0; i < cashMap.length; i++) {
if (cashMap[i][1] > shouldGive) {
pointer = i - 1;
break;
} else if (cashMap[i][1] === shouldGive) {
pointer = i;
break; }}let tempGive = shouldGive;
let Tpointer = pointer;
// Define the final array returned
let result = []
// * Here is the code that needs to be changed repeatedly
// sub indicates how many cards of the denomination to which Tpointer is pointing
let sub = Math.floor(tempGive / cashMap[Tpointer][1])
while (Tpointer >= 0) {
if (sub * cashMap[Tpointer][1] > cid[Tpointer][1]) {
sub = cid[Tpointer][1] / cashMap[Tpointer][1];
result.push([cid[Tpointer][0], sub * cashMap[Tpointer][1]])
tempGive = (tempGive - sub * cashMap[Tpointer][1]).toFixed(2)
Tpointer--;
if (Tpointer < 0) return { status: "INSUFFICIENT_FUNDS".change: []}; sub =Math.floor(tempGive / cashMap[Tpointer][1]);
} else {
result.push([cid[Tpointer][0], sub * cashMap[Tpointer][1]])
tempGive = (tempGive - sub * cashMap[Tpointer][1]).toFixed(2)
Tpointer--;
if (Tpointer < 0) break;
sub = Math.floor(tempGive / cashMap[Tpointer][1]);
}
}
result = result.filter(item= > item[1]! = =0)
const cidSum = cid.reduce((pre,item) = > pre + item[1].0)
for (let i = 0; i < result.length; i++) {
if (cidSum === shouldGive) {
let test = [];
for (let v of cashMap) {
if (v[0] === result[i][0]) {
test.push([v[0],result[i][1]])}else {
test.push([v[0].0])}}return {
status: 'CLOSED'.change: test
}
}
}
return { status: "OPEN".change: result }
}
checkCashRegister(19.5.20The [["PENNY".0.5], ["NICKEL".0], ["DIME".0], ["QUARTER".0], ["ONE".0], ["FIVE".0], ["TEN".0], ["TWENTY".0], ["ONE HUNDRED".0]]);
Copy the code
The title to reflect
- Don’t be afraid of difficult problems. First of all, learn to break them down into small problems.
- By solving one small problem after another, the big difficult problems will come naturally.
- Must be familiar with common API operations such as rounding, reserving decimals, summing, and de-weighting.