Are you frightened about problem-solving? Here are some problems to upheave your preparation for the next interview.

alFuad
4 min readNov 5, 2020

--

Photo by Tim Gouw on Unsplash

Calculating factorial using for loop, while loop and in a recursive way:

Creating Fibonacci series using for loop, while loop and in a recursive way

Checking whether a number is Prime or not

Finding the largest element in an array

Reversing a string:

function reverseString(str) {
return str.split("").reverse().join("");
}
console.log(reverseString("I am a good boy")); //"yob doog a ma I"

Counting words in a string:

Removing duplicate elements from an array:

Some special cases:

  1. Null vs Undefined

So this is one of the most confusing parts for freshers. What is null? If you check its type, you will be surprised to see that it is object. However null is a primitive value. Undefined means declaring a variable without assigning a value to it. You can get undefined in some cases. 1. When you implicitly return a function without a return statement, 2. setting undefined to something as value. 3. when you don’t pass parameters to function etc.

2. == vs ===

== don’t check types while ===(strict equality) do so. see the example below:

1. "1" == 1 //true but "1" === 1 //false
2. 0 == false //true but 0 === false //false

3. How Javascript works asynchronously

Here is an in-depth explanation of how JS works under the hood. You can check out this.

4. map(), filter(), find()

Are you a little bit confused about these three array method? These are very powerful stuff. If you have still bewilderment about these, now it’s time to accept them diaphanously, let’s have a look at the following example:

map() and filter() return the result as an array after passing the condition, but there is a slight difference between this. map() takes an array as argument, iterates over the array, filter() apply condition over the array it takes, if the condition is true, the elements are added to the resulting array. find() method only returns the first element of the array it takes which passes the applied condition.

5. Global variable vs Window variable

Simply global variable is accessible anywhere in your program. In the case of a JavaScript runtime environment like node.js, it is called global. But it is called window in the browser. You will understand this clearly by the following examples I have used to explain lexical scoping.

6. “new” keyword

The new keyword in javascript is used to create an object.

function Person(name, age, profession) {
this.name = name;
this.age = age;
this.profession = profession;
}
const personObj = new Person('John', 30, "Frontend Developer");console.log(personObj.profession);

But you can not use it in case of ES6 fat arrow function, otherwise, you will get a type error:

function foo(a, b) {
var add = a + b;
console.log(add);
}
new foo(1, 2);
-----const bar = (a, b) => {
const add = a + b;
console.log(add);
};
new bar(1, 2); //TypeError: bar is not a constructor

7. bind() vs call() vs apply()

At the very beginning let’s have a look at the use cases of these three methods:

bind():

call():

apply():

We clearly see that in the case of bind() method, it returns another function definition instead of returning our expected result. Next, we have assigned this function to another variable and called that. In this way, we can use that function as much as we wish and anywhere.

In the case of call() method, that function with which you are using call() method, is immediately called() as same as apply() method but the only difference between call() and apply() is call() takes arguments as comma-separated value and apply() takes arguments as an array. As I have shown in my example that if you pass arguments without an array, it will throw the TypeError.

call ====> comma-separated

apply ====> array

8. Lexical Scoping

This is one of the most confusing stuff beginners have to face. Before starting, let’s see the example code below:

(i)arrow function + regular function === global + global

(ii)arrow function + arrow function === global + global

(iii)regular function + arrow function === local + local

(iv)regular function + regular function === local + global

So basically the incidents that happened in my above examples(also used in bind, call, apply method) have been summarised in four points. This is because of the lexical scoping of “this” keyword. In browser global is called window. That means if you run the above code in the browser console, you will see setInterval, setTimeout, etc of global objects. So what do we understand from this example? “this” keyword is mostly dependent on where and how you use it. Now generally the question arises whether we can use call, bind, and apply method in this case. Let’s see:

We see that call is unable to set the value of “this” for lexical scoping. The same thing occurs in the case of bind, apply. “this” will always search its nearest value. Here there is no value for “this” keyword, so it is indicating global object.

last but not least, here is another example to make a beginner confused enough:

Well, here we see that the setTimeout() is a global method. So, this is unable to read user1 object. We can overcome that problem by using bind, call or apply method which we have learned already. But we have a smart arrow function by which we can safely slacken setTimeout().

--

--

alFuad
0 Followers

extremely passionate about web technology and innovation