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 137 of 534
Trim off '?' from strings in JavaScript
We are required to write a JavaScript function that takes in a string as the only argument. The string is likely to contain question marks (?) in the beginning and the end. The function should trim off all these question marks from the beginning and the end keeping everything else in place. For example − If the input string is − const str = '??this is a ? string?'; Then the output should be − 'this is a ? string' Method 1: Using Regular Expressions The simplest approach uses ...
Read MoreInsert a character at nth position in string in JavaScript
We are required to write a JavaScript function that takes in a string as the first argument and a number as the second argument and a single character as the third argument, let's call this argument char. The number is guaranteed to be smaller than the length of the array. The function should insert the character char after every n characters in the string and return the newly formed string. For example − If the arguments are − const str = 'NewDelhi'; const n = 3; const char = ' '; Then the ...
Read MoreComparing the performance of recursive and looped factorial function in JavaScript
We will be writing two JavaScript functions, the job of both the functions will be to take in a number and return its factorial. The first function should make use of a for loop or while loop to compute the factorial. Whereas the second function should compute the factorial using a recursive approach. Lastly, we should compare the times taken by these functions over a large number of iterations. Iterative Factorial Function The iterative approach uses a simple for loop to calculate the factorial: const factorial = (num = 1) => { ...
Read MorePrimality test of numbers in JavaScript
A prime number (or a prime) is a natural number greater than 1 that cannot be formed by multiplying two smaller natural numbers. All other natural numbers greater than 1 are called composite numbers. A primality test is an algorithm for determining whether an input number is prime. We are required to write a JavaScript function that takes in a number and checks whether it is a prime or not. Basic Primality Test Algorithm The most efficient approach is to check divisibility up to the square root of the number, skipping even numbers after checking for 2. ...
Read MoreEuclidean Algorithm for calculating GCD in JavaScript
In mathematics, Euclid's algorithm is a method for computing the greatest common divisor (GCD) of two numbers, the largest number that divides both of them without leaving a remainder. The Euclidean algorithm is based on the principle that the greatest common divisor of two numbers does not change if the larger number is replaced by its difference with the smaller number. For example, 21 is the GCD of 252 and 105 (as 252 = 21 × 12 and 105 = 21 × 5), and the same number 21 is also the GCD of 105 and 252 − 105 ...
Read MoreChecking power of 2 using bitwise operations in JavaScript
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) = true Understanding Powers of Two in Binary Powers of two in binary form always have exactly one bit set to 1: 1: 0001 2: 0010 4: 0100 8: 1000 16: 10000 32: 100000 The Bitwise Approach ...
Read MoreRegular Expression Matching in JavaScript
Regular expression matching is a fundamental concept in computer science where we match an input string against a pattern containing special characters. In JavaScript, we can implement custom regex matching using dynamic programming to handle patterns with . (any character) and * (zero or more occurrences). Problem Definition Given an input string str and a pattern p, implement regular expression matching with support for: . → Matches any single character * → Matches zero or more of the preceding element The matching must cover the entire input string ...
Read MoreCombination sum problem using JavaScript
The combination sum problem involves finding all unique combinations from a given array where the numbers sum to a target value. Each number can be used multiple times, making this a classic backtracking problem. Problem Statement Given a set of candidate numbers (without duplicates) and a target number, find all unique combinations where the candidate numbers sum to the target. Constraints: All numbers (including target) are positive integers The same number may be chosen multiple times The solution set must not contain duplicate combinations Example: Input: candidates = ...
Read MoreConverting degree to radian in JavaScript
Converting degrees to radians is a common mathematical operation in JavaScript, especially when working with trigonometric functions or graphics programming. Understanding Radians The radian is the standard unit for measuring angles in mathematics. One complete circle equals 2π radians, which is equivalent to 360 degrees. This means π radians equals 180 degrees. The Conversion Formula To convert degrees to radians, multiply the degree value by π/180: radians = degrees × (π / 180) Method 1: Using the Standard Formula const degreeToRadian = (degree) => { const ...
Read MoreFinding power set for a set in JavaScript Power Set
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 example If S = {x, y, z}, the subsets are: { {}, {x}, {y}, {z}, {x, y}, {x, z}, {y, z}, {x, y, z} } We need ...
Read More