Ad
  • Default User Avatar

    Welcome to CodeWars!
    I hope your time here is enlightening.

    That's quite a piece of code you have there - it looks like it took some time to write and I commend you for keeping track of all the variables whilst writing it.
    (Well done for remembering to allocate and free memory for the array of remainders too.)

    I have a few points of advice for you to bear in mind for future coding... hope they help:

    1. The remainders (1, 10, 9, 12, 3, 4) were given in the problem text. They're always the same for every application of thirt, so there's no need to calculate them every time - you can store the literal values as constants.
    2. One of the principles of writing good code is DRY - don't repeat yourself. If you find that you're repeating yourself (or even copy-pasting your own code), ask yourself "do I really need to use the same code again?" If you really do, then it's usually best to write a seperate "helper" function to do the work. It's less typing, less code to change if things go wrong and usually much clearer.
    3. Variable names should usually be as descriptive as possible. Generic names like new_number can be fine (I'd be a hypocrite if I said never to use them!), but if your code is more than about thirty lines, generic names make it hard to keep track of what each variable is supposed to be.
    4. Another help in long code is comments. Even a one line explanation of what each loop is doing can clarify code enormously.

    If any of this seems abstract, take a look at the solutions of other code warriors.
    Ask yourself "If I needed to modify someone else's code to solve a slightly different problem, which code would make it easiest?"
    Then try to write similar code yourself.

    Practice makes perfect :)