Javascript Articles

Page 451 of 534

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

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 847 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

Find Max Slice Of Array | JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 275 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

Remove duplicates from a array of objects JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 717 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

If string includes words in array, remove them JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 846 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

Compress array to group consecutive elements JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 439 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

How to calculate total time between a list of entries?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 220 Views

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 More

How to write a JavaScript function that returns true if a portion of string 1 can be rearranged to string 2?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 407 Views

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 More

Prefix sums (Creating an array with increasing sum) with Recursion in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 446 Views

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 More

Determining happy numbers using recursion JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 836 Views

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 More

JavaScript Sleep() function?

vineeth.mariserla
vineeth.mariserla
Updated on 15-Mar-2026 53K+ Views

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
Showing 4501–4510 of 5,340 articles
« Prev 1 449 450 451 452 453 534 Next »
Advertisements