Key JavaScript Concepts I Mastered During LeetCode’s 30-Day Challenge
Tags: javascript, coding, algorithms, leetcode
Published: 2023-10-26 15:48:27


![Photo by [AltumCode](https://unsplash.com/@altumcode?utm_source=medium&utm_medium=referral) on [Unsplash](https://unsplash.com?utm_source=medium&utm_medium=referral)](https://cdn-images-1.medium.com/max/7230/0*92evsvmUZXN8wjgK)

LeetCode is one of the most famous coding platforms and is considered to be the best for preparing for algorithms, data structures, interview preparation, and everything in between. Recently, they announced a [30-day JavaScript coding challenge](https://leetcode.com/discuss/study-guide/3458761/Open-to-Registration!-30-Days-of-LC-JavaScript-Challenge), which was entirely focused on solving problems using JavaScript concepts and constructs. The best part was their in-depth editorial for each problem, explaining everything down to the very minor detail.

For me personally, JavaScript is my go-to programming language for solving problems as I feel most comfortable with it. I have successfully solved over 150+ problems on LeetCode ([https://leetcode.com/ilatif/](https://leetcode.com/ilatif/)) before taking up this challenge, and almost all of them were in JavaScript. While I still have reservations about choosing JavaScript to solve problems in coding competitions and similar scenarios, which I will discuss in a separate article, it remains my preferred language. I have been using JavaScript for over a decade in my day job, and I am quite familiar with most of the concepts. However, there were still some concepts that I was missing, and we will be discussing them one by one

## Function Arity

**Challenge**: [https://leetcode.com/problems/curry/](https://leetcode.com/problems/curry/solution/)

Consider we’ve defined a function like below:

![](https://cdn-images-1.medium.com/max/2080/1*Iv0G0juMnsjF7U5-YK3fIA.png)

At runtime, how can we determine the number of arguments/parameters a function receives? While we can typically deduce this by examining the code, accessing this information during runtime becomes essential. So, what exactly is arity? I’m glad you asked. **Arity** refers to the number of arguments a function can accept. For instance, if a function accepts **three arguments**, we can say its **arity** is **3**. Accessing this information plays a crucial role in solving this LeetCode challenge

I thought that’s not possible, as even though we can define the number of arguments/parameters a function can accept, we can still pass more arguments/parameters to it, and JavaScript won’t complain. For example:

![](https://cdn-images-1.medium.com/max/2080/1*3buT9BETddcRkNwFj5SN9w.png)

This is perfectly valid code, and JavaScript won’t complain due to its dynamic nature. I based my solution on the misconception I mentioned above, namely, the belief that we cannot obtain the number of arguments/parameters a function can receive during runtime. It was only after I submitted my solution and opened the Editorial to review it that I realized it is indeed possible to determine a function’s arity at runtime.

![](https://cdn-images-1.medium.com/max/2080/1*ui3H5ym9cPMX1pdNsC1I7Q.png)

Every defined function has a length property that can be used to retrieve the arity of the function.

## 5 instanceof Number?

Challenge: [https://leetcode.com/problems/check-if-object-instance-of-class/](https://leetcode.com/problems/check-if-object-instance-of-class/solution/)

Consider following code.

![](https://cdn-images-1.medium.com/max/2080/1*LAk7NZJibUfpHk-KMV-SOQ.png)

What do you think the output would be? If your answer is true, then congratulations, your answer is incorrect. You might be wondering, how so? Essentially, instanceof in JavaScript determines whether an object is an instance of a particular class. It does this by checking if the object contains Class.prototype, and if so, it returns true. In the example above, 5 and name are primitive values, which means they are values themselves and not actual objects. When we use them in code, JavaScript temporarily coerces them to objects internally (a process known as [Boxing](https://javascript.plainenglish.io/javascript-boxing-wrappers-5b5ff9e5f6ab)). We are calling instanceof on them directly, and since they are not objects at that time, instanceof would return false.

To obtain the correct results, we can utilize the Object constructor around primitive values, which will convert them to objects of Numberand String, respectively

![](https://cdn-images-1.medium.com/max/2132/1*eOapFox6e21unsFmvSgszg.png)

## typeof Person === “class”?

Challenge: [https://leetcode.com/problems/check-if-object-instance-of-class/](https://leetcode.com/problems/check-if-object-instance-of-class/solution/)

Consider the following code. What do you think output of typeof Person would be?

![](https://cdn-images-1.medium.com/max/2080/1*9DElZIxE8teU8nlSPfDSzw.png)

[Classes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes) were introduced in ES6, providing a convenient syntax for declaring properties and methods. Initially, when I encountered typeof Person, I had the notion that it would output class despite being aware that classes are internally converted to function syntax by the JavaScript compiler (something similar to the example below)."

![Transpiled with TypeScript Playground](https://cdn-images-1.medium.com/max/2132/1*HMH-DlqV0jR1kaEAiJahhw.png)

So the correct result for typeof Person is function. It serves as a gentle reminder that it's perfectly normal to overlook minor details at times.

## Generator functions

Challenge: [https://leetcode.com/problems/generate-fibonacci-sequence](https://leetcode.com/problems/generate-fibonacci-sequence)

Generator functions allow the execution of a function to pause and resume based on how it’s being called. The basic syntax of a generator function looks like this:

![](https://cdn-images-1.medium.com/max/2080/1*EAGhrsVHV9wOi0Kfq28xfw.png)

The * after the function keyword is what declares a generator function. yield is used to return a value from the generator function and pauses the execution until the next invocation.

I had a vague understanding of generator functions: 1) that they can only yield a single value and 2) that they are always (re)executed from the beginning. Additionally, since I had never used them in production code, I had no chance to improve or refine my knowledge about them. Fortunately, this JavaScript challenge provided me with the opportunity to delve deep into generators and expand my understanding.

Consider the following example:

![](https://cdn-images-1.medium.com/max/2080/1*ZEYurznlHwccnhzCDksmOQ.png)

This generator function yields three values, and in order to obtain all of them, we need to call the function three times. Each function invocation will resume from where the last yield statement left off and continue executing statements until it encounters the next yield statement. At that point, it will pause execution and return the generated output. To explore generators in detail, please refer to [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Generator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Generator).

## The best challenge?

For me, the most exciting challenge throughout the entire 30-day challenge was the [19th-day challenge](https://leetcode.com/problems/array-of-objects-to-matrix/). It tasked us with converting an array of objects into a matrix. In my opinion, it is the ideal problem-solving question for JavaScript developers. It tests your capability to comprehend a problem statement, construct a mental model around it, and then implement a solution using JavaScript constructs. It doesn’t involve any fancy data structures or algorithms, just pure JavaScript skills are required. This makes it a perfect question to ask in JavaScript interviews.

## Bonus

I have open-sourced all of my submissions/solutions to the challenge. You can find them at [https://github.com/ilatif/gravity](https://github.com/ilatif/gravity).

It has been a fun and exciting experience to participate in [LeetCode’s 30-day JavaScript Challenge](https://leetcode.com/discuss/study-guide/3458761/Open-to-Registration!-30-Days-of-LC-JavaScript-Challenge). I have learned a lot from it. Feel free to share your thoughts and let me know in the comments how useful you found this article. Thank you!