Hi, after all the other test were approved, i noticed that the test #13 sometimes was checked and sometimes not, with the same code, i started spaming a little bit the “Run the test” and the code approved, #13 and #19 was done.
I’m curious why that happens. Here is the code:
let cid = [
['PENNY', 1.01],
['NICKEL', 2.05],
['DIME', 3.1],
['QUARTER', 4.25],
['ONE', 90],
['FIVE', 55],
['TEN', 20],
['TWENTY', 60],
['ONE HUNDRED', 100]
];
const divCid = document.getElementById("cid");
const inputCash = document.getElementById("cash");
const purchaseBtn = document.getElementById("purchase-btn");
const divChange = document.getElementById("change-due");
const pPrice = document.getElementById("price");
divCid.textContent = cid
const price = 19.5;
pPrice.textContent = `Price: $${price}`;
class Register {
constructor(){
this.amountWorthChange = [[0,0,0,0,0,0,0,0,0],[1,5,10,50,100,500,1000,2000,10000],[0,0,0,0,0,0,0,0,0]]
};
calculateAmount() {
cid = cid.map((subArray) => [subArray[0],subArray[1]*100])
this.amountWorthChange[0] = this.amountWorthChange[1].map((worth, i) => cid[i][1]/worth);
}
change() {
return parseFloat(inputCash.value) - price;
};
calculateChangeDisponibility(){
this.calculateAmount()
let change = change()*100;
for (let i = 8; i > -1; i--) {
while(change >= this.amountWorthChange[1][i] && this.amountWorthChange[0][i] > 0) {
change -= this.amountWorthChange[1][i];
this.amountWorthChange[0][i] --;
this.amountWorthChange[2][i] += this.amountWorthChange[1][i];
};
};
return change
};
options() {
const cidSum = cid.reduce((acc, subArray) => acc + subArray[1], 0);
const changeDisponibility = !this.calculateChangeDisponibility();
const cash = parseFloat(inputCash.value)
if(price > cash) {
alert("Customer does not have enough money to purchase the item")
}
else if(price === cash) {
divChange.innerHTML = "No change due - customer paid with exact cash"
}
else if(cidSum === this.change() && price < cash && changeDisponibility && this.amountWorthChange[0].every(number => number===0)){
this.updateClosedText()
this.updateCid()
divCid.textContent = cid
}
else if(cidSum > this.change() && price < cash && changeDisponibility){
this.updateText()
this.updateCid()
divCid.textContent = cid
}
else if(!changeDisponibility){
divChange.innerHTML = "Status: INSUFFICIENT_FUNDS"
}
this.resetAmountChangeArray()
inputCash.value = ""
}
resetAmountChangeArray() {
this.amountWorthChange[2] = this.amountWorthChange[2].map(() => 0);
this.amountWorthChange[0] = this.amountWorthChange[0].map(() => 0);
}
updateText() {
let text = "Status: OPEN"
for (let i = 8; i > -1; i--) {
if(this.amountWorthChange[2][i] > 0) {
text += `</br>${cid[i][0]}: $${this.amountWorthChange[2][i]/100}`
}
}
return divChange.innerHTML = text
}
updateClosedText() {
let text = "Status: CLOSED"
for (let i = 8; i > -1; i--) {
if(this.amountWorthChange[2][i] > 0) {
text += `</br>${cid[i][0]}: $${this.amountWorthChange[2][i]/100}`
}
}
return divChange.innerHTML = text
}
updateCid() {
cid = cid.map((subArray, i) => [subArray[0], (subArray[1] - this.amountWorthChange[2][i]) / 100]);
}
};
const register = new Register
purchaseBtn.addEventListener("click", () => register.options())