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.
Great, another easy challenge, with only five lines of code and one bug (🚀) to fix. To solve this challenge we don't need to invest much of our intellectual energy. If you look closely at the buggy line, you'll see that the bug🚀 represents some kind of object that has a push(...)
function available. From experience we know that push
and pop
functions are usually associated with arrays, and the only array in our code is arr
, so 🚀 is arr
.
Even if you manage to solve a challenge without fully reading/understanding the code, it never hurts to take a closer look afterwards.
This code adds numbers into arr
, and finally R
gets the value from arr
at index N-2
. Apparently this value is 6 because it is given, but you can also manually compute it. One way is by understanding what the for-loop is doing: it's pushing numbers into arr
starting from 0 up to 7 (included), so in total it has added 8 numbers to the array (= N
). This means that index 7 is the last item of the array (=N-1
), thus index N-2
holds the value of 6.
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)