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
Javascript Articles
Page 325 of 534
Min 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 of str2.ExampleFollowing is the code −const str1 = 'abcdefgh'; const str2 = 'gedcf'; const subIncludesAll = (str, str2) ...
Read MoreUncamelising a string in JavaScript
We are required to write a JavaScript function that takes in a string as the first argument and a separator character as the second argument.The first string is guaranteed to be a camelCased string. The function should convert the case of the string by separating the words by the separator provided as the second argument.For example −If the input string is −const str = 'thisIsAString'; const separator = '_';Then the output string should be −const output = 'this_is_a_string';ExampleFollowing is the code −const str = 'thisIsAString'; const separator = '_'; const separateCase = (str = '', separator = ' ') => ...
Read MoreSwapcase function in JavaScript
We are required to write a JavaScript function that takes in a string as the only argument.The string might contain both uppercase and lowercase alphabets.The function should construct a new string based on the input string in which all the uppercase letters are converted to lowercase and all the lowercase letters are converted to uppercase.ExampleFollowing is the code −const str = 'ThIs Is A STriNG'; const findLetter = (char = '') => { if(char.toLowerCase() === char.toUpperCase){ return char; }else if(char.toLowerCase() === char){ return char.toUpperCase(); }else{ return char.toLowerCase(); ...
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.Therefore for example −If someone's email address is −const email = 'ramkumar@example.com';Then it is displayed like this −const masked = 'r...r@example.com';We are required to write a JavaScript function that takes in an email string and returns the masked email for that string.ExampleFollowing is the code −const email = 'ramkumar@example.com'; const maskEmail = (email = '') => { const [name, domain] = email.split('@'); const { length: len } = name; const maskedName = name[0] + '...' + ...
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 + 1We 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 integer.ExampleFollowing is the code −const findPartitions = (num = 1) => { const arr = Array(num + 1).fill(null).map(() => { return Array(num + 1).fill(null); }); for (let j = 1; j
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.ExampleFollowing is the code −const squareRoot = (num, precision = 0) => { if (num deviation) { res -= ((res ** 2) - num) / (2 * res); }; return Math.round(res * (10 ** precision)) / (10 ** precision); }; console.log(squareRoot(16)); console.log(squareRoot(161, 3)); console.log(squareRoot(1611, 4));OutputFollowing is the output on console −4 12.689 40.1373
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 −const output = ['b', 'c', 'd'];ExampleFollowing is the code −const arr1 = ['a', 'b', 'c', 'd', 'e']; const arr2 = ['k', 'j', 'b', 'c', 'd', 'w']; const longestCommonSubsequence = ...
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';ExampleFollowing is the code −const str1 = 'ABABC'; const str2 = 'BABCA'; const findCommon = (str1 = '', str2 = '') => { const s1 = [...str1]; const s2 = [...str2]; const arr ...
Read MoreEncrypting a string using Caesar Cipher in JavaScript
Caesar Cipher AlgorithmThe 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 exampleWith a left shift of 3, D would be replaced by A, E would become B, and so on. We are required 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 negative integer ...
Read MoreFinding all the unique paths in JavaScript
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