No, i mean if the change is 124, if i divide it by 100 i would get $1.24. Is this the way i should convert an integer to dollars?
yes, that would work!
Hey, i managed to make it work like you said.
I tested it but i still have an error.
The program works great but the nickels display this problem even if i work with integers not with decimals
The number should be 205 but its showing a decimal number instead.
This is how i reversed my cid:
const reversedCid = cid.map(item => item[1] * 100).reverse();
// [10000, 6000, 2000, 5500, 9000, 425, 310, 205, 101]
console.log(reversedCid);
Maybe you can use Math.ceil to fix that?
Alright, will try that
When i console.log cashToChange and the cash.value is a big number (more than 306) i get this error:
But when the cash.value is less than 307 its fine and it wont show the error.
that’s for the number of loop cycles needed there, console.log
makes the loop slower, making it get caught in the loop protector much easier. Your possible solutions are: make a more officient loop OR remove console.log
after testing to avoid the error
Hey, i have done everything but the only thing that don’t work is when the cash.value is the same as sum.
let cash = document.getElementById("cash");
let btn = document.getElementById("purchase-btn");
let change = document.getElementById("change-due");
let drawer = document.getElementById("cash-in-drawer");
let changeDue = [];
let price = 1.87;
// let price = 0.5;
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]
];
// Display cash on the drawer
function displayDrawer() {
for(let i = 0; i < cid.length; i++) {
let el = document.createElement('p');
el.textContent = `${cid[i][0]}: ${Math.round(cid[i][1] * 100) / 100}`;
drawer.appendChild(el);
}
}
//Sum of the cash in the drawer
let sum = 0;
for(let i = 0; i < cid.length; i++){
sum += cid[i][1];
}
sum = Math.round((sum + price) * 100) / 100;
console.log(sum);
//Event listener
btn.addEventListener("click", () => {
change.textContent = "";
if(cash.value < price){
alert("Customer does not have enough money to purchase the item");
cash.value = "";
}else if(cash.value == price) {
change.textContent = "No change due - customer paid with exact cash";
cash.value = "";
}else if(cash.value === sum){
change.textContent = "Status: CLOSED";
cash.value = "";
}else if(cash.value > price && cash.value !== sum) {
let changeStatus = document.createElement('p');
changeStatus.textContent = "Status: OPEN";
change.appendChild(changeStatus);
const reversedCid = cid.map(item => Math.ceil(item[1] * 100)).reverse();
// [10000, 6000, 2000, 5500, 9000, 425, 310, 205, 101]
let oldReversedCid = cid.map(item => Math.ceil(item[1] * 100)).reverse();
const billValue = [10000, 2000, 1000, 500, 100, 25, 10, 5, 1];
let cashToChange = cash.value * 100 - price * 100;
let i = 0;
while(i < billValue.length){
if(cashToChange >= billValue[i] && reversedCid[i] !== 0){
cashToChange -= billValue[i];
reversedCid[i] -= billValue[i];
}else{
i++;
}
}
cash.value = "";
if(cashToChange > 0){
change.textContent = "Status: INSUFFICIENT_FUNDS"
}else{
for(let i = 0; i < oldReversedCid.length; i++){
changeDue.push(oldReversedCid[i] -= reversedCid[i]);
}
changeDue.reverse();
for(let i = 0; i < cid.length; i++){
cid[i][1] = Math.round((cid[i][1] - changeDue[i] / 100) * 100) / 100;
}
for(let i = 0; i < changeDue.length; i++){
if(changeDue[i] > 0){
let changeDueEl = document.createElement('p');
change.appendChild(changeDueEl);
changeDueEl.textContent = `${cid[i][0]} : ${changeDue[i] / 100}`;
}
}
//Console log UPDATED CID on CASH IN DRAWER
console.log("New Cid Updated " + cid);
drawer.innerHTML = "";
displayDrawer();
}
}
})
displayDrawer();
I have added the condition but when the cash.value == sum it will give change and say status open and not closed.
Here is the condition
cid
changes between each test, but as the app is not restarted, this doesn’t
I tried to put it inside of the eventListenen, but it still wont work.
Here is what i did:
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]
];
// Display cash on the drawer
function displayDrawer() {
for(let i = 0; i < cid.length; i++) {
let el = document.createElement('p');
el.textContent = `${cid[i][0]}: ${Math.round(cid[i][1] * 100) / 100}`;
drawer.appendChild(el);
}
}
//Event listener
btn.addEventListener("click", () => {
change.textContent = "";
//Sum of the cash in the drawer
let sum = 0;
for(let i = 0; i < cid.length; i++){
sum += cid[i][1];
}
sum = Math.round((sum + price) * 100) / 100;
console.log(sum);
if(cash.value < price){
alert("Customer does not have enough money to purchase the item");
cash.value = "";
}else if(cash.value == price) {
change.textContent = "No change due - customer paid with exact cash";
cash.value = "";
}else if(cash.value > price) {
let changeStatus = document.createElement('p');
if(cash.value === sum){
changeStatus.textContent = "Status: CLOSED";
}else {
changeStatus.textContent = "Status: OPEN";
}
change.appendChild(changeStatus);
I changed the last condition on the event listener too.
that event listener is missing a }
. If you provide code with right syntax you could get help debugging
(post deleted by author)
Yeah, i know. Thats because there is more code inside the event listener. Here is all of it:
let cash = document.getElementById("cash");
let btn = document.getElementById("purchase-btn");
let change = document.getElementById("change-due");
let drawer = document.getElementById("cash-in-drawer");
let changeDue = [];
let price = 1.87;
// let price = 0.5;
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]
];
// Display cash on the drawer
function displayDrawer() {
for(let i = 0; i < cid.length; i++) {
let el = document.createElement('p');
el.textContent = `${cid[i][0]}: ${Math.round(cid[i][1] * 100) / 100}`;
drawer.appendChild(el);
}
}
//Event listener
btn.addEventListener("click", () => {
change.textContent = "";
//Sum of the cash in the drawer
let sum = 0;
for(let i = 0; i < cid.length; i++){
sum += cid[i][1];
}
sum = Math.round((sum + price) * 100) / 100;
console.log(sum);
if(cash.value < price){
alert("Customer does not have enough money to purchase the item");
cash.value = "";
}else if(cash.value == price) {
change.textContent = "No change due - customer paid with exact cash";
cash.value = "";
}else if(cash.value > price) {
let changeStatus = document.createElement('p');
if(cash.value === sum){
changeStatus.textContent = "Status: CLOSED";
}else {
changeStatus.textContent = "Status: OPEN";
}
change.appendChild(changeStatus);
const reversedCid = cid.map(item => Math.ceil(item[1] * 100)).reverse();
// [10000, 6000, 2000, 5500, 9000, 425, 310, 205, 101]
let oldReversedCid = cid.map(item => Math.ceil(item[1] * 100)).reverse();
const billValue = [10000, 2000, 1000, 500, 100, 25, 10, 5, 1];
let cashToChange = cash.value * 100 - price * 100;
let i = 0;
while(i < billValue.length){
if(cashToChange >= billValue[i] && reversedCid[i] !== 0){
cashToChange -= billValue[i];
reversedCid[i] -= billValue[i];
}else{
i++;
}
}
cash.value = "";
if(cashToChange > 0){
change.textContent = "Status: INSUFFICIENT_FUNDS"
}else{
for(let i = 0; i < oldReversedCid.length; i++){
changeDue.push(oldReversedCid[i] -= reversedCid[i]);
}
changeDue.reverse();
for(let i = 0; i < cid.length; i++){
cid[i][1] = Math.round((cid[i][1] - changeDue[i] / 100) * 100) / 100;
}
for(let i = 0; i < changeDue.length; i++){
if(changeDue[i] > 0){
let changeDueEl = document.createElement('p');
change.appendChild(changeDueEl);
changeDueEl.textContent = `${cid[i][0]} : ${changeDue[i] / 100}`;
}
}
//Console log UPDATED CID on CASH IN DRAWER
console.log("New Cid Updated " + cid);
drawer.innerHTML = "";
displayDrawer();
}
}
})
displayDrawer();
You didn’t say what isn’t working or specify how we can help you. Right now you are relying on others to do your work for you.
Please take the time to explain what you were trying to do and what failed.
Please do some debugging and share what you have logged.