10 Most Important JS Questions and Answers.

Md.Saiful Islam
4 min readMay 13, 2020

Today We will talk about 10 most important Js interview questions.

Let’s start —

  1. How would you verify a prime number?

Answer:

A number is called prime if it has only two divisors — 1 and itself.To check it, we won’t need to check all number till the given number rather than we can check the sqart(n) number.that’s enough.Confused?? Let’s see how it works.

function isPrime( number ) {const max = Math.ceil( Math.sqrt(number) );for (let i = 2; i <= max; i++) {   if (number % i === 0) {      return false;   } }return true;
}
console.log(isPrime(7));// output: true

2. How would you find the greatest common divisor of two numbers?

Answer:

The greatest common divisor (gcd) of two or more integers, which are not all zero, is the largest positive integer that divides each of the integers. For example, the gcd of 8 and 12 is 4.Now try to implement it.

function GCD(a,b) {let divisor = 2,gcd = 1;if(a<2 || b<2){ return 1; }while(a>=divisor && b>=divisor){if(a % divisor == 0 && b % divisor == 0){gcd =divisor; }divisor++;}return gcd;}console.log(GCD(15,25));console.log(GCD(10,50));

We can try another way —

function greatestCommonDivisor(a, b){if(b == 0) return a;else return greatestCommonDivisor(b, a%b);}
console.log(GCD(15,25));

3. How would you remove duplicate members from an array?

Answer:

I will start looping and keep an object and an associated array. If i find an element for the first time i will set its value as true (that will tell me element added once.). if i find a element in the exists object, i will not insert it into the return array.Like as —

const removeDuplicates = (arr) =>{  const exits={},  newArr=[];  let element;  for (let index = 0,len = arr.length;index<len; index++) {    element = arr[index];    if(!exits[element]){    newArr.push(element);    exits[element] =true;    }  }  console.log(newArr);}removeDuplicates([1,1,2,3,4,2,5,6,4,3,1,8]);//[1,2,3,4,5,6,8]

4. swap number without temp.

Answer:

We can do it two way.Let’s see —

//Xor method
const swap = (a,b) => {
a=a^b;b=a^b;a=a^b;console.log(a,b);}swap(4,-9) //-9 4

Another way is —

function swapNumb(a, b){ 
console.log('before swap: ','a: ', a, 'b: ', b);
b = b -a;
a = a+ b;
b = a-b;
console.log('after swap: ','a: ', a, 'b: ', b);
}
swapNumb(2, 3);

5. How would you reverse a string in JavaScript?

Answer:

We can solve this problem by using different method.Here I will show two of them.

function reverse (str) {  if (str === "") {    return "";  } else {     return reverse(str.substr(1)) + str.charAt(0);   } }console.log(reverse('hello'));// olleh

Another Way —

function reverse(str){ 
if(!str || str.length <2) return str;
return str.split('').reverse().join('');
}

6. Write a simple function that returns a boolean indicating whether or not a string is a palindrome.

Answer:

function isPalindrome(str) {str = str.replace(/\W/g, '').toLowerCase();return (str == str.split('').reverse().join(''));}console.log(isPalindrome("level"));                   // logs 'true'console.log(isPalindrome("levels"));                  // logs 'false'console.log(isPalindrome("A car, a man, a maraca"));  //logs ‘true’

7. What will be the output of the following code?

Answer:

var output = (function(x){delete x;return x;})(0);console.log(output);

The output would be 0. The delete operator is used to delete properties from an object. Here x is not an object but a local variable. delete operators don't affect local variables.

8.What will be the output of the code below?

Answer:

var Employee = {company: 'xyz'}var emp1 = Object.create(Employee);delete emp1.companyconsole.log(emp1.company);

The output would be xyz. Here, emp1 object has company as its prototype property. The delete operator doesn't delete prototype property.

9. What will be the output of the code below?

Answer:

var trees = ["xyz","xxxx","test","ryan","apple"];delete trees[3];console.log(trees.length);

The output would be 5. When we use the delete operator to delete an array element, the array length is not affected from this. This holds even if you deleted all elements of an array using the delete operator.

10. What is the significance, and what are the benefits, of including ‘use strict’ at the beginning of a JavaScript source file?

Answer:

The short and most important answer here is that use strict is a way to voluntarily enforce stricter parsing and error handling on your JavaScript code at runtime. Some of the key benefits of strict mode include:

· Makes debugging easier.

· Prevents accidental globals

· Eliminates this coercion

· Disallows duplicate parameter values

· Makes eval() safer.

· Throws error on invalid usage of delete

That’s All for today.Thanks for you time.

--

--