- Joined
- Feb 22, 2023
Is my code niggerlicious?
JavaScript:
let price = 3.26;
let cid = [
["PENNY", 1.01],
["NICKEL", 2.05,
["DIME", 3.1],
["QUARTER", 4.26],
["ONE", 90],
["FIVE", 55],
["TEN", 20],
["TWENTY", 60],
["ONE HUNDRED", 100]
];
let initCID = [
["PENNY", 0],
["NICKEL", 0],
["DIME", 0],
["QUARTER", 0],
["ONE", 0],
["FIVE", 0],
["TEN", 0],
["TWENTY", 0],
["ONE HUNDRED", 0]
]
function findInititalCID(){
for(let i = 0; i < cid.length; i++){
initCID[i][1] = cid[i][1]
}
console.log(initCID);
}
function initCash(){
for(let i=0; i < cid.length; i++){
cid[i][1] = initCID[i][1];
}
return cidDisplayUpdate();
}
let totalCID = 0;
function funds(){
totalCID = 0
let x = 0;
totalCID = 0;
for(let i = 0; i < cid.length; i++){
let y = cid[i][1]
x = parseFloat((x+y).toFixed(2));
}
return totalCID = x;
}
// number input
const cash = document.querySelector("#cash")
//purchase button
const purchaseButton = document.querySelector("#purchase-btn");
purchaseButton.onclick = purchase;
//price
const pDisplay = document.querySelector("#price");
pDisplay.innerText = `Price: $${price}`;
//change due
const displayChangeDue = document.querySelector("#change-due");
//Variables for the cid display
const penny = document.querySelector("#penny");
penny.innerText = `$${cid[0][1]}`;
const nickel = document.querySelector("#nickel");
nickel.innerText = `$${cid[1][1]}`;
const dime = document.querySelector("#dime");
dime.innerText = `$${cid[2][1]}`;
const quarter = document.querySelector("#quarter");
quarter.innerText = `$${cid[3][1]}`;
const one = document.querySelector("#one");
one.innerText = `$${cid[4][1]}`;
const five = document.querySelector("#five");
five.innerText = `$${cid[5][1]}`;
const ten = document.querySelector("#ten");
ten.innerText = `$${cid[6][1]}`;
const twenty = document.querySelector("#twenty");
twenty.innerText = `$${cid[7][1]}`;
const hundred = document.querySelector("#hundred");
hundred.innerText = `$${cid[8][1]}`;
//Variables for the change-due element
const open = "OPEN";
const close = "CLOSED"
const insufficientFunds = "<p>Status: INSUFFICIENT_FUNDS</p>"
//function to update cid display
function cidDisplayUpdate(){
penny.innerText = `$${cid[0][1]}`;
nickel.innerText = `$${cid[1][1]}`;
dime.innerText = `$${cid[2][1]}`;
quarter.innerText = `$${cid[3][1]}`;
one.innerText = `$${cid[4][1]}`;
five.innerText = `$${cid[5][1]}`;
ten.innerText = `$${cid[6][1]}`;
twenty.innerText = `$${cid[7][1]}`;
hundred.innerText = `$${cid[8][1]}`;
}
//calculate change back function
function calculate(x){
cidDisplayUpdate();
let y = (x*1).toFixed(2);
x = parseFloat(y);
if(x == 0){
return console.log('Complete')
} else if (x < 0){
return console.log(`ERROR! X: ${x}`)
} else {
if(cid[8][1] > 0 && x >= 100){ //ONE HUNDRED
y = cid[8][1]/100
x -= y*100
cid[8][1] -= y*100;
console.log(x, y, cid[8][1]);
displayChangeDue.innerHTML += `<p>${cid[8][0]}: $${y*100}</p>`
return calculate(x);
}
if(cid[7][1] > 0 && x >= 20){ //TWENTY
if(Math.floor(x/20) > cid[7][1]/20){
y = (cid[7][1] / 20)
} else {
y = Math.floor(x/20)
}
x -= y*20
cid[7][1] -= y*20;
console.log(x, y, cid[7][1]);
displayChangeDue.innerHTML += `<p>${cid[7][0]}: $${y*20}</p>`
return calculate(x);
}
if(cid[6][1] > 0 && x >= 10){ //TEN
if(Math.floor(x/10) > cid[6][1]/10){
y = (cid[6][1] / 10)
} else {
y = Math.floor(x/10)
}
x -= y*10
cid[6][1] -= y*10;
displayChangeDue.innerHTML += `<p>${cid[6][0]}: $${y*10}</p>`
return calculate(x);
}
if(cid[5][1] > 0 && x >= 5){//FIVE
if(Math.floor(x/5) > cid[5][1]/5){
y = (cid[5][1] / 5)
} else {
y = Math.floor(x/5)
}
x -= y*5
cid[5][1] -= y*5
displayChangeDue.innerHTML += `<p>${cid[5][0]}: $${y*5}</p>`
return calculate(x)
}
if(cid[4][1] > 0 && x >= 1){//One
if(Math.floor(x) > cid[4][1]){
y = cid[4][1];
} else {
y = Math.floor(x)
}
x -= y;
cid[5][1] -= y;
displayChangeDue.innerHTML += `<p>${cid[4][0]}: $${y}</p>`
return calculate(x)
}
if(cid[3][1] > 0 && x >= 0.25){ //Quarter
if(Math.floor(x/0.25) > (cid[3][1] /0.25)){
y = cid[3][1]/0.25
} else {
y = Math.floor(x/0.25)
}
x -= parseFloat((y*0.25).toFixed(2))
cid[3][1] -= parseFloat((y*0.25).toFixed(2))
displayChangeDue.innerHTML += `<p>${cid[3][0]}: $${y*0.25}</p>`
return calculate(x)
}
if(cid[2][1] > 0 && x >= 0.1){ //Dime
if(Math.floor(x/0.1) > (cid[2][1] /0.1)){
y = cid[2][1]/0.1
} else {
y = Math.floor(x/0.1)
}
x -= parseFloat((y*0.1).toFixed(2));
cid[2][1] -= parseFloat((y*0.1).toFixed(2));
displayChangeDue.innerHTML += `<p>${cid[2][0]}: $${y*0.1}</p>`
return calculate(x)
}
if(cid[1][1] > 0 && x >= 0.05){ //NICKEL
if(Math.floor(x/0.05) > (cid[1][1] / 0.05)){
y = cid[1][1]/0.05
} else {
y = Math.floor(x/0.05)
}
x -= parseFloat((y*0.05).toFixed(2))
cid[1][1] -= parseFloat((y*0.05).toFixed(2))
displayChangeDue.innerHTML += `<p>${cid[1][0]}: $${y*0.05}</p>`
return calculate(x)
}
if(cid[0][1] > 0 && x >= 0.01){ //PENNY
if(Math.floor(x/0.01) > (cid[0][1] / 0.01)){
y = cid[0][1]/0.01
} else {
y = Math.floor(x/0.01)
}
x -= parseFloat((y*0.01).toFixed(2))
cid[0][1] -= parseFloat((y*0.01).toFixed(2))
displayChangeDue.innerHTML += `<p>${cid[0][0]}: $${y*0.01}</p>`
return calculate(x)
} else {
displayChangeDue.innerHTML = `${insufficientFunds}`;
return initCash();
}
}
}
//purchase function
function purchase(){
funds();
console.log("CID:" + totalCID);
let changeDue = (cash.value - price).toFixed(2);
console.log("Change:" + changeDue)
findInititalCID();
if(cash.value < price){
return alert("Customer does not have enough money to purchase the item");
} else if(cash.value == price){
return displayChangeDue.innerHTML = "<p> No change due - customer paid with exact cash </p>";
} else if (totalCID < changeDue){
return displayChangeDue.innerHTML = `${insufficientFunds}`;
} else if(totalCID == changeDue){
displayChangeDue.innerHTML = `<p>Status: Closed</p>`
return calculate(changeDue);
} else {
displayChangeDue.innerHTML = `<p>Status: OPEN</p>`
return calculate(changeDue);
}
}
JavaScript:
x = parseFloat((x+y).toFixed(2));