Found 9321 Articles for Object Oriented Programming

Finding all the unique paths in JavaScript

AmitDiwan
Updated on 11-Dec-2020 09:09:31

184 Views

Suppose we have an array of m * n order. A person starts at the start block of the 2-D array (0, 0) and he wants to reach the end (m, n). The limitation is that at once, he can either move one step down or one step right.We are required to write a JavaScript function that takes in the height and width of the 2-D grid.The function should find out the number of unique paths that are available to the person to reach to the end.ExampleFollowing is the code −const height = 3; const width = 4; const findUniquePath ... Read More

Square matrix rotation in JavaScript

AmitDiwan
Updated on 11-Dec-2020 09:08:03

501 Views

We are required to write a JavaScript function that takes in an array of arrays of n * n order (square matrix). The function should rotate the array by 90 degrees (clockwise). The condition is that we have to do this in place (without allocating any extra array).For example −If the input array is −const arr = [    [1, 2, 3],    [4, 5, 6],    [7, 8, 9] ];Then the rotated array should look like −const output = [    [7, 4, 1],    [8, 5, 2],    [9, 6, 3], ];ExampleFollowing is the code −const arr = ... Read More

Interpolation Search in JavaScript

AmitDiwan
Updated on 11-Dec-2020 09:04:07

512 Views

Interpolation SearchInterpolation search is an algorithm for searching for a key in an array that has been ordered by numerical values assigned to the keys (key values).For exampleSuppose, we have a sorted array of n uniformly distributed values arr[], and we need to write a function to search for a particular element target in the array.It does the following operations to find the position −// The idea of the formula is to return a higher value of pos// when element to be searched is closer to arr[hi]. And// smaller value when closer to arr[lo]pos = lo + ((x - arr[lo]) ... Read More

Levenshtein Distance in JavaScript

AmitDiwan
Updated on 11-Dec-2020 09:02:31

7K+ Views

Levenshtein DistanceThe Levenshtein distance is a string metric for measuring the difference between two sequences. It is the minimum number of single-character edits required to change one word into the other.For example −Consider, we have these two strings −const str1 = 'hitting'; const str2 = 'kitten';The Levenshtein distance between these two strings is 3 because we are required to make these three edits −kitten → hitten (substitution of "h" for "k")hitten → hittin (substitution of "i" for "e")hittin → hitting (insertion of "g" at the end)We are required to write a JavaScript function that takes in two strings and calculates ... Read More

Finding power set for a set in JavaScript Power Set

AmitDiwan
Updated on 11-Dec-2020 09:00:55

656 Views

The power set of a set S is the set of all of the subsets of S, including the empty set and S itself. The power set of set S is denoted as P(S).For exampleIf S = {x, y, z}, the subsets are −{    {},    {x},    {y},    {z},    {x, y},    {x, z},    {y, z},    {x, y, z} }We are required to write a JavaScript function that takes in an array as the only argument. The function should find and return the power set for the input array.ExampleFollowing is the code −const set ... Read More

Converting degree to radian in JavaScript

AmitDiwan
Updated on 11-Dec-2020 08:59:00

279 Views

RadianThe radian is the unit for measuring angles and is the standard unit of angular measure used in many areas of mathematics.We are required to write a JavaScript function that takes in a number that represents some degree and returns its corresponding radian.ExampleFollowing is the code −const deg = 180; const degreeToRadian = (degree) => {    const factor = (Math.PI / 180);    const rad = degree / factor;    return rad; }; console.log(degreeToRadian(deg));OutputFollowing is the output on console −10313.240312354817

Recursive Staircase problem in JavaScript

AmitDiwan
Updated on 11-Dec-2020 08:57:58

564 Views

Suppose we have the following problem −There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 or 2 stairs at a time. We are required to count the number of ways, the person can reach the top.We are required to write a JavaScript function that takes in a number n that denotes the number of stairs. The function should count and return the number of ways in which the stairs can be climbed.ExampleFollowing is the code −const recursiveStaircase = (num = 10) => {    if (num

Implementing block search in JavaScript

AmitDiwan
Updated on 11-Dec-2020 08:57:07

185 Views

Block SearchJust like Binary Search, Block Search is also a searching algorithm for sorted arrays. The basic idea is to check fewer elements (than linear search) by jumping ahead by fixed steps or skipping some elements in place of searching all elements.For exampleSuppose we have an array arr of length n and block (to be jumped) of size m. Then we search at the indexes arr[0], arr[m], arr[2 * m], ..., arr[k * m] and so on.Once we find the interval arr[k * m] < x < arr[(k+1) * m], we perform a linear search operation from the index k ... Read More

Hamming Distance between two strings in JavaScript

AmitDiwan
Updated on 11-Dec-2020 08:54:59

1K+ Views

Hamming DistanceThe Hamming distance between two strings of equal length is the number of positions at which the corresponding symbols are different.For example, consider the following strings −const str1 = 'delhi'; const str2 = 'delph';The Hamming Distance of these strings is 2 because the fourth and the fifth characters of the strings are different. And obviously in order to calculate the Hamming Distance we need to have two strings of equal lengths.Therefore, we are required to write a JavaScript function that takes in two strings, let’s say str1 and str2, and returns their hamming distance.ExampleFollowing is the code −const str1 ... Read More

Computing the Cartesian Product of Two Sets in JavaScript

AmitDiwan
Updated on 31-Mar-2023 15:40:15

191 Views

Cartesian Product Inset theory a Cartesian product is a mathematical operation that returns a set (or product set or simply product) from multiple sets.That is, for sets A and B, the Cartesian product A × B is the set of all ordered pairs (a, b) where a ∈ A and b ∈ B.We are required to write a JavaScript function that takes in two arrays let us call them arr1 and arr2, they both represent two distinct sets.The function should construct a 2-D array that contains the cartesian product of those two sets and finally return that array.ExampleFollowing is the code ... Read More

Advertisements