Front End Technology Articles

Page 187 of 652

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

Converting number of corresponding string without using library function in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 406 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

Removing duplicate objects from array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 366 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

Converting miles per gallon to kilometer per liter in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 640 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

Sort an array according to another array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 436 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

Finding sum of all numbers within a range in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 839 Views

Problem We are required to write a JavaScript function that takes in an array that specifies a range. Our function should find and return the sum of all the natural numbers falling in the range including the range numbers. Example Following is the code − const range = [4, 67]; const findSum = ([l, h]) => { let sum = 0; for(let i = l; i { // Formula: Sum = n * (first + last) / 2 // where ...

Read More

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

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 317 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

Building a lexicographically increasing sequence of first n natural numbers in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 187 Views

We are required to write a JavaScript function that takes in a number n and returns an array containing the first n natural numbers. The condition is that the numbers should be sorted lexicographically, which means all numbers starting with 1 should come before any starting with 2, 3, 4, and so on. Understanding Lexicographical Ordering Lexicographical ordering treats numbers as strings. For example, "10" comes before "2" because "1" comes before "2" alphabetically. This creates the sequence: 1, 10, 11, 12, ..., 19, 2, 20, 21, ..., etc. Example Implementation const num = 24; ...

Read More

Combine two different arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 480 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 254 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 1861–1870 of 6,519 articles
« Prev 1 185 186 187 188 189 652 Next »
Advertisements