0% found this document useful (0 votes)
2 views6 pages

Interview Questions

The document discusses various JavaScript concepts including the differences between service workers and web workers, code output predictions, design patterns, palindrome checks, Fibonacci series, factorial calculation, event loops, and array reversal methods. It provides code examples and explanations for each topic. Additionally, it includes a practice link for further learning.

Uploaded by

itaminul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

Interview Questions

The document discusses various JavaScript concepts including the differences between service workers and web workers, code output predictions, design patterns, palindrome checks, Fibonacci series, factorial calculation, event loops, and array reversal methods. It provides code examples and explanations for each topic. Additionally, it includes a practice link for further learning.

Uploaded by

itaminul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Interview Questions

How is a Service Worker different from a Web Worker?

Service workers are used to intercept network requests. In a way, the service worker is like a proxy server
assisting in improving offline user experience and designing PWAs.

The intended use case for web workers is parallelism, while service workers are used for creating offline
support for a web app.

Predict the output of the below code:

function display() {
var a = b = 10;
}

display();

console.log('b', typeof b === 'undefined');


console.log('a', typeof a === 'undefined');

----

The output of the above code is:

b false
a true

This is because the assignment operator has right to left associativity in Javascript which means it will be
evaluated from right to left.

So first, b will be assigned the value 10 and then it is assigned to a.

Therefore, the below code:

function display() {
var a = b = 10;
}

is same as

function display() {
var a = (b = 10);
}

which is same as

function display() {
b = 10;
var a = b;
}

So b becomes a global variable because there is no var keyword before it and a becomes a local variable.

Therefore, outside the function, only b is available so 𝘁𝘆𝗽𝗲𝗼𝗳 𝗮 === '𝘂𝗻𝗱𝗲𝗳𝗶𝗻𝗲𝗱' comes as 𝘁𝗿𝘂𝗲 and
𝘁𝘆𝗽𝗲𝗼𝗳 𝗯 === '𝘂𝗻𝗱𝗲𝗳𝗶𝗻𝗲𝗱' comes as 𝗳𝗮𝗹𝘀𝗲.

The Singleton Design Pattern


The Singleton design pattern ensures a class can have only one instance and provides global access to it.
Examples include caches, thread pools, and registries. Typically, the class's constructor is set to private to
enforce this constraint.

JavaScript does not have the concept of private constructors, so the most common way to achieve the
same effect is to use closures and immediately-invoked function expressions (IIFE)

In this example, the Person constructor is defined within an IIFE, which creates a closure around the
constructor and makes it private to the IIFE.
Palindrome strings
A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward
or forward. This includes capital letters, punctuation, and word dividers.

Implement a function that checks if something is a palindrome. If the input is a number, convert it to string
first.

Examples(Input ==> Output)


"anna" ==> true
"walter" ==> false
12321 ==> true
123456 ==> false

const isPalindrome = (input) => {


const str = String(input);
const reversedStr = str.split('').reverse().join('');
return str === reversedStr;
}

const result = isPalindrome('12321');

Example 02:

const isPalindrome = (input) => {


const str = String(input);
for (let i = 0; i < str.length / 2; i++) {
if (str[i] !== str[str.length - 1 - i]) {
return false;
}
}
return true;
}

const result = isPalindrome('walter');

Fibonacci Series
In mathematics, the Fibonacci sequence is a sequence in which each number is the sum of the two
preceding ones. Numbers that are part of the Fibonacci sequence are known as Fibonacci numbers.

const displayFabonacciSeries = (inputNumber) => {


let f1 = 0, f2 = 1, f3=0;

for (let i = 1; i <= inputNumber; i++) {


f3 = f1 + f2;
console.log(f3);
f1 = f2;
f2 = f3;
};
};

displayFabonacciSeries(5); // 1 2 3 5 8
Factorial Number
In mathematics, the factorial of a non-negative integer n, denoted by n! is the product of all positive integers
less than or equal to n.

const displayFactorial = (inputNumber) => {

let result = 1;
for (let i = 1; i <= inputNumber; i++) {
result *= i;
};
console.log(result);
};

displayFactorial(5); // 120

Event Loop
The event loop is a mechanism that allows JavaScript to handle asynchronous operations by managing the
execution of code:

What is the output of the following codes:


function logA() { console.log('A') }
function logB() { console.log('B') }
function logC() { console.log('C') }
function logD() { console.log('D') }

logA();
setTimeout(logB, 0);
Promise.resolve().then(logC);
logD();

Output: ?

Practce Link: https://www.jsv9000.app/

Please reverse an Array without using Built-in reverse() Method:

const reverseAnArray = (arr) => {

if (!Array.isArray(arr)) {
throw new Error("This is not a valid array");
}
const reversedArray = [];
for (let i = 0; i < arr.length; i++) {
reversedArray[i] = arr[arr.length - 1 - i];
}
return reversedArray;
}

console.log(reverseAnArray([1, 2, 3])); // Output: [3, 2, 1]

Example 2:

const reverseAnArray = (arr) => {


if (!Array.isArray(arr)) {
throw new Error("This is not a valid array");
}

const reversedArray = [];


for (let i = arr.length - 1; i >= 0; i--) {
reversedArray.push(arr[i]);
}
return reversedArray;
}

console.log(reverseAnArray([1, 2, 3])); // Output: [3, 2, 1]

You might also like