Front End Technology Articles

Page 212 of 652

Sorting and find sum of differences for an array using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 418 Views

Problem We need to write a JavaScript function that takes an array of integers, sorts them in descending order, and then calculates the sum of differences between consecutive pairs. For example, if the array is: [6, 2, 15] After sorting in descending order: [15, 6, 2] The sum of differences would be: (15 - 6) + (6 - 2) = 9 + 4 = 13 Solution Here's the implementation that sorts the array and calculates the sum of consecutive differences: const arr = [6, 2, 15]; ...

Read More

Finding the smallest value in a JSON object in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 925 Views

Finding the smallest value in a JSON object is a common task in JavaScript. This involves traversing through an object's properties and comparing their numeric values to determine the minimum. When working with objects that contain numeric values, we need to iterate through all properties and find the one with the smallest value. JavaScript provides several approaches to accomplish this efficiently. Example Here's how to find the smallest value using the reduce() method: const obj = { "a": 4, "b": 2, "c": 5, ...

Read More

Changing second half of string number digits to zero using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 249 Views

Problem We are required to write a JavaScript function that takes in a string number as the only argument. Our function should return the input number with the second half of digits changed to 0. In cases where the number has an odd number of digits, the middle digit onwards should be changed to 0. For example: 938473 → 938000 How It Works The algorithm works by calculating the midpoint of the string. For even-length strings, exactly half the digits are preserved. For odd-length strings, the middle digit becomes part of ...

Read More

How to turn a JSON object into a JavaScript array in JavaScript ?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 671 Views

Converting a JSON object with numeric string keys into a JavaScript array is a common operation. This is particularly useful when dealing with API responses or data structures that use indexed properties. The Problem Consider this JSON object where index keys are mapped to string values: const obj = { "0": "Rakesh", "1": "Dinesh", "2": "Mohit", "3": "Rajan", "4": "Ashish" }; console.log(typeof obj); // "object" object Method 1: Using Object.keys() and forEach() This approach iterates through object keys and maps ...

Read More

Accumulating array elements to form new array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 338 Views

We are required to write a JavaScript function that takes in an array of numbers, arr, as the first argument and a number, num, (num ≤ length of array) as the second argument. Our function should add up each contiguous subarray of length num of the array arr to form corresponding elements of new array and finally return that new array. Problem Example For example, if the input to the function is: const arr = [1, 2, 3, 4, 5, 6]; const num = 2; Then the output should be: [3, ...

Read More

How to form a two-dimensional array with given width (columns) and height (rows) in JavaScript ?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 461 Views

We are required to write a JavaScript function that takes in three arguments: height --> no. of rows of the array width --> no. of columns of the array val --> initial value of each element of the array Then the function should return the new array formed based on these criteria. Method 1: Using Array.apply() with map() This approach creates arrays using Array.apply() and fills them with map(): const rows = 4, cols = 5, val = 'Example'; const fillArray = (width, height, value) => { ...

Read More

Checking for special numbers in JavaScript

Ranjit Kumar
Ranjit Kumar
Updated on 15-Mar-2026 579 Views

In JavaScript, checking for special numbers often involves mathematical operations on digits. A palindrome digit sum check determines if the sum of a number's digits forms a palindrome. Problem We need to write a JavaScript function that takes a number and returns true if the sum of its digits is a palindrome number, false otherwise. For example, with input 781296: const num = 781296; The expected output is: true Output Explanation The digit sum of 781296 is 7+8+1+2+9+6 = 33, which reads the same forwards and backwards (palindrome). ...

Read More

Calculating the LCM of multiple numbers in JavaScript

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

We are required to write a JavaScript function that takes in an array of numbers of any length and returns their LCM (Least Common Multiple). We will approach this problem in parts: Part 1 − We will create a helper function to calculate the Greatest Common Divisor (GCD) of two numbers Part 2 − Then using Part 1 helper function we will create another helper function to calculate the Least Common Multiple (LCM) of two numbers. Part 3 − Finally, using Part 2 helper function we will create a function that loops over the array and ...

Read More

Merging nested arrays to form 1-d array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 389 Views

We are required to write a JavaScript function that takes in two nested arrays, arr1 and arr2, as the first and the second argument. Our function should create and return a third array that contains all the elements of arr1 and arr2 but flattened to single dimension For example, if the input to the function is — const arr1 = [ 1, [ 2, [ 4, 5, [ ...

Read More

Sorting an array by date in JavaScript

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

Sorting an array by date in JavaScript is a common task when working with arrays of objects containing date information. This article shows how to sort objects by their date properties using JavaScript's built-in sorting methods. The Problem Suppose we have an array of objects like this: const arr = [ {id: 1, date: 'Mar 12 2012 10:00:00 AM'}, {id: 2, date: 'Mar 8 2012 08:00:00 AM'} ]; We need to sort this array according to the date property of each object, either newest first ...

Read More
Showing 2111–2120 of 6,519 articles
« Prev 1 210 211 212 213 214 652 Next »
Advertisements