When you write code and forget to comment it, and then you come back and try to figure out why the heck you wrote certain parts of it.

Let this be a lesson for future me.

  • 7
    A humbling but valuable lesson :)
  • 1
    Just a guess, but...

    I suppose 'index' is initially 0 (or equivalent). You increment it at the start of the loop, instead of at the end.

    This id like 1-based indexing, so sumOfMonths[index - 1] will return the current month, which means:

    sumOfDays == sumOfMonths[index - 1]

    You're interested in the previous month, so you have to get the previous index and subtract 1 again. Hence sumOfMonths[index - 2]
  • 0
    Next time use map and filter instead of a for loop and if
  • 0
    @Chlodovechus I don't think that makes it less confusing to come back to...
  • 0
    @stacked @Voxera @Chlodovechus @IcyTv

    This is the module where this screenshot is from:


    Our national identity card numbers are formatted in a way where you can calculate the date of birth and the gender.
  • 1
    @monkehparade the -2 is because you only subtract up to the sum of the previous month, not the current one and since the loop starts with index++ it will be one ahead of the current month when in the loop.

    But you should clean up the if ... return true, false and only return the condition of the if ;)

    Also, the month sum seems to be for a leap year but I could not see any handling of non leap year?
  • 1
    @Voxera I, for some reason, keep coming back to this rant to see your comment.
Add Comment