DEV Community

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

Posted on

1

Road to Genius: beginner #4

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 a pretty interesting challenge. One thing we haven't covered until now are JavaScript objects. An object is a "thing" that can hold different variables internally. To illustrate this, say we have an object called "Animal", an animal can have a color, height and weight. In JavaScript code our Animal object would look like this:

animal = {
  color: 'black',
  height: 50,
  weight: 100
}
Enter fullscreen mode Exit fullscreen mode

We can access and change an object's internal elements (= members) like this: animal.height = 70. We can also create and remove members from objects, let's add a type member to our animal like this: animal['type'] = 'panther'.

Now back to our challenge, we have to fix two bugs (💰 and 💧) in the code, such that R yields 3. You don't have to start executing the code to solve the challenge, just a little bit of insight is enough here.

One of the buggy lines is: if (💧 in mp == false), basically it checks whether the identifier 💧 (which is most likely a variable) is not inside our object mp. In other words, it checks if mp is missing the member represented by 💧. Right now we can't say for certain what 💧 should be, until we investigate the next line.

The next buggy line is 💰[s] = 0;, which sets the s index in 💰 to zero. If you have a little bit of programming experience, you should realize that these two lines are very related. The identifier 💰 should be mp, while 💧 should be s.

programming challenge answer

You may ask, but why? The answer requires you to fully understand the code. We use the object mp to keep track of occurrences of characters in the string S, which in this case is 010101. All that the code does is iterate over each character in this string, and count how many times each character occurs. After running this code, the object will look like this:

mp = {
   '0': 3, 
   '1': 3,
}
Enter fullscreen mode Exit fullscreen mode

Each of the characters occurs 3 times in the string S. That's why R == mp[0] == 3.


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

SurveyJS custom survey software

Simplify data collection in your JS app with a fully integrated form management platform. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more. Integrates with any backend system, giving you full control over your data and no user limits.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay