Web Development Articles

Page 461 of 801

Reduce an array to the sum of every nth element - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 329 Views

We are required to write a JavaScript function that takes in an array of numbers and returns the cumulative sum of every number present at the index that is a multiple of n from the array. Let's write the code for this function − Example const arr = [1, 4, 5, 3, 5, 6, 12, 5, 65, 3, 2, 65, 9]; const num = 2; const nthSum = (arr, num) => { let sum = 0; for(let i = 0; i < arr.length; i++){ ...

Read More

How to compare two string arrays, case insensitive and independent about ordering JavaScript, ES6

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 855 Views

We need to write a function that compares two strings to check if they contain the same characters, ignoring case and order. This is useful for validating anagrams or checking string equivalence. For example: const first = 'Aavsg'; const second = 'VSAAg'; isEqual(first, second); // true Using Array Sort Method This method converts strings to arrays, sorts the characters, and compares the results. We extend the String prototype to add a sort method. const first = 'Aavsg'; const second = 'VSAAg'; const stringSort = function(){ return this.split("").sort().join(""); ...

Read More

Program to append two given strings such that, if the concatenation creates a double character then omit one of the characters - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 238 Views

We are required to write a JavaScript function that takes in two strings and concatenates the second string to the first string. If the last character of the first string and the first character of the second string are the same then we have to omit one of those characters. Let's say the following are our strings in JavaScript − Problem Example const str1 = 'Food'; const str2 = 'dog'; // Expected output: 'Foodog' (last 'd' of 'Food' matches first 'd' of 'dog') Solution Let's write the code for this function − ...

Read More

Find Max Slice Of Array | JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 279 Views

In JavaScript, finding the maximum slice of an array with at most two different numbers is a common problem that can be efficiently solved using the sliding window technique. This approach maintains a dynamic window that expands and contracts to find the longest contiguous subarray containing no more than two distinct elements. How It Works The sliding window algorithm uses two pointers (start and end) to define a window boundary. We track distinct elements using a map and adjust the window size when the distinct count exceeds two. Example const arr = [1, 1, 1, ...

Read More

Prime numbers in a range - JavaScript

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

We are required to write a JavaScript function that takes in two numbers, say, a and b and returns the total number of prime numbers between a and b (including a and b, if they are prime). For example − If a = 2, and b = 21, the prime numbers between them are 2, 3, 5, 7, 11, 13, 17, 19 And their count is 8. Our function should return 8. Understanding Prime Numbers A prime number is a natural number greater than 1 that has no positive divisors other than 1 ...

Read More

Remove duplicates from a array of objects JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 724 Views

Removing duplicates from an array of objects is a common task in JavaScript. We need to identify objects with the same properties and values, keeping only unique objects in the result. Using JSON.stringify() with Map The most straightforward approach uses JSON.stringify() to convert objects to strings for comparison: const arr = [ { "timestamp": 564328370007, "message": "It will rain today" }, { ...

Read More

Comparing forEach() and reduce() for summing an array of numbers in JavaScript.

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 498 Views

When summing arrays in JavaScript, both forEach() and reduce() are common approaches. This article compares their performance to help you choose the right method. Since we can't demonstrate with truly massive arrays here, we'll simulate the performance impact by running the summing operation many times in a loop. The Two Approaches Here's how each method works for summing an array: const arr = [1, 4, 4, 54, 56, 54, 2, 23, 6, 54, 65, 65]; // Using reduce() - functional approach const reduceSum = arr => arr.reduce((acc, val) => acc + val); // ...

Read More

If string includes words in array, remove them JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 853 Views

In JavaScript, you may need to remove specific words from a string based on an array of target words. This is useful for content filtering, text cleaning, or removing unwanted terms from user input. Problem Overview We need to create a function that removes all occurrences of words present in an array from a given string, while handling whitespace properly to avoid multiple consecutive spaces. Method 1: Using reduce() with Regular Expressions const string = "The weather in Delhi today is very similar to the weather in Mumbai"; const words = [ ...

Read More

Summing numbers from a string - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 996 Views

We are required to write a JavaScript function that takes in a string that contains some one-digit numbers in between and the function should return the sum of all the numbers present in the string. Let's say the following is our string with numbers: const str = 'gdf5jhhj3hbj4hbj3jbb4bbjj3jb5bjjb5bj3'; Method 1: Using for Loop with Type Conversion This approach splits the string into characters and checks each one using type conversion: const str = 'gdf5jhhj3hbj4hbj3jbb4bbjj3jb5bjjb5bj3'; const sumStringNum = str => { const strArr = str.split(""); let ...

Read More

Compress array to group consecutive elements JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 446 Views

We are given a string that contains some repeating words separated by dash (-) like this: const str = 'monday-sunday-tuesday-tuesday-sunday-sunday-monday-monday-monday'; console.log(str); monday-sunday-tuesday-tuesday-sunday-sunday-monday-monday-monday Our job is to write a function that returns an array of objects, where each object contains two properties: val (the word) and count (their consecutive appearance count). Expected Output Format For the above string, the compressed array should look like this: const arr = [{ val: 'monday', count: 1 }, { val: 'sunday', ...

Read More
Showing 4601–4610 of 8,010 articles
« Prev 1 459 460 461 462 463 801 Next »
Advertisements