简体   繁体   中英

slice() Methods/Javascript

 function checkCashRegister(price, cash, cid) { let amount = { "ONE HUNDRED": 100, "TWENTY": 20, "TEN": 10, "FIVE": 5, "ONE": 1, "QUARTER": 0.25, "DIME": 0.1, "NICKEL": 0.05, "PENNY": 0.01 } let copy = cid.slice(); let sum = cid.map(el => (el[1])).reduce((a, b) => (a + b)); let changeDue = cash - price; let newArr = []; let result = {}; if (sum === changeDue) { result.status = 'CLOSED'; result.change = copy; } for (let i = cid.length - 1; i >= 0; i--) { let count = 0; let unit = amount[cid[i][0]]; while (unit <= changeDue && unit <= cid[i][1]) { changeDue -= unit; cid[i][1] -= unit; count++; } newArr.push([cid[i][0], count * unit]); } return result; } console.log(checkCashRegister(19.5, 20, [ ["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0] ]));

Hello, I am trying to solve a problem that I should print the status and the amount of change. I am in the middle of process, and I have a method related question.

Since if the amount of the money cash machine has is the same as cash subtracted by price, I should print "status: close" and the money, I copied the "cid" array(which is input) with slice() method so that It does not affect the "cid" that I am going to use in the other condition where I use it to calculate the amount of change.

so to check if the closed status works well, I printed it, and I expected PENNY to be 0.5 but, for some reason, it has been affected by the code below if-statement(for loop part) and the PENNY part is 0.000XXX. so when I tried to move the return statement right below the if-statement. it worked as I expected.

My question is I expected the for loop neither to affect nor to be affected because slice() methods does not alter the original arry(cid), but it did not work as I expected.

Can anyone figure out why?

The problem with your code is that it subtracts pennies as fractional values, so when you do arithmetics with number of pennies you might end up with non-rounded pennies, I mean you can get 0.0799999 instead of 0.08, etc. This is because 0.01 is internally represented only approximately. This is not the case with integer values, which are always represented exactly. So, the first thing I would do I would get rid of fractions by working with number of pennies instead of number of dollars (where 1 penny is 1, one dollar is 100, 100 dollars is 10000 etc)

Consider the example:

console.log(100 - (0.01 + 0.01)) // 99.98
console.log(100 - 0.01 - 0.01) // 99.97999999999999

As for the main question about why result is affected - this is because although you indeed copied the array by

let copy = cid.slice();

nevertheless, the new array has the same elements. I mean copy[1] and cid[1] both points to the same JS object, which happen to be ['PENNY', 0.5]. And this is really the same object in absolute sense.

See, you didn't do 'deep copy', you only did 'shallow' copy. You copied the container, but inner elements are the same, they are linked to the same exact things. Like this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM