AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 324 of 840

JavaScript Count the number of unique elements in an array of objects by an object property

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

In JavaScript, when dealing with arrays of objects, it's common to need to extract specific values based on object properties and count how many unique occurrences exist for a particular property. Whether you're processing a list of users, products, or any other data structure, counting unique elements by a property is a useful operation. In this article, we'll discuss how to count the number of unique elements in an array of objects based on a specific property using a few different techniques. Problem Statement You have an array of objects representing users, each with properties like name, ...

Read More

How to call a function repeatedly every 5 seconds in JavaScript?

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

In JavaScript, setInterval() allows you to execute a function repeatedly at specified time intervals. To call a function every 5 seconds, you pass the function and 5000 milliseconds as arguments. Syntax setInterval(function, delay); Where function is the function to execute and delay is the time in milliseconds between executions. Basic Example function myFunction() { console.log("Function called at:", new Date().toLocaleTimeString()); } // Call myFunction every 5 seconds (5000 milliseconds) setInterval(myFunction, 5000); Function called at: 2:30:15 PM Function called at: 2:30:20 PM Function called at: 2:30:25 ...

Read More

Pushing positives and negatives to separate arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 452 Views

We are required to write a function that takes in an array and returns an object with two arrays positive and negative. They both should be containing all positive and negative items respectively from the array. We will be using the Array.prototype.reduce() method to pick desired elements and put them into an object of two arrays. Using Array.reduce() Method The code for this will be − const arr = [97, -108, 13, -12, 133, -887, 32, -15, 33, -77]; const splitArray = (arr) => { return arr.reduce((acc, val) => { ...

Read More

Grade book challenge JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 365 Views

We are required to write a function that finds the mean of the three scores passed to it and returns the letter value associated with that grade according to the following table. Grade Scale Score Range Grade 90-100 A 80-89 B ...

Read More

Finding the k-prime numbers with a specific distance in a range in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 486 Views

K-Prime Numbers A natural number is called k-prime if it has exactly k prime factors, counted with multiplicity. For example, 4 is a 2-prime number because 4 = 2 × 2, and both instances of 2 are counted separately. Similarly, 8 is a 3-prime number because 8 = 2 × 2 × 2, giving us three prime factors. Problem We need to write a JavaScript function that takes a number k, a distance, and a range. The function should return an array of pairs containing k-prime numbers within the range where the distance between them equals ...

Read More

Sorting only a part of an array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 926 Views

We are required to write a JavaScript function that takes in an array of strings as the first argument and two numbers as second and third argument respectively. The purpose of our function is to sort the array. But we have to sort only that part of the array that falls between the start and end indices specified by second and third argument. Keeping all the other elements unchanged. For example: const arr = ['z', 'b', 'a']; sortBetween(arr, 0, 1); This function should sort the elements at 0 and 1 index only. And the ...

Read More

Converting decimal to binary or hex based on a condition in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 251 Views

Problem We need to write a JavaScript function that takes in a number n and converts it based on a condition: If a number is even, convert it to binary. If a number is odd, convert it to hex. Solution We can use JavaScript's toString() method with different radix values to perform the conversion. For binary conversion, we use radix 2, and for hexadecimal conversion, we use radix 16. Example Here's the implementation: const num = 1457; const conditionalConvert = (num = 1) ...

Read More

Finding score of brackets in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 270 Views

We are required to write a JavaScript function that takes in a balanced square bracket string as an argument and computes its score based on specific rules. Scoring Rules The bracket scoring follows these rules: [] has score 1 AB has score A + B, where A and B are balanced bracket strings [A] has score 2 * A, where A is a balanced bracket string Example Input and Output For the input string '[][]': Input: '[][]' Output: 2 This works because [] scores 1, and [][] is two ...

Read More

How to call a function that returns another function in JavaScript?

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

In JavaScript, calling a function that returns another function involves understanding higher-order functions and closures. When a function returns another function, you can either store the returned function in a variable or call it immediately using double parentheses. Basic Syntax There are two main ways to call a function that returns another function: // Method 1: Store in variable then call let returnedFunction = outerFunction(); returnedFunction(); // Method 2: Call immediately outerFunction()(); Simple Example let outerFunction = function() { ...

Read More

How to transform two or more spaces in a string in only one space? JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 365 Views

In JavaScript, you can transform multiple consecutive spaces in a string into a single space using regular expressions with the replace() method. This is useful for cleaning up text input or formatting strings consistently. The Regular Expression Approach The most effective method uses the regular expression /\s{2, }/g where: \s matches any whitespace character (spaces, tabs, newlines) {2, } matches two or more consecutive occurrences g is the global flag to replace all instances Method 1: Using replace() with Regular Expression ...

Read More
Showing 3231–3240 of 8,392 articles
« Prev 1 322 323 324 325 326 840 Next »
Advertisements