# Most Important Interview Questions For JavaScript Engineer

#Find Largest Number In Array

Array is a collection of data where we can store a lots of data together with out any problem. If we want to understand how to get largest number in an Array then we should write a code. So, the code example is

Code: var numbers = [4, 7, 2, 34, 24, 113, 50, 43];

var largest = nums[0];

for(var i = 0; i < nums.length; i++){

var element = nums[i];

if(element > largest){

largest = element;

}

}

console.log(“largest is”, largest);

In the above code we see that we have an array of numbers. Initially we take zero index as largest number . Under for loop we check a condition that we get a index data from for loop if that data is bigger than our supposed data then our data will be largest data otherwise it will be small.

#Find Summation Of All Numbers In Array

If we want to find summation of all numbers in an array then we have to declare an array of sum numbers. After that initially we have to take a variable which will be contain initial summation of all numbers is zero. Then we have to use for loop for the whole process like the below code.

Code: var numbers=[1, 2, 3, 4, 5];

var sum=0;

for(var i=0; i<numbers.length; i++){

sum=sum+numbers[i];

}

console.log(sum);

#Remove Duplicate Item From An Array

We Can Also easily reduce duplicate numbers from an array. For this we have to take an array of numbers. Initially we have to declare a variable with empty array where we can store original data without duplicate. Below example will be best for understand it more.

Code: var num=[2,3,4,2,5,3,8,10];

var uniqueName=[];

for(var i=0; i<num.length; i++){

var element=num[i];

var index=uniqueName.indexOf(element);

if(index == -1) {

uniqueName.push(element);

}

}

console.log(uniqueName);

If we can not get any match on the unique name the index will be -1 and if it is true then we can push number into unique name otherwise not.

#FInd Number Of Words From String

If we look at our code example we can find easily how the code count words from string .

Code: var strg=”I am a good boy. You are not good boy.”;

var count= 0;

for (var i=0;i<strg.length;i++) {

var element=strg[i];

if(element==” “ && strg[i-1] !== “ “) {

count++;

}

}

count++;

console.log(count);

#Reverse A string

Reverse string is a very easy question for all the candidates. If we use a function for that code then we have to send some strings. We know string is not an array but like array. Default value of a string is empty string. By using for loop we can easily find a reverse string. Code below.

Code: function stringRev(str){

var Reverse=” ”;

for(var i=0;i<str.length;i++) {

var char=str[i];

Reverse=char + Reverse

}

return Reverse;

}

var string=”I am a good boy.”;

var result=stringRev(string);

console.log(result);

#Recursive Fibonacci Element

If add first two number and get the result same as the third number then its called fibonacci number. Suppose we take first number as 0 and second number as 1 then next fibonacci number will be 0+1 = 1. Below code example will be helpful for better understanding.

Code: function fibonacci(n){

if(n==0) {

return 0;

} else if(n==1) {

return 1;

} else {

return fibonacci(n-1)+fibonacci(n-2);

}

}

var result=fibonacci(10);

console.log(result);

#Find Leap Year

If we divide any year by 400 and get output as 0 then the year should call leap year. But Just divided by 400 will not give us a real result. For the actually result we have divide a year with 400 and 4 . If a year divide by both of the numbers then then year will be called as a leap year. One last thing year should not be divided by 100. Bellow code will help us for better understanding.

Code: function leapYear(year){

if(year%400==0) {

return true;

} else if(year%4==0 && year%100!==0) {

return true;

} else {

return false;

}

}

var year2013=leapYear(2013);

console.log(year2013);

#Global And Local Variable

There are lots of variables which we create for our programs. But the are 2 variables which we know and one is Global variable and second one is Local variable. In global variable we have to declare it out of the function cause this type of variable we use for anywhere when needed. But global variable is less used than Local variable. In Programming we use local variable most. Which is decleard in a function for special use.

#Bind Method

Bind method is the method where we can create our own method in existing object. By creating our own method we can do whatever we want. Basically it creates its won function for the better work. We can use bind method in any object.

#Prime Number

If Numbers which is not divided by from 2 to before that number is called prime number. If any number divided by 2 and its before number it will not be a prime number. Bellow code example will be more understanding for all .

Code: function isPrime(num){

for(var i=2;i<num;i++) {

if(num%i==0) {

return ‘Not a prime number’;

}

}

return ‘A prime number’;

}

var result=isPrime(10);

console.log(result);