DEV Community

Cover image for Road to Genius: beginner #14
Ilya Nevolin
Ilya Nevolin

Posted on

Road to Genius: beginner #14

Each day I solve several challenges and puzzles from Codr's ranked mode. The goal is to reach genius rank, along the way I explain how I solve them. You do not need any programming background to get started, but you will learn a ton of new and interesting things as you go.

programming challenge

This is the last challenge before we reach the next rank, and it's a pretty nice one. We only need to fix one bug ๐ŸŽ to complete the challenge.

The code starts with a function div that takes two parameters dividend and divisor, this already reveals the nature of the function at hand. The buggy line is acc += ๐ŸŽ;, basically we need to figure out which value/variable is used for the summation. This can either be dividend, divisor, count or acc itself. By looking at the while-loop's condition it is (dividend - acc >= 0). From this we can conclude that acc needs to stay below dividend's value, so we can rule out dividend as the answer. Variable count seems to be used for counting the number of times that divisor fits into dividend, so let's rule it out as well.

Let's see what if we use acc as ๐ŸŽ, starting with value 3 (=divisor), it will evolve as: 3 -> 6 -> 12 -> 24. It's the same as writing acc *= 2. In this case count will only reach value 2 (not 3 as required by the challenge: A=3).

On the other hand, if we use divisor for ๐ŸŽ, it will evolve as: 3 -> 6 -> 9 -> 12. In this case the while-loop will end once acc becomes 9, and count will be 3 (thus A=3).

programming challenge answer


If you feel inspired and motivated to upgrade your coding + debugging skills, join me on the Road to Genius at https://nevolin.be/codr/

Top comments (0)