1.
Reverse a string
function reverseStringInbuilt(str) {
return str.split('').reverse().join('');
}
function reverseStringCustom(str) {
let reversedStr = '';
for (let i = str.length - 1; i >= 0; i--) {
reversedStr += str[i];
}
return reversedStr;
}
2. Find unique element in an array with count:
function findUniqueElements(arr) {
const elementCount = {};
const uniqueElements = [];
// Count the occurrences of each element
for (const element of arr) {
elementCount[element] = (elementCount[element] || 0) + 1;
}
// Filter elements that occur only once
for (const element of arr) {
if (elementCount[element] === 1) {
uniqueElements.push(element);
}
}
return uniqueElements;
}
function findUniqueElements(arr) {
return arr.filter((element, index) => {
return arr.indexOf(element) !== index;
});
3. Group element based on key
function groupBy(array, key) {
return array.reduce((result, currentValue) => {
const groupKey = currentValue[key];
if (!result[groupKey]) {
result[groupKey] = [];
}
result[groupKey].push(currentValue);
return result;
}, {});
}
4. Sorting function
function sortArrayCustom(arr) {
let n = arr.length;
for (let i = 0; i < n - 1; i++) {
for (let j = 0; j < n - 1 - i; j++) {
if (arr[j] < arr[j + 1]) {
let temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}return arr;
}
5. find duplicate items in an array
function findDuplicatesInbuilt(arr) {
return arr.filter(
(item, index) => arr.indexOf(item) !== index && arr.indexOf(item)
=== index
);
function findDuplicatesCustom(arr) {
const duplicates = [];
for (let i = 0; i < arr.length; i++) {
for (let j = i + 1; j < arr.length; j++) {
if (arr[i] === arr[j] && !duplicates.includes(arr[i])) {
duplicates.push(arr[i]);
}
}
}
return duplicates;
}
Q: What is Higher Order Function?
Ans: A Higher-order functions are regular functions that take other functions as
arguments or return functions as their results. Eg:
function x() {
console.log("Hi)";
};
function y(x) {
x();
};
y();
Q: Call, apply and bind?
The call method calls a function with a given this value and arguments provided
const person = {
fullName: function () {
return this.firstName + " " + this.lastName;
},
};
const person1 = {
firstName: "John",
lastName: "Doe",
};
console.log(person.fullName.call(person1));
The apply method is similar to call, but it takes arguments as an array
const person = {
fullName: function (city, country) {
return this.firstName + " " + this.lastName + ", " + city + ", " +
country;
},
};
const person1 = {
firstName: "John",
lastName: "Doe",
};
console.log(person.fullName.apply(person1, ["Oslo", "Norway"]));
The bind method creates a new function that, when called, has its this value set to
the provided value, with a given sequence of arguments
const person = {
firstName: "John",
lastName: "Doe",
fullName: function () {
return this.firstName + " " + this.lastName;
},
};
const person1 = {
firstName: "Jane",
lastName: "Doe",
};
const fullName = person.fullName.bind(person1);
console.log(fullName());
Q: Hoisting
Hoisting is a concept which enables us to extract values of variables and
functions even before initialising/assigning value without getting error and this is
happening due to the 1st phase (memory creation phase) of the Execution
Context.
Q: Data Hiding and Encapsulation:
● Encapsulation hides the internal details of an object and exposes only
necessary methods and properties. It improves code maintainability and
security.
Q: Memoization
Memoization:
● Memoization optimizes expensive function calls by caching their results.
It's useful for recursive or repetitive computations.
Q: Closure
A closure is a function that has access to its outer function scope even after
the function has returned. Meaning, A closure can remember and access
variables and arguments reference of its outer function even after the function
has returned.
If a function needs to access a variable, it first goes to its local memory. When it
does not find it there, it goes to the memory of its lexical parent.
function x() {
var a = 7;
function y() {
console.log(a);
}
return y;
}
var z = x();
console.log(z); // value of z is entire code of function y.