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
Object Oriented Programming Articles
Page 57 of 589
Finding 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 MoreSwapcase function in JavaScript
In JavaScript, a swapcase function converts uppercase letters to lowercase and lowercase letters to uppercase while leaving non-alphabetic characters unchanged. Problem Statement We need to create a function that takes a string and returns a new string where: Uppercase letters become lowercase Lowercase letters become uppercase Non-alphabetic characters remain unchanged Method 1: Using Helper Function This approach uses a separate function to determine the case of each character: const str = 'ThIs Is A STriNG'; const findLetter = (char = '') => { if (char.toLowerCase() === ...
Read MoreMin window substring in JavaScript
We are required to write a JavaScript function that takes in two strings let's call them str1 and str2. The size of str1 is guaranteed to be greater than that of str2. We are required to find the smallest substring in str1 that contains all the characters contained in str2. For example − If the input strings are − const str1 = 'abcdefgh'; const str2 = 'gedcf'; Then the output should be − const output = 'cdefg'; because this the smallest consecutive substring of str1 that contains all characters ...
Read MoreFinding the longest word in a string in JavaScript
We are required to write a JavaScript function that takes in a string as the only argument. The function should then iterate through the string and find and return the longest word from the string. For example − If the input string is − const str = 'Coding in JavaScript is really fun'; Then the output string should be − const output = 'JavaScript'; Using Array.reduce() Method The most efficient approach uses the reduce() method to iterate through words and track the longest one: const str = ...
Read MoreAre mean mode of a dataset equal in JavaScript
We are required to write a JavaScript function that takes in an array of numbers and checks if the mean and mode are equal. The function should calculate both statistical measures and return true if they match, false otherwise. For example − If the input array is − const arr = [5, 3, 3, 3, 1]; Then the output for this array should be true because both the mean and mode of this array are 3. Understanding Mean and Mode The mean is the average of all numbers in the dataset. The ...
Read MoreFinding union of two sets in JavaScript
The union of two sets is the combination of all unique elements from both sets. In JavaScript, we can find the union of two arrays using several approaches. What is Union Set Union Set is the set made by combining the elements of two sets. Therefore, the union of sets A and B is the set of elements in A, or B, or both. For example − If we have two sets denoted by two arrays like this − const arr1 = [1, 2, 3]; const arr2 = [100, 2, 1, 10]; ...
Read More