JavaScript Algorithms and Data Structures Projects - Cash Register

I was able to get an output of the ‘correct’ return responses for each scenario. I used Class and constructors to get the desired obj returned. It was not hard coded for specific responses, and I am unsure why it does not pass the test when it looks identical to the answer besides some auto formatted spacing from the constructor. Here is the last snippet of my code that relates to this specific part of the problem. Any advice on where to look to refactor my code/format would be greatly appreciated

WARNING

The challenge seed code and/or your solution exceeded the maximum length we can port over from the challenge.

You will need to take an additional step here so the code you wrote presents in an easy to read format.

Please copy/paste all the editor code showing in the challenge from where you just linked.

 // statusArrBuilder, will use later to create change response in open status
  const statusArrBuilder = () => {
    if (hundredsUsed > 0) {
      let amount = 0;
      amount = hundredsUsed * 100;
      statusArr.push(["ONE HUNDRED", amount]);
    }
    if (twentiesUsed > 0) {
      let amount = 0;
      amount = twentiesUsed * 20;
      statusArr.push(["TWENTY", amount]);
    }
    if (tensUsed > 0) {
      let amount = 0;
      amount = tensUsed * 10;
      statusArr.push(["TEN", amount]);
    }
    if (fivesUsed > 0) {
      let amount = 0;
      amount = fivesUsed * 5;
      statusArr.push(["FIVE", amount]);
    }
    if (onesUsed > 0) {
      let amount = 0;
      amount = onesUsed;
      statusArr.push(["ONE", amount]);
    }
    if (quartersUsed > 0) {
      let amount = 0;
      amount = quartersUsed * 0.25;
      statusArr.push(["QUARTERS", amount]);
    }
    if (dimesUsed > 0) {
      let amount = 0;
      amount = dimesUsed * 0.1;
      statusArr.push(["DIME", amount]);
    }
    if (nickelsUsed > 0) {
      let amount = 0;
      amount = nickelsUsed * 0.05;
      statusArr.push(["NICKEL", amount]);
    }
    if (penniesUsed > 0) {
      let amount = 0;
      amount = penniesUsed * 0.01;
      statusArr.push(["PENNY", amount]);
    }
    console.log(statusArr);
    return statusArr;
  };

  // create class and constructor functions for later
  class Status {
    constructor(status, change) {
      this.status = status;
      this.change = change;
    }
  }

  // setting conditions for register status
  // if the code is left with more than a penny, then we will not get exact change and it will be insufficient funds
  if (roundedChange > 0.01) {
    const insufficientStatus = new Status("INSUFFICIENT_FUNDS", []);
    finalAnswer = insufficientStatus;
    // return insufficientStatus;
  }
  // for loop to get the maximum amount of change in the cid
  let total = 0;
  for (let i = 0; i < compareArr2.length; i++) {
    total += compareArr2[i][1];
  }
  // added a buffer of half a penny for exact change since rounding has been an issue throughout
  if (change < total + 0.005 && change > total - 0.005) {
    const closedStatus = new Status("CLOSED", compareArr2);
    finalAnswer = closedStatus;
    // return closedStatus;
  }
  // hardest part, need to make array show ONLY the values that were used in the compareArr to calculate the change from cid
  else {
    const openStatus = new Status("OPEN", statusArrBuilder());
    finalAnswer = openStatus;
    // return openStatus;
  }
  console.log(finalAnswer);
  return finalAnswer;
}

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],
]);
///////////////////////////////////////////////
// here is the obj that is returned 
{ status: 'OPEN',
  change: 
   [ [ 'TWENTY', 60 ],
     [ 'TEN', 20 ],
     [ 'FIVE', 15 ],
     [ 'ONE', 1 ],
     [ 'QUARTERS', 0.5 ],
     [ 'DIME', 0.2 ],
     [ 'PENNY', 0.04 ] ] }

chrome browser

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36

Challenge: JavaScript Algorithms and Data Structures Projects - Cash Register

Link to the challenge:

why is there a “class component” in there?!

care to explain that? thanks and happy learning :slight_smile:

yes, so I was thinking of a way to create an object based off of the cid amount conditions. So, if the conditions for “insufficient_funds” was met then an obj would be created with the status as the first arg and the change as the second, which would be the string and then an empty array in that case.

if it passed the condition for closed then it would return the string and an array I made earlier with the same values as cid

and for open return the string and pass in a function that builds the 2 level array with the correct amount of currency used

I was thinking this would be a good way to create an object that is flexible based on the conditions of the test. I am not aware if doing this specifically creates complications for returning said obj to pass the test.

The returned object must only have the properties requested and nothing else. Including more properties breaks the ability of the tests to see if you returned the right object.

okay, thank you that makes a lot of sense. I will fix the issues now

thank you for pointing out the class component. I saw afterwards that it was redundant to have. However, even after replacing it with just objects themselves, I still cannot return any of those outputs. JeremyLT hinted that I have more properties, but I do not see that. Do you see the cause of not being able to return anything? I can console.log it but it will not register a return. Also my chrome debugger will not work so I cannot go thru it step by step. Any pointers on where I should focus my attempts would be greatly appreciated

share your currently attempted code for this, thanks :slight_smile:

it is very long, probably too long, but a lot of this was trial and error. Therefore, I think the only way I can share the entire code is by linking to my repository on Github, if that is alright.

Also the “ideal change” part is not directly useful to the solution, but helped me understand as I went.

I commented around the blocks where I noticed the global return would stop working which is by my “cidCheck” variables I declared after the function they represent was created. You can find it directly above my statusArrBuilder() towards the bottom. Thank you for wanting to take a look.

lines 1686 and 1706 is where I found returning an object in the global scope would work vs would no longer work

could you please make a branch with only those codes that are in play? that way it becomes much more clearer to debug

  • thats is tooo many :grin:

Yeah, this problem should take at most a couple hundred lines, or as few as a couple dozen.

I tried removing the “ideal change” code that I thought did not matter, but it affected the change count in the end. I think I will just restart and try to focus on being much more concise and purposeful. I appreciate the advice and willingness to help!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.