Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Javascript Articles
Page 138 of 534
Levenshtein Distance in JavaScript
The Levenshtein distance is a string metric for measuring the difference between two sequences. It represents the minimum number of single-character edits (insertions, deletions, or substitutions) required to change one word into another. Understanding Levenshtein Distance Consider these two strings: const str1 = 'hitting'; const str2 = 'kitten'; The Levenshtein distance between these strings is 3 because we need three edits: kitten → hitten (substitute "h" for "k") hitten → hittin (substitute "i" for "e") hittin → hitting (insert "g" at the end) ...
Read MoreInterpolation Search in JavaScript
Interpolation search is an efficient searching algorithm for sorted arrays with uniformly distributed values. Unlike binary search, which always checks the middle element, interpolation search estimates where the target value is likely to be found based on its value relative to the array bounds. How Interpolation Search Works The algorithm uses a mathematical formula to estimate the position of the target element: pos = lo + ((target - arr[lo]) * (hi - lo) / (arr[hi] - arr[lo])) This formula returns a higher position when the target is closer to the end of the array, ...
Read MoreSquare matrix rotation in JavaScript
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 = [ ...
Read MoreFinding all the unique paths in JavaScript
In a grid navigation problem, we need to find the number of unique paths from the top-left corner (0, 0) to the bottom-right corner (m-1, n-1) of an m×n grid, where movement is restricted to only right or down directions. This is a classic dynamic programming problem. Each cell's value represents the number of ways to reach that position from the starting point. Algorithm Explanation The solution uses dynamic programming with these key principles: First row and first column have only 1 path each (straight line) For any other cell, paths = paths from above ...
Read MoreEncrypting a string using Caesar Cipher in JavaScript
The Caesar Cipher algorithm is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example: With a left shift of 3, D would be replaced by A, E would become B, and so on. We need to write a JavaScript function that takes in a string to be encrypted as the first argument and a shift amount as the second argument. The shift amount can be a positive or ...
Read MoreFinding the longest common consecutive substring between two strings in JavaScript
We are required to write a JavaScript function that takes in two strings. Let's call them str1 and str2. The function should then find out the longest consecutive string that is common to both the input strings and return that common string. For example − If the input strings are − const str1 = 'ABABC'; const str2 = 'BABCA'; Then the output string should be − const output = 'BABC'; How It Works This problem uses dynamic programming to build a 2D matrix where each cell [i][j] represents the ...
Read MoreFinding the common streak in two arrays in JavaScript
We are required to write a JavaScript function that takes in two arrays of literals, let's call them arr1 and arr2. The function should find the longest common streak of literals in the arrays. The function should finally return an array of those literals. For example − If the input arrays are − const arr1 = ['a', 'b', 'c', 'd', 'e']; const arr2 = ['k', 'j', 'b', 'c', 'd', 'w']; Then the output array should be − ['b', 'c', 'd'] Algorithm Overview This problem uses dynamic programming to ...
Read MoreFinding square root of a number without using Math.sqrt() in JavaScript
We are required to write a JavaScript function that takes in a positive integer as the only argument. The function should find and return the square root of the number provided as the input. Newton's Method (Recommended) The most efficient approach is Newton's method (also called the Babylonian method), which uses iterative approximation to converge on the square root. const squareRoot = (num, precision = 0) => { if (num deviation) { res -= ((res ** 2) - num) / (2 * res); ...
Read MoreFinding all possible ways of integer partitioning in JavaScript
The partition of a positive integer n is a way of writing n as a sum of positive integers. Two sums that differ only in the order of their summands are considered the same partition. For example, 4 can be partitioned in five distinct ways: 4 3 + 1 2 + 2 2 + 1 + 1 1 + 1 + 1 + 1 We are required to write a JavaScript function that takes in a positive integer as the only argument. The function should find and return all the possible ways of partitioning that ...
Read MoreMasking email to hide it in JavaScript
It is a common practice that when websites display anyone's private email address they often mask it in order to maintain privacy. For example, if someone's email address is: const email = 'ramkumar@example.com'; It is displayed like this: r...r@example.com We need to write a JavaScript function that takes an email string and returns the masked email for that string. Basic Email Masking Function Here's a simple approach that masks the username part of the email: const email = 'ramkumar@example.com'; const maskEmail = (email = '') => ...
Read More