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
Web Development Articles
Page 237 of 801
Finding 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 MoreTribonacci series in JavaScript
The tribonacci sequence is a generalization of the Fibonacci sequence where each term is the sum of the three preceding terms. Unlike Fibonacci which uses two preceding terms, tribonacci uses three. The standard tribonacci series starts with 0, 1, 1, and each subsequent term is the sum of the previous three terms: 0, 1, 1, 2, 4, 7, 13, 24, 44, 81, 149... How It Works Each term after the first three follows the formula: T(n) = T(n-1) + T(n-2) + T(n-3) Where T(0) = 0, T(1) = 1, T(2) = 1 ...
Read MoreFinding n subarrays with equal sum in JavaScript
We are required to write a JavaScript function that takes in an array of integers as the first argument and an integer as the second argument. The function should check whether we can create n (second argument) subarrays from the original array such that all the subarrays have the equal sum. For example − If the inputs are − const arr = [4, 3, 2, 3, 5, 2, 1]; const num = 4; The output should be true because the subarrays are: [5], [1, 4], [2, 3], [2, 3] all having sum equal ...
Read More