### Javascipt Interview Question and Answers - 2

In this article, I am going to take 3 question which are frequently asked in Javascript interviews. These questions are somewhat linked with the concept of reversing string. These are asked to check the logical ability and approach.
Give someone a program, you frustrate them for a day; teach them how to program, you frustrate them for a lifetime.
Q1. Write a program to reverse the string/word.e.g. if input is 'india', the output should be 'aidni'.
Condition is, the solution should be with minimal lines of code.

So pause for a moment, think about it and them reach out to the solution.

To solve this problem, we need to think, how we can reverse the string, do we have any in built function to reverse a string? Or should we loop through each character of string from last and assign to new string? Or any other approach?

We don't have any built-in function like reverse for a string but we do have for array, Can we make a string to array? Yes we can by splitting the string using split(''). So the problem solved.
This solution should work

1.    function reverseString(str) {
2.         return str.split('').reverse().join('');
3.    }

We can also write this solution using ECMAS 6 arrow function;

var reverseString = (str) => str.split('').reverse().join('')

Boom!!

Now lets move to second problem
Q. Write a program to check if the given string is palindrome or not.

For this we just need to modify and add one condition in our last program.i.e. test if reverse string and input string are same or not

var isPalindrome = (str) => str === str.split('').reverse().join('')

That's it we got the solution.

Let's move to more challenging problem

Q. Write a program to find the largest palindrome and its length in a given string.
Note: There can be more than one palindrome words in a string, we need the largest one.
e.g. str = 'ahanahaikmalayalamkkm' in this we have palindromes like malayalam, ahanaha, kmalayalamk, etc but largest is kmalayalamk.

We can actually need our last solution as a part of this solution.
Here I have tried to solve it using Dynamic programing.

var obj = {
largest : 0,
largestPalindrome: ''

}
function largestPalindrome(fullStr){
if(isPalindrome(fullStr)){ // use isPalindrome we wrote
if(fullStr.length > obj.largest){
obj.largest = fullStr.length;
obj.largestPalindrome = fullStr;
}
} else {
largestPalindrome(fullStr.substr(0,fullStr.length - 1));
largestPalindrome(fullStr.substr(1,fullStr.length));
}
}

Try converting this function using arrow function.

Thank you!!

### Javascript Hoisting Interview Question and Answers - 5

In this post,I have tried to cover all the types of possible interview questions and answers on hoisting. If you are not familiar with the hoisting, I would highly recommend you to go to my post on hoisting . Once you are comfortable with the topic, you can attempt these questions. Understanding the type of question will help you to answer. I have provided the hint after each question. All the answers are given at the end of the post. //Question 1 console.log('bar:', bar) bar = 15 var foo = 1 console.log(foo, bar) var bar Hint : Basics of hoisting //Question 2 var foo = 5 console.log('foo:', foo) var foo; var bar = 10; var bar; console.log('bar:', bar) var baz = 10 var baz = 12 console.log('baz:', baz) Hint : Basics of hoisting     //Question 3 function foo() { function bar() { return 5 } return bar() function bar() { return 10 } } console.log(foo()); Hi

### Javascript Closure Interview Questions and Answers - 6

Here in the post, I have posted all the possible interview questions and answers on closure. If you are not familiar with the closure , I would highly recommend you to go to my post on   closure . Once you are comfortable with the topic, you can attempt these questions. All the answers are given at the end of the post. //Question 1 function foo() { var a = 5 function bar() { console.log(a) } return bar; } var baz = foo() baz()   // Question 2 function outer(a) { var b = 2 function inner() { var c = 5 console.log(a * b * c) } return inner; } var multiply = outer(5) multiply() // Question 3 const arr = [10, 20, 30, 40, 50] for (var i = 0; i < arr.length; i++) { setTimeout(function() { console.log('Index: ' + i + ', element: ' + arr[i]) }, 1000) } // Question 4 for (var i = 0; i < 5; i++) { setTimeout(function(i) { return function() {

### Come Closure in Javascript

What is a closure? If we want to derive a definition,we can say, A closure is a feature of Javascript where inner function has access to the outer (enclosing) function’s variables—scope chain. This scope chains means inner function has: access to its own scope (variables defined between its curly brackets and its parameters), access to the outer function’s variables and parameters,  access to the global variables. Now we will look into it in detail. Consider following example var globalVar = "xyz"; function outerFunc(outerArg) { var outerVar = 'Alex'; function innerFunc(innerArg) { var innerVar = 'Chris'; console.log("globalVar = " + globalVar); console.log("outerArg = " + outerArg); console.log("innerArg = " + innerArg); console.log("outerVar = " + outerVar); console.log("innerVar = " + innerVar); } innerFunc(456); } outerFunc(12