Javascript Articles

Page 274 of 534

Finding square root of a number without using Math.sqrt() in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Dec-2020 845 Views

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 More

Finding the common streak in two arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Dec-2020 278 Views

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 More

Finding the longest common consecutive substring between two strings in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Dec-2020 1K+ Views

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 More

Encrypting a string using Caesar Cipher in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Dec-2020 772 Views

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 More

Finding all the unique paths in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Dec-2020 281 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
AmitDiwan
Updated on 11-Dec-2020 613 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
AmitDiwan
Updated on 11-Dec-2020 742 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
AmitDiwan
Updated on 11-Dec-2020 9K+ 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
AmitDiwan
Updated on 11-Dec-2020 826 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
AmitDiwan
Updated on 11-Dec-2020 592 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

Read More
Showing 2731–2740 of 5,338 articles
« Prev 1 272 273 274 275 276 534 Next »
Advertisements