DEV Community

Cover image for Road to Genius: advanced #41
Ilya Nevolin
Ilya Nevolin

Posted on

Road to Genius: advanced #41

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.

let S = 0;
for (let i = 0; i < 150; iπŸ’§) {
  let h = Math.floor(i / πŸ’Ž);
  if (h > 0)
    S += i % h;
}

// πŸ’§ = ? (operator)
// πŸ’Ž = ? (number)
// such that S = 73 (number)
Enter fullscreen mode Exit fullscreen mode

Here's a pretty short code for an advanced challenge, let's have a look. The core of the code is a for-loop that runs from 0 to 150 (excluded), and then depending on the if-condition it increases S's value.

We won't have to fully analyze the code since fixing the two required bugs is pretty simple.

The first one πŸ’§ has to be the ++ operator, to ensure the for-loop works correctly.

The second bug πŸ’Ž is quite tricky, you can't determine its value in an easy way, it could be any number. Either you reverse engineer the code to ensure that S = 73 at the end, or take a peek at the possible answers for πŸ’Ž: 0, 2 and 150. Dividing i by 0 or 150 will never yield a value larger than 0, so the if-condition is never going to trigger. The only choice left is 2:

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. Get started and become a certified Codr today at https://nevolin.be/codr/

Latest comments (0)