4

I found a simple code example in a book teaching programming concepts:

let result = 1;
let counter = 0;
while (counter < 10) {
result = result * 2;
counter = counter + 1;
}
console.log(result);
// 1024

...I can't believe I didn't get it right the first time. Yes, the result is 1024 and not 20. For it to result in 20, the statement would have to be:
result = result * counter.

Upon reflection I concluded: An exponent is a multiplication by a multiplication, so... it exponentially grows.

So I ask: how do you develop a second nature for these kind of problems? You studied it? You studied and practiced it? You're awesome at mathematics? It annoys me that my intuition often wrongs me when it comes to mathematics.

Comments
  • 3
    Hopefully the next example explains why this example should've used a for loop.
  • 0
    Where is the exponent? The result is just repeatedly multiplied by 2.
    Not exactly complicated math.
  • 1
    I also sometimes stumble upon seemingly very simple things. In such cases I usually open a scratchpad that lets me see values on the fly and start playing around either until I fully understand what's up or until I stop caring (so that I have to redo the process the next time).

    But yeah, I get your point. I do develop some sense of those things with enough practice. When I was starting out, I was almost afraid of for example map and reduce, now I like them quite a lot because they let me do things with single expressions instead of with explicit loops.
  • 0
    @Lensflare I'm not great at math, so. It is also generally known that an exponent is repeated multiplication. I just looked it up again.
  • 1
    Thanks to code I can avoid some math :p I'm pretty sure that some things I've written are nicely translatable to some nice math formula.

    Altough, I do not consider it a big failure to have above thing wrong. Can happen, don't punish yourself so hard.
  • 0
    @kamen Yes, the seemingly very simple things can get annoying. For me a scratchpad with live values works best as well. It's like a computer program; if you only output values and then do nothing with them, the program forgets them.

    For me it's enough practice as well as a deep dive in a strong foundational theoretical background which really helps put things together. map and reduce intimidated me as well a few years ago but now they're a bit more understandable. Analogies help as well. I noticed during my internship that my code got much cleaner after reducing all the loops to single statements. :)
  • 0
    @CaptainRant ah, I see what you mean now. 10 is the exponent.
    But imo the example is just needlessly confusing.
    I don’t see what the point is.
    If the point is to explain iterative processes via loops then the point could have been made easier by summing some numbers.

    On a slightly different note, using loops for that kind of stuff is didactically bad.
    The typical beginner stuff is always explained in loops in a very unnatural way where it should really be a functional style map or something.
    And dev rookies remember that kind of crap and build the craziest shit with loops because they feel like it’s how it should be, but it’s not.
  • 0
    @Lensflare In the chapter of conditional constructs, the author wanted to illustrate a program that would do something useful vs. just outputting a value. It also has to do with the concept of a program's environment (bound variables), illustrating that the variable outside the loop is aware of itself inside the conditional expression as well as outside of it. He used this 'convoluted' example as a teaser for a later chapter where he explains things more in-depth.

    And of course, a good programmer knows to use the right tools for the right scenarios and not just blindly use loops.. that's.. for true beginners.
  • 0
    This is certainly bad code, so it's ok to be tricked by it (it's way too imperative, it should just be 2 ** 10 [or Math.pow(2, 10) if you're old school]) but getting a feel for compounding effects inside a loop happens naturally. Here's how it happened for me:

    - Write a loop in college

    - Run the code

    - Nothing happens, my laptop fans go to 100%

    - "Fuck, loops are dangerous!"

    (Repeat until you get it through your exceptionally thick skull, in my case)
  • 0
    I think even in general, your average programmer has been stung by the power of looping more than 100 times in their career so whenever a programmer sees a loop they're already on high alert - thinking about bounds, stack overflow, locking up the UI/ main thread, etc.

    Loops are one of the most powerful programming concepts, so programmers (excluding front-end developers who know nothing except declarative bindings in react/angular) so after a while you just get a spidey sense for it.
  • 2
    > 'Yes, the result is 1024 and not 20. For it to result in 20, the statement would have to be:

    result = result * counter.'

    Something that is bugging me... Considering the example

    ```

    let result = 1;

    let counter = 0;

    while (counter < 10) {

    result = result * 2;

    counter = counter + 1;

    }

    ```

    && `result = result * counter`... wouldn't you end up multiplying by zero, thus ending up w/ `result == 0;`?
  • 1
    @D-4got10-01 Another reason this code is so crap (along with most educational code). The order of operations is not as solid as it could be here, for exactly the same reason you've stated. In fact, I don't usually like to start counter variables with 0 unless they're used exclusively for indexing work. In the case where they're a literal numerical counter, start them at 1!
  • 1
    Dunno, this stuff is just intuitive to me. The moment you ask me what "result" is my eyes immediatelly just snap to all references to "result" and the first thing I notice is result = result*2

    So I immediatelly understand that the result is some sort of X * 2^N... I don't know the result until I analyze rest or the code and initial conditions but I know straight up it aint gonna be 20 when the loop has more than one iterations.

    And I was never really good at math, but I'm naturally good at logic and was good at it in uni too.

    Not sure that helps you though
Add Comment