DEV Community

Cover image for Road to Genius: smart #22
Ilya Nevolin
Ilya Nevolin

Posted on

Road to Genius: smart #22

Each day I solve several coding 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, and you will learn a ton of new and interesting things as you go.

coding challenge

I solved two super easy challenges which are not worth mentioning, but then this one is a game changer. Quite a lot of code, but as you may know by now, whenever we have to fix bugs it's not necessary to fully understand the code in order to complete the challenge.

We need to fix the bugs 💰 and 🍎, don't be scared of the amount of complex code, it's easier than it looks. The first bug needs to be an operator, the line of code is:
if (res 💰 MAX_INTERGER - 1 || res < -1 * MAX_INTERGER)
Fortunately for us, the variable names used in this line of code reveal the nature of the code. First off, the variable res is usually shorthand for result, response or in this case residue (aka remainder). If we simplify the code above it's actually doing the following:
if (remainder 💰 VERY_LARGE or remainder < VERY_SMALL)
Since the second operator is <, the first one is likely going to be > or >=, one of these will be mathematically more correct, but that's beyond the scope of this post right now.

The second bug 🍎 is the declaration of a variable, to fix it we need to find some variable that hasn't been declared before, and that is acc (the accumulator).

coding challenge answer

By solving these challenges you train yourself to be a better programmer. You'll learn newer and better ways of analyzing, debugging and improving code. As a result you'll be more productive and valuable in business. Join me on the Road to Genius and upgrade your programming skills, at https://nevolin.be/codr/

Oldest comments (0)