Cash Register Project Failing

Cash Register Project Failing
0

I’m attempting the final project in the JS & Data infrastructure section and it keeps stating that the test is failing. Here is my code (Messy and bad, yes, I apologize)

function checkCashRegister(price, cash, cid) {

  var changeDenominations  = [["PENNY", 1],["NICKEL", 5],["DIME", 10],["QUARTER", 25],["ONE", 100],["FIVE", 500],["TEN", 1000],["TWENTY", 2000],["ONE HUNDRED", 10000]];
  
  var changeGiven = [["PENNY", 0],["NICKEL", 0],["DIME", 0],["QUARTER", 0],["ONE", 0],["FIVE", 0],["TEN", 0],["TWENTY", 0],["ONE HUNDRED", 0]];
  
  var cashInRegister = 0;
  var transactionComplete = {status : null, change : null};
  var changeDue = (cash - price);

  //FIND  CHANGE IN THE DRAWER

for(var i = 0; i <cid.length;i++){
  cashInRegister += (cid[i][1] * 100);
};
cashInRegister /= 100;
checkForProperChange();
//////////////////////////////////////////////

  //INSUFFICIENT FUNDS
  if(cashInRegister < changeDue){
    transactionComplete.status = "INSUFFICIENT_FUNDS"
    console.log(transactionComplete);
      return transactionComplete;
  }

  //CHECK FOR PERFECT CHANGE
  function checkForProperChange(){
    // CHANGE CID TO CENTS
    for(var j = 0;j<cid.length;j++){
      cid[j][1] *=100;
    }
    //Check to see if the amount remaining in the register is 0, and change status to CLOSED


    changeDue *= 100; //Convert change to cents

    //Determine if the denomination is possible to subtract from, then subtract from changeDue and cid, then add to changeGiven
    for(var i=changeDenominations.length-1;i>=0;i--){
      if(changeDue - changeDenominations[i][1] >= 0 && Math.round(cid[i][1] *100) - changeDue >= 0){
        changeDue -= changeDenominations[i][1];
        cid[i][1]-=changeDenominations[i][1];
        changeGiven[i][1] += changeDenominations[i][1];
        i++;
      }
      //If changeDue = 0, determine if there is money left in the drawer to mark the transaction status as Closed or Open and slice the empty arrays if transaction status is open
      if(changeDue == 0){
        var sumAll = 0;
        for(var k = 0;k <cid.length;k++){
          sumAll += cid[k][1];
          if(k == cid.length-1 && sumAll == 0){
                    for(var j = 0;j<changeGiven.length;j++){
          changeGiven[j][1]/=100;
                    }
        transactionComplete.status = "CLOSED";
        transactionComplete.change = changeGiven;
        console.log(transactionComplete)
        return transactionComplete;
          }
        }
        for(var k = 0;k<cid.length;k++){
          if(cid[k][1] > 0){ 
        }
        for(var j = 0;j<changeGiven.length;j++){
          changeGiven[j][1]/=100;
          if(changeGiven[j][1] == 0){
            changeGiven.splice(j,1);
            j--;
          }  
        }
        transactionComplete.status = "OPEN";
        transactionComplete.change = changeGiven.reverse();
        console.log(transactionComplete)
        return transactionComplete;
        }
      }
    }
  }
}
checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]); //should return an object.

checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]); //should return {status: "OPEN", change: [["QUARTER", 0.5]]}

checkCashRegister(3.26, 100, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]); //should return {status: "OPEN", change: [["TWENTY", 60], ["TEN", 20], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.5], ["DIME", 0.2], ["PENNY", 0.04]]}

checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]); //should return {status: "INSUFFICIENT_FUNDS", change: []}

checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]); //should return {status: "INSUFFICIENT_FUNDS", change: []}

checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]) //should return {status: "CLOSED", change: [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]}


I can’t seem to understand why. Any assistance is appreciated, thank you!

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

Welcome Alec!
Can you try posting the codepen for us? It is easier to debug that way.

Cheers :+1:

Here is the codepen link. I appreciate you trying to help! https://codepen.io/AlecFletcher/pen/abzGpjV?editors=1111

Here is the challenge if anyone would like to attempt to plug the code in and check the output.

I did some poking around and changing your transaction complete variable to this fixed two of the check boxes:

var transactionComplete = {status: "", change: []};

I would have to look at it more tomorrow, but the main problem seems to be with checkbox 1. The returned transactionComplete variable is not being returned as an object. When you put in this block of code, the first checkbox is complete:

function checkCashRegister(price, cash, cid) {

  var changeDenominations  = [["PENNY", 1],["NICKEL", 5],["DIME", 10],["QUARTER", 25],["ONE", 100],["FIVE", 500],["TEN", 1000],["TWENTY", 2000],["ONE HUNDRED", 10000]];
  
  var changeGiven = [["PENNY", 0],["NICKEL", 0],["DIME", 0],["QUARTER", 0],["ONE", 0],["FIVE", 0],["TEN", 0],["TWENTY", 0],["ONE HUNDRED", 0]];
  
  var cashInRegister = 0;
  var transactionComplete = {
    status: '',
    change: []
  };
  return transactionComplete
}

checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]])

Somewhere in your code, transactionComplete is not being registered as an object.

Ahhh, I should have realized that, seeing how the first one was failing. Thank you so much! I’ll look into it.

I figured it out and it was a ridiculous mistake on my part.

        transactionComplete.status = "OPEN";
        transactionComplete.change = changeGiven.reverse();
        console.log(transactionComplete)
        return transactionComplete;
        }
      }
    }
  }
}

This was the end of the code that was not working. After doing this :

        transactionComplete.status = "OPEN";
        transactionComplete.change = changeGiven.reverse();
        console.log(transactionComplete)
        return transactionComplete;
        }
      }
    }
  }
return transactionComplete;
}

It worked. I appreciate the help and I apologize that it was such a small error.