Found 6683 Articles for Javascript

Finding power set for a set in JavaScript Power Set

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

657 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

280 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

565 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

Function to calculate the least common multiple of two numbers in JavaScript

AmitDiwan
Updated on 11-Dec-2020 08:52:47

420 Views

The least common multiple, (LCM) of two integers a and b, is the smallest positive integer that is divisible by both a and b.For example −LCM of 4 and 6 is 12 because 12 is the smallest number that is exactly divisible by both 4 and 6.We are required to write a JavaScript function that takes in two numbers, computes and returns the LCM of those numbers.ExampleFollowing is the code −const num1 = 4; const num2 = 6; const findLCM = (num1, num2) => {    let hcf;    for (let i = 1; i

Combination sum problem using JavaScript

AmitDiwan
Updated on 11-Dec-2020 08:51:51

420 Views

Suppose we are given a set of candidate numbers (without duplicates) and a target number (target).We are required to write a function that finds all unique combinations in candidates where the candidate numbers sum to the target.The same repeated number may be chosen from candidates an unlimited number of times.Note −All numbers (including target) will be positive integers.The solution set must not contain duplicate combinations.For example −If the inputs are −candidates = [2, 3, 6, 7], target = 7, The solution to this can be −[    [7],    [2, 2, 3] ];Since the problem is to get all the ... Read More

Regular Expression Matching in JavaScript

AmitDiwan
Updated on 11-Dec-2020 08:50:06

190 Views

Suppose we are given an input string str and a pattern p, we are required to implement regular expression matching with support for. and *.The functions of these symbols should be −. --> Matches any single character.* --> Matches zero or more of the preceding elements.The matching should cover the entire input string (not partial).Notestr could be empty and contains only lowercase letters a-z.p could be empty and contains only lowercase letters a-z, and characters like. or *.For example −If the input is −const str = 'aa'; const p = 'a';Then the output should be false because a does not ... Read More

Checking power of 2 using bitwise operations in JavaScript

AmitDiwan
Updated on 11-Dec-2020 08:47:20

292 Views

We are required to write a JavaScript function that takes in a number and determines whether or not it is a power of two.For example −f(23) = false f(16) = true f(1) = true f(1024) = trueApproach −Powers of two in binary form always have just one bit. Like this −1: 0001 2: 0010 4: 0100 8: 1000Therefore, after checking that the number is greater than zero, we can use a bitwise hack to test that one and only one bit is set. The same is shown below −num & (num - 1)ExampleFollowing is the code −const num1 = 256; ... Read More

Advertisements