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
Object Oriented Programming Articles
Page 21 of 589
Finding 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 MoreSorting elements of stack using JavaScript
We are required to write a JavaScript function that takes in an array of integers and sorts it using recursion with stack operations (push and pop methods). The function sorts the array in-place without using built-in sorting methods. How It Works The algorithm uses two recursive functions: sortStack() - Removes elements from the stack and recursively sorts the remaining elements sortedInsert() - Inserts an element in its correct position in an already sorted stack Example Here's the complete implementation: const stack = [-3, 14, 18, -5, 30]; const sortStack = (stack ...
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 MoreIndexing numbers to alphabets in JavaScript
We are required to write a JavaScript function that takes in a number between the range [0, 25], both inclusive and returns the corresponding alphabet character. Return Value The function should return the corresponding alphabet for that number, where 0 = 'A', 1 = 'B', 2 = 'C', and so on. Using String.fromCharCode() Method The most common approach uses ASCII values. The ASCII value of 'A' is 65, so we add our number to get the corresponding character. const num = 15; const indexToAlpha = (num = 1) => { ...
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 MoreReversing the alphabet from back to front and front to back in JavaScript
In JavaScript, we can create a function that finds the "mirror" position of an alphabet character. Given a letter, we find its position from the start and return the letter at the same position from the end. Understanding the Logic The alphabet has 26 letters. If we split it into two halves, each letter in the first half corresponds to a letter in the second half when reversed. For example, 'a' (1st position) corresponds to 'z' (26th position), 'b' corresponds to 'y', and so on. Example Implementation Here's how we can implement this functionality: ...
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 MoreUsing a recursive function to capitalize each word in an array in JavaScript
We are required to write a JavaScript function that takes in an array of String literals. The function should do the following two things − Make use of recursive approach Make first word of each string element capital. Our function should do this without using extra space for storing another array. For example − If the input array is − const arr = ['apple', 'banana', 'orange', 'grapes']; Then the array should be transformed to − const output = ['Apple', 'Banana', 'Orange', 'Grapes']; ...
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 MoreDifference between first and the second array in JavaScript
When working with arrays in JavaScript, you often need to find elements that exist in one array but not in another. This operation is commonly known as finding the "difference" between two arrays. We'll create a function that takes two arrays and returns all elements from the first array that don't exist in the second array. Example Here's how to implement this functionality: const arr1 = ['1', '2', '3', '4/2', '5/4', '6-2']; const arr2 = ['1', '2', '3', '5/4', '4/2', '6-1', '7/2', '8-2']; const differenceBetween = (arr1 = [], arr2 = []) => { ...
Read More