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
Articles by AmitDiwan
Page 473 of 840
Find the middle element of an array using recursion JavaScript
We are required to write an array function, say findMiddle that returns the middlemost element of the array without accessing its length property and without using any kind of built-in loops. If the array contains an odd number of elements, we return the one, middlemost element, or if the array contains an even number of elements, we return an array of two middlemost elements. So, let's write the code for this function. As you've already guessed, we will be making use of recursion to find these elements. The code for the recursive function will be — How It ...
Read MoreProgram to find uncommon elements in two arrays - JavaScript
Finding uncommon elements in two arrays means identifying elements that exist in one array but not in both. This is also known as finding the symmetric difference between two arrays. Problem Definition Given two arrays, we need to find elements that are present in either the first array or the second array, but not in both arrays. const arr1 = [12, 54, 2, 4, 6, 34, 3]; const arr2 = [54, 2, 5, 12, 4, 1, 3, 34]; console.log("Array 1:", arr1); console.log("Array 2:", arr2); Array 1: [12, 54, 2, 4, 6, 34, ...
Read MoreHow to create a URL object using JavaScript?
The URL object in JavaScript provides a convenient way to parse and manipulate URLs. It extracts components like protocol, host, pathname, and search parameters from URL strings. Syntax let url = new URL(urlString); let url = new URL(urlString, baseURL); Parameters urlString - The URL string to parse baseURL - (Optional) Base URL for relative URLs Example URL Object Example body { ...
Read MoreChanging an array in place using splice() JavaScript
The splice() method allows you to modify arrays in place by removing, adding, or replacing elements. This article demonstrates how to use splice() to remove duplicate elements that exceed a specified count limit. The Problem We need to write a function that takes an array and a number n, then removes elements that appear more than n times while preserving the order of remaining elements. Solution Using splice() We'll track element counts using a hashmap and use splice() to remove excess occurrences during iteration: const arr = [7, 26, 21, 41, 43, 2, 26, ...
Read MoreCreating an associative array in JavaScript with push()?
An associative array in JavaScript is essentially an object that uses string keys to store arrays of values. You can create this structure by combining forEach() loops with the push() method to group related data. Example: Grouping Students by ID Here's how to create an associative array that groups student names by their student ID: var studentDetails = [ { studentId: 1, studentName: "John" }, { ...
Read MoreCount appearances of a string in another - JavaScript
We are required to write a JavaScript function that takes in two strings and returns the count of the number of times the first string appears in the second string. Let's say our string is: const main = 'This is the is main is string'; We have to find the appearance of the below string in the above "main" string: const sub = 'is'; Using Regular Expression with replace() Let's write the code for this function using a regular expression approach: const main = 'This is the is ...
Read MoreHow to copy text to the clipboard with JavaScript?
To copy text to the clipboard in JavaScript, we can use both modern and legacy approaches. The modern navigator.clipboard API is recommended, while the older document.execCommand() method provides fallback support. Modern Approach: Using navigator.clipboard API The navigator.clipboard.writeText() method is the modern standard for copying text. It returns a Promise and works asynchronously. button { border: none; ...
Read MoreExplain shorthand functions in JavaScript?
Arrow functions, also known as shorthand functions, were introduced in ES2015 and allow us to write functions in a shorter, more concise way. They don't have their own binding to this and inherit this from the surrounding context. Basic Syntax Arrow functions use the => syntax instead of the function keyword: // Regular function function add(a, b) { return a + b; } // Arrow function const add = (a, b) => a + b; Different Arrow Function Forms ...
Read MoreAlgorithm to dynamically populate JavaScript array with zeros before and after values
We are given a months array, which contains elements less than 12, where each element will be between 1 and 12 (both inclusive). Our job is to take this array and create a full months array with 12 elements. If the element is present in the original array we use that element, otherwise we use 0 at that place. For example: Input → [5, 7, 9] Output → [0, 0, 0, 0, 5, 0, 7, 0, 9, 0, 0, 0] Now, let's write the code: Using Array.includes() Method const months = [6, ...
Read MoreGroup array by equal values JavaScript
Let's say, we have an array of string/number literals that contains some duplicate values like this: const array = ['day', 'night', 'afternoon', 'night', 'noon', 'night', 'noon', 'day', 'afternoon', 'day', 'night']; We are required to write a function groupSimilar() that takes in this array and returns a new array where all the repeating entries are grouped together in a subarray as the first element and their total count in the original array as the second element. So, for this example, the output should be: [ [ 'day', 3 ], ...
Read More