Web Development Articles

Page 188 of 801

How to merge two strings alternatively in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

In JavaScript, merging two strings alternatively means combining characters from both strings one by one. This creates a new string where characters alternate between the first and second input strings. For example, if we have two strings: const str1 = 'abc'; const str2 = 'def'; console.log('String 1:', str1); console.log('String 2:', str2); String 1: abc String 2: def The expected output should be: adbecf Using Loop-Based Approach Here's a function that merges two strings alternatively by iterating through both strings simultaneously: const str1 = 'abc'; const ...

Read More

Checking for centrally peaked arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 157 Views

We are required to write a JavaScript function that takes in an array of numbers, arr, as the first and the only argument. Our function should check whether the input array is a centrally peaked array or not. If it is a centrally peaked array, we should return true, false otherwise. Conditions for Centrally Peaked Array The conditions for being a centrally peaked array are: arr.length >= 3 There exists some i with 0 < i < arr.length - 1 such that: ...

Read More

Removing duplicate objects from array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 358 Views

Removing duplicate objects from arrays is a common requirement in JavaScript applications. Let's explore different approaches to eliminate duplicate objects while preserving the original data structure. The Problem Consider an array containing duplicate objects: const arr = [ {"title": "Assistant"}, {"month": "July"}, {"event": "Holiday"}, {"title": "Assistant"} ]; console.log("Original array:", arr); Original array: [ { title: 'Assistant' }, { month: 'July' }, { event: 'Holiday' }, { title: 'Assistant' ...

Read More

Checking for a Doubleton Number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 365 Views

A "doubleton number" is a natural number that contains exactly two distinct digits. For example, 23, 35, 100, and 12121 are doubleton numbers because they each use only two different digits. Numbers like 123 (three digits) and 9980 (three digits: 9, 8, 0) are not doubleton numbers. Problem Statement We need to write a JavaScript function that takes a number and returns true if it's a doubleton number, false otherwise. Solution The approach is to convert the number to a string, track unique digits using an object, and check if exactly two distinct digits exist. ...

Read More

Sort an array according to another array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 424 Views

When working with arrays in JavaScript, you may need to sort one array based on the order of elements in another array. This is useful when you have a reference array that defines the desired ordering. Suppose we have two arrays like these: const arr1 = [1, 3, 2, 4, 5, 6]; const arr2 = [1, 2, 5]; console.log("Original arr1:", arr1); console.log("Reference arr2:", arr2); Original arr1: [ 1, 3, 2, 4, 5, 6 ] Reference arr2: [ 1, 2, 5 ] We want to sort arr1 so that elements appearing in ...

Read More

Converting number of corresponding string without using library function in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 401 Views

Converting a number to a string without using built-in functions like String() or toString() requires extracting individual digits and building the result character by character. Problem We need to write a JavaScript function that takes a number and converts it to its corresponding string representation without using the built-in functions String() or toString() or direct string concatenation. Approach The solution extracts digits from right to left using the modulo operator (%) and integer division. Each digit is converted to its character representation and prepended to build the final string. Example const num = ...

Read More

How to filter out common array in array of arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 311 Views

When working with arrays of arrays in JavaScript, you may need to remove duplicate subarrays and keep only unique ones. This is useful for data deduplication and filtering operations. The Problem Suppose we have an array of arrays with duplicate subarrays: const arr = [ [ "Serta", "Black Friday" ], [ "Serta", ...

Read More

Converting miles per gallon to kilometer per liter in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 624 Views

Converting miles per gallon (MPG) to kilometers per liter (km/L) is a common unit conversion in JavaScript applications dealing with fuel efficiency across different measurement systems. Understanding the Conversion To convert MPG to km/L, we need two conversion factors: 1 mile = 1.609344 kilometers 1 gallon = 4.54609188 liters (Imperial gallon) The conversion formula is: km/L = MPG × (1.609344 ÷ 4.54609188) Example Implementation const num = 25; const converter = (mpg) => { let LITRES_PER_GALLON = 4.54609188; let KILOMETERS_PER_MILE = 1.609344; ...

Read More

Combine two different arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 470 Views

Suppose we have two arrays, the first array contains the scheduled date for some events and the second array contains the names of those events, like this − const dates = [ { id: "1", date: "2017-11-07" }, { id: "1", date: "2017-11-08" }, { ...

Read More

Center of each side of a polygon in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 247 Views

In JavaScript, finding the center (midpoint) of each side of a polygon involves calculating the average coordinates between consecutive vertices. This is useful in computational geometry for polygon analysis and graphics applications. Problem Definition Given an array of coordinate pairs representing polygon vertices, we need to find the midpoint of each side. Each side connects two consecutive vertices, and the last vertex connects back to the first vertex to close the polygon. // Example polygon coordinates (longitude, latitude) const polygonVertices = [ [-73.9280684530257, 40.8099975343718], [-73.9282820374729, 40.8100875554645], ...

Read More
Showing 1871–1880 of 8,010 articles
« Prev 1 186 187 188 189 190 801 Next »
Advertisements