1.
Reverse a string
function reverseStringInbuilt(str) {
return [Link]('').reverse().join('');
}
function reverseStringCustom(str) {
let reversedStr = '';
for (let i = [Link] - 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) {
[Link](element);
}
}
return uniqueElements;
}
function findUniqueElements(arr) {
return [Link]((element, index) => {
return [Link](element) !== index;
});
3. Group element based on key
function groupBy(array, key) {
return [Link]((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 = [Link];
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 [Link](
(item, index) => [Link](item) !== index && [Link](item)
=== index
);
function findDuplicatesCustom(arr) {
const duplicates = [];
for (let i = 0; i < [Link]; i++) {
for (let j = i + 1; j < [Link]; j++) {
if (arr[i] === arr[j] && ) {
[Link](arr[i]);
}
}
}
return duplicates;
}
6. Occurrence of each character in the string:
function countCharacterOccurrences(str) {
let charCount = {};
for (let char of str) {
if (charCount[char]) {
charCount[char]++;
} else {
charCount[char] = 1;
}
}
return charCount;
}
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() {
[Link]("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 [Link] + " " + [Link];
},
};
const person1 = {
firstName: "John",
lastName: "Doe",
};
[Link]([Link](person1));
The apply method is similar to call, but it takes arguments as an array
const person = {
fullName: function (city, country) {
return [Link] + " " + [Link] + ", " + city + ", " +
country;
},
};
const person1 = {
firstName: "John",
lastName: "Doe",
};
[Link]([Link](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 [Link] + " " + [Link];
},
};
const person1 = {
firstName: "Jane",
lastName: "Doe",
};
const fullName = [Link](person1);
[Link](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() {
[Link](a);
}
return y;
}
var z = x();
[Link](z); // value of z is entire code of function y.