DEV Community 👩‍💻👨‍💻

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

Posted on

Road to Genius: smart #20

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

Even though this challenge contains quite a lot of code, it's pretty easy to solve. We only need to fix the bugs 💧 and ☃️, so we don't even have to fully analyze & understand the code to complete it.

The first bug 💧 is easy since it's just the initial declaration of a variable, you can quickly skim through the code and you'll find that profit is the only variable that hasn't been declared yet, so that must be it.

The last bug ☃️ appears to be a function call, because it provides an argument arr, and the only function here is maxProfit.

coding challenge answer

Now that we've solved it, let's take a moment to analyze what the code is doing. It starts with an array arr, and then calls maxProfit(arr). If you look closely at the for-loop, it starts from index 1 (instead of 0), then the if-statement checks whether the previous value from the array is smaller than the current value; if it is then: profit = MathMax(profit, price - min). If it's not then min = MathMin(min, price). In a nutshell its computing the profit with some specific requirements that we don't know (but can analyze).


Join me on the Road to Genius and upgrade your programming skills, at https://nevolin.be/codr/

Top comments (0)

In defense of the modern web

I expect I'll annoy everyone with this post: the anti-JavaScript crusaders, justly aghast at how much of the stuff we slather onto modern websites; the people arguing the web is a broken platform for interactive applications anyway and we should start over;

React users; the old guard with their artisanal JS and hand authored HTML; and Tom MacWright, someone I've admired from afar since I first became aware of his work on Mapbox many years ago. But I guess that's the price of having opinions.