Getting lost in complexity/order of ejecution. Don´t know what to do...!

I´m working on the cash register algorithm challenge. And I´m still stuck in the same problem that i was 2 days ago. I build a for loop with some conditionals inside to make it work, but somewhere inside the conditionals, I get irremediably lost and I don´t know how to make it work.

I have tried to erase it and build it different ways but I always come up with the same approach and I can´t get my head around how to “debug” this:

…note: “fromthere” means from where it should start counting backwards. Don´t worry about this.

identicalObject = [["PENNY", 0],["NICKEL", 0],["DIME", 0],["QUARTER", 0],["ONE", 0],["FIVE", 0],["TEN", 0],["TWENTY", 0],["ONE HUNDRED", 0]]
    // toReturn = 96.74
    var mustbethesame = 0
  var typeofCurrency = [0.01,0.05,0.1,0.25,1,5,10,20,100]
  //            cid = [1.05,2.05,3.1,4.25,90,55,20,60,100]


for (var i=fromthere; i > -1; i--){
       if (typeofCurrency[i] > toReturn){
         1==1 //continue to the next one
       }
       else {
         let counter = 0;
         counter = cid[i][1] / typeofCurrency[i]

         for (var j=0; j<counter;j++){
          mustbethesame += typeofCurrency[i]
          if (mustbethesame < toReturn){
            identicalObject[i][1] += typeofCurrency[i]
          }
          else {
            1==1 // continue to the next one
          }
         }

       }
     }

Basically, apart from an empty array what the for loop will be filling with values, I create an empty variable which its supposed to serve as “blocker”. If this variable gets bigger than the value to return, then the loop doesn´t add the last value and go to the next currency (or at least its what it supposed to do)

Here´s what it returns:

console.log(identicalObject) // returns

[ 'PENNY', 0 ],
  [ 'NICKEL', 0 ],
  [ 'DIME', 0 ],
  [ 'QUARTER', 0 ],
  [ 'ONE', 0 ],
  [ 'FIVE', 15 ],
  [ 'TEN', 20 ],
  [ 'TWENTY', 60 ],
  [ 'ONE HUNDRED', 0 ]

When it should return:

[["TWENTY", 60], ["TEN", 20], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.5], ["DIME", 0.2], ["PENNY", 0.04]]

So for a strange reason it does well until it reaches below “FIVE”, in that it just stop and doesn´t do “ONE”, “QUARTER”…etc :roll_eyes:

For me, at least, I want to dive in and code, and in doing so, I frequently miss something critical to actually making it work. It sounds like you’ve got a similar mindset. So, you’re probably going to be frustrated with my answer below, but know it comes from personal experience and genuinely wanting to help you.

First

When my code has such poor variable naming, inconsistent spacing, and hacky code (e.g. 1==1) as what you’ve posted, it’s a huge red flag that I really need to take a big step back, walk away for a bit (an hour to a couple of days is normal for me), and then —when my brain has let go of the current approach, or I realize I hadn’t accounted for x— then I know it’s time to revisit the problem.

And yes, I sometimes have to do that multiple times for the same problem.

Second

For many reasons, you should provide a fully replicable version of your code.

When I come here to help others, I benefit from having all the code. For example, I can assume what you’ve set fromthere, toReturn, etc to, but I can’t verify it.

I also can’t run your code to replicate your results or tweak your code.

And as someone trying to help, I personally try to provide explanatory comments inline and/or working examples that start from your actual code (e.g. 0, 1 2, 3, 4) but if you don’t provide all of your code, ideally in an easily editable and sharable format (e.g. https://repl.it, https://codepen.io/, https://jsbin.com/, https://jsfiddle.net/, http://plnkr.co/, https://codesandbox.io, etc.) then I simply can’t help you that way.

Third

I’ve also learned that poor variable names, poor spacing, hacks, etc. make it hard for other people to read my code, and therefore they’re less likely to look at my code, and therefore they’re less likely to give me meaningful feedback.

As a result, I make a point to clean up my code before posting it with a request for help.

Obviously, this makes it more likely that I’ll get help, but it frequently helps me actually understand the problem in a different way and I solve the problem on my own, or it helps me find silly typos and again I solve the problem on my own.


So, I strongly suggest, that you (a) refactor your code with variable names that would make sense to a friend or family member who doesn’t know how to code, (b) clean up your indentions, © add pseudo-code comments explaining different branches of logic, and (d) then repost your request for help.

1 Like

Hello @Onpointiscake,

I’d like to echo @metasean points. We really do need to see all of the code instead of this snippet. One thing I noticed is your use of 1=1 to skip the loop. That won’t do anything. I think you are looking for a continue or break statement.

Cheers,
Austin