Web Development Articles

Page 478 of 801

Inserting element at falsy index in an array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 246 Views

We are required to write an Array function, let's say, pushAtFalsy() The function should take in an array and an element. It should insert the element at the first falsy index it finds in the array. If there are no empty spaces, the element should be inserted at the last of the array. We will first search for the index of empty position and then replace the value there with the value we are provided with. Understanding Falsy Values In JavaScript, falsy values include: null, undefined, false, 0, "" (empty string), and NaN. However, since 0 ...

Read More

Split string into equal parts JavaScript

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

In JavaScript, splitting a string into equal parts can be accomplished in several ways. This article demonstrates how to split a string into n equal parts using an alternating pattern that takes characters from both ends of the string. Problem Statement We need to write a JavaScript function that takes a string and a number n as arguments, where n exactly divides the string length. The function should return an array of n strings of equal length, formed by alternating between the first and last characters of the remaining string. For example: If the string ...

Read More

Array of adjacent element's average - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 324 Views

Let's say, we have an array of numbers: const arr = [3, 5, 7, 8, 3, 5, 7, 4, 2, 8, 4, 2, 1]; We are required to write a function that returns an array with the average of the corresponding element and its predecessor. For the first element, as there are no predecessors, so that very element should be returned. Let's write the code for this function, we will use the Array.prototype.map() function to solve this problem: Example const arr = [3, 5, 7, 8, 3, 5, 7, 4, 2, 8, ...

Read More

JavaScript R- eturn Array Item(s) With Largest Score

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 204 Views

We have an array of arrays that contains the marks scored by some students in different subjects. We need to write a function that returns the top scorer(s) for each subject, handling cases where multiple students have the same highest score. Problem Setup Given this input data: const arr = [ ['Math', 'John', 100], ['Math', 'Jake', 89], ['Math', 'Amy', 93], ['Science', 'Jake', 89], ['Science', 'John', 89], ['Science', 'Amy', 83], ...

Read More

Build maximum array based on a 2-D array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 168 Views

Let's say, we have an array of arrays of Numbers like below − const arr = [ [1, 16, 34, 48], [6, 66, 2, 98], [43, 8, 65, 43], [32, 98, 76, 83], [65, 89, 32, 4], ]; We are required to write a function that maps over this array of arrays and returns an array that contains the maximum (greatest) element from each subarray. So, for the above array, the output should be − [48, 98, 65, 98, 89] Using ...

Read More

Rearrange string so that same character become n distance apart JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 326 Views

We need to rearrange a string so that identical characters are exactly n positions apart from each other. This problem requires careful character placement to maintain the specified distance constraint. For example, if we have the string "accessories" and n = 3, we need to ensure that each 's' character is exactly 3 positions away from other 's' characters, and the same applies to other repeated characters. Problem Understanding The algorithm works by: Counting the frequency of each character Sorting characters by frequency (most frequent first) Placing characters in a round-robin fashion with n distance apart ...

Read More

Removing identical entries from an array keeping its length same - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 193 Views

We have to write a function that takes in an array, removes all duplicates from it and inserts the same number of empty strings at the end. For example − If we find four duplicate values, we have to remove them all and insert four empty strings at the end. Understanding the Problem The goal is to: Remove duplicate elements from the array Keep only the last occurrence of each element Maintain the original array length by adding empty strings Example ...

Read More

Object difference in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 187 Views

In JavaScript, finding the difference between two objects means identifying keys that exist in the first object but not in the second. This is useful for comparing data structures, tracking changes, or filtering object properties. Problem Statement We need to write a JavaScript function that takes two objects (possibly nested) and returns a new object containing only the key-value pairs that exist in the first object but are missing from the second object. Basic Implementation Here's a function that compares two objects and returns the difference: const obj1 = { "firstName": ...

Read More

Remove elements from array using JavaScript filter - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 501 Views

Suppose, we have two arrays of literals like these − const arr1 = [4, 23, 7, 6, 3, 6, 4, 3, 56, 4]; const arr2 = [4, 56, 23]; We are required to write a JavaScript function that takes in these two arrays and filters the first to contain only those elements that are not present in the second array. And then return the filtered array to get the below output − const output = [7, 6, 3, 6, 3]; Method 1: Using filter() with indexOf() The filter() method creates ...

Read More

Search and update array based on key JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 240 Views

In JavaScript, you often need to merge data from two arrays based on a common property. This is useful when you have reference data in one array and need to enrich objects in another array. Consider these two arrays: let arr1 = [ {"LEVEL":4, "POSITION":"RGM"}, {"LEVEL":5, "POSITION":"GM"}, {"LEVEL":5, "POSITION":"GMH"} ]; let arr2 = [ {"EMAIL":"test1@stc.com", "POSITION":"GM"}, {"EMAIL":"test2@stc.com", "POSITION":"GMH"}, {"EMAIL":"test3@stc.com", "POSITION":"RGM"}, {"EMAIL":"test3@CSR.COM.AU", "POSITION":"GM"} ]; console.log("Original arr2:", arr2); ...

Read More
Showing 4771–4780 of 8,010 articles
« Prev 1 476 477 478 479 480 801 Next »
Advertisements