Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Web Development Articles
Page 461 of 801
Reduce an array to the sum of every nth element - JavaScript
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 MoreHow to compare two string arrays, case insensitive and independent about ordering JavaScript, ES6
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 MoreProgram to append two given strings such that, if the concatenation creates a double character then omit one of the characters - JavaScript
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 MoreFind Max Slice Of Array | JavaScript
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 MorePrime numbers in a range - JavaScript
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 MoreRemove duplicates from a array of objects JavaScript
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 MoreComparing forEach() and reduce() for summing an array of numbers in JavaScript.
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 MoreIf string includes words in array, remove them JavaScript
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 MoreSumming numbers from a string - JavaScript
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 MoreCompress array to group consecutive elements JavaScript
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