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
Javascript Articles
Page 451 of 534
How 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 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 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 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 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 MoreHow to calculate total time between a list of entries?
Let's say, we have an array that contains some data about the speed of a motor boat during upstreams and downstreams like this − Following is our sample array − const arr = [{ direction: 'upstream', velocity: 45 }, { direction: 'downstream', velocity: 15 }, { direction: 'downstream', velocity: 50 }, { direction: 'upstream', velocity: 35 }, { direction: 'downstream', velocity: 25 }, { ...
Read MoreHow to write a JavaScript function that returns true if a portion of string 1 can be rearranged to string 2?
We need to write a function that checks if the characters from one string can be rearranged to form another string. This is essentially checking if one string contains all the characters needed to build the second string. The function scramble(str1, str2) should return true if characters from str1 can be rearranged to match str2, otherwise false. Problem Examples str1 = 'cashwool', str2 = 'school' → true (cashwool contains: c, a, s, h, w, o, o, l - enough to make 'school') str1 = 'katas', str2 = 'steak' → false (katas missing 'e' ...
Read MorePrefix sums (Creating an array with increasing sum) with Recursion in JavaScript
Consider the following array of numbers: const arr = [10, 5, 6, 12, 7, 1]; The sum of its consecutive elements taking one less element in every go will be: [10, 5, 6, 12, 7, 1] = 10 + 5 + 6 + 12 + 7 + 1 = 41; [5, 6, 12, 7, 1] = 5 + 6 + 12 + 7 + 1 = 31; [6, 12, 7, 1] = 6 + 12 + 7 + 1 = 26; [12, 7, 1] = 12 + 7 + 1 = 20; [7, 1] ...
Read MoreDetermining happy numbers using recursion JavaScript
A happy number is a number which eventually reaches 1 when replaced by the sum of the square of each digit. Whereas if during this process any number gets repeated, the cycle will run infinitely and such numbers are called unhappy numbers. For example − 13 is a happy number because, 1^2 + 3^2 = 10 and, 1^2 + 0^2 = 1 On the other hand, 36 is an unhappy number. We are required to write a function that uses recursion to determine whether or not a number is a happy number. Algorithm ...
Read MoreJavaScript Sleep() function?
JavaScript doesn't have a built-in sleep() function like other languages (C's sleep(), Java's Thread.sleep(), Python's time.sleep()). However, we can create one using Promises and async/await. Creating a Sleep Function We can implement a sleep function using setTimeout() wrapped in a Promise: function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } Using Sleep with Promises function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } console.log("Start"); sleep(2000).then(() => { console.log("After 2 seconds"); }); Start ...
Read More