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 185 of 801
Deriving Random10() function from Random7() in JavaScript
Suppose we have a random7() function that generates random numbers from 1 to 7. We need to create a random10() function that generates random numbers from 1 to 10, using only the random7() function. Problem const random7 = () => Math.ceil(Math.random() * 7); This function yields a random number between 1 and 7 (inclusive) every time we call it. We need to write a random10() function that returns random numbers between 1 and 10 (inclusive) using only this random7() function. Using Rejection Sampling (Optimal Solution) The most efficient approach uses rejection sampling. We ...
Read MoreGet max value per key in a JavaScript array
When working with arrays of objects in JavaScript, you might need to find the object with the maximum value for a specific property within each group. This is a common task when analyzing data grouped by categories. Consider this array of fruit data: const arr = [ {a:1, b:"apples"}, {a:3, b:"apples"}, {a:4, b:"apples"}, {a:1, b:"bananas"}, {a:3, b:"bananas"}, {a:5, b:"bananas"}, {a:6, b:"bananas"}, {a:3, b:"oranges"}, ...
Read MoreFinding closest pair sum of numbers to a given number in JavaScript
We need to write a JavaScript function that takes an array of numbers and a target number, then returns the pair of numbers from the array whose sum is closest to the target. The function should find two different numbers from the original array that, when added together, produce a sum nearest to the specified target value. Basic Approach Using Nested Loops Here's a straightforward solution using nested loops to check all possible pairs: const arr = [1, 2, 3, 4, 5, 6, 7]; const num = 14; const closestPair = (arr, target) => ...
Read MoreHyphen string to camelCase string in JavaScript
Converting hyphen-separated strings to camelCase is a common requirement in JavaScript development. We need to transform strings like 'this-is-an-example' into 'thisIsAnExample'. Problem Statement Given a string with words separated by hyphens: const str = 'this-is-an-example'; We need to convert it into camelCase format: const output = 'thisIsAnExample'; Using split(), map(), and join() Method The most straightforward approach splits the string by hyphens, capitalizes each word except the first, and joins them back: const str = 'this-is-an-example'; const changeToCamel = str => { ...
Read MoreDistance between 2 duplicate numbers in an array JavaScript
We are required to write a JavaScript function that takes in an array of numbers that contains at least one duplicate pair of numbers. Our function should return the distance between all the duplicate pairs of numbers that exist in the array. The distance is calculated as the minimum difference between indices where duplicate numbers appear. Example Let's work with an array containing duplicates and find the minimum distance between each duplicate pair: const arr = [2, 3, 4, 2, 5, 4, 1, 3]; const findDistance = arr => { var ...
Read MoreRemove smallest number in Array JavaScript
In JavaScript, removing the smallest number from an array can be accomplished in several ways. This article demonstrates different approaches to find and remove the smallest element from an array in place. Method 1: Using reduce() and splice() This approach uses reduce() to find the index of the smallest element, then splice() to remove it: const arr = [2, 1, 3, 2, 4, 5, 1]; const removeSmallest = arr => { const smallestCreds = arr.reduce((acc, val, index) => { let { num, ind ...
Read MoreHow to find capitalized words and add a character before that in a given sentence using JavaScript?
When working with strings that contain capitalized words, you might need to insert a character (like a comma) before each capital letter. This is useful for parsing concatenated sentences or formatting text data. Problem Statement Given a string with capitalized words, we want to add a comma before each capital letter that appears after another character: const str = "Connecting to server Connection has been successful We found result"; The goal is to transform this into: "Connecting to server, Connection has been successful, We found result" Solution Using Regular Expression We can ...
Read MoreSum of all the non-repeating elements of an array JavaScript
To find the sum of non-repeating elements in an array, we need to identify elements that appear exactly once and add them together. Problem Statement Given an array of numbers, we want to calculate the sum of all elements that appear only once in the array. const arr = [14, 54, 23, 14, 24, 33, 44, 54, 77, 87, 77, 14]; console.log("Input array:", arr); Input array: [14, 54, 23, 14, 24, 33, 44, 54, 77, 87, 77, 14] In this array, the non-repeating elements are: 23, 24, 33, 44, 87. Their ...
Read MorePair of (adjacent) elements of an array whose sum is lowest JavaScript
We are required to write a JavaScript function that takes in an array of numbers. The function should return a subarray of two adjacent elements from the original array whose sum is the least amongst all adjacent pairs of the array. If the length of the array is less than 2, we should return boolean false. Example Input For example, if the input array is: const arr = [41, 44, -12, 13, -23, 1, 5, -4, 2, 2]; Here, the sum of pair [-23, 1] is -22 which is the least for any ...
Read MoreRemoving a specific substring from a string in JavaScript
We are given a main string and a substring, our job is to create a function, let's say removeString() that takes in these two arguments and returns a version of the main string which is free of the substring. Here, we need to remove the separator from a string, for example: this-is-a-string Using split() and join() Method The most common approach is to split the string by the substring and then join the parts back together: const removeString = (string, separator) => { // we split the string ...
Read More