AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 467 of 840

Array sum: Comparing recursion vs for loop vs ES6 methods in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 331 Views

When working with arrays in JavaScript, there are multiple ways to calculate the sum of all elements. Let's compare three popular approaches: recursion, traditional for loops, and ES6 methods like reduce(). To demonstrate performance differences, we'll test each method with a large number of iterations. This gives us insights into which approach performs best for array summation tasks. Recursive Approach The recursive method calls itself until it processes all array elements: const recursiveSum = (arr, len = 0, sum = 0) => { if(len < arr.length){ ...

Read More

Check if a string has white space in JavaScript?

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

To check if a string contains whitespace in JavaScript, you can use several methods. The most common approaches are indexOf(), includes(), and regular expressions. Using indexOf() Method The indexOf() method returns the index of the first whitespace character, or -1 if none is found: function stringHasTheWhiteSpaceOrNot(value){ return value.indexOf(' ') >= 0; } var whiteSpace = stringHasTheWhiteSpaceOrNot("MyNameis John"); if(whiteSpace == true){ console.log("The string has whitespace"); } else { console.log("The string does not have whitespace"); } // Test with different strings console.log(stringHasTheWhiteSpaceOrNot("HelloWorld")); ...

Read More

Count groups of negatives numbers in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 461 Views

We have an array of numbers like this: const arr = [-1, -2, -1, 0, -1, -2, -1, -2, -1, 0, 1, 0]; Let's say, we are required to write a JavaScript function that counts the consecutive groups of negative numbers in the array. Like here we have consecutive negatives from index 0 to 2 which forms one group and then from 4 to 8 forms the second group. So, for this array, the function should return 2. Understanding the Problem A group of consecutive negative numbers is defined as: One ...

Read More

Continuing a loop in functional programming JavaScript.

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 177 Views

In functional programming JavaScript, there's no direct equivalent to the continue statement used in traditional loops. However, you can achieve similar behavior using array methods like filter(), forEach(), and some(). The Problem with Traditional Continue Traditional for loops use continue to skip iterations, but functional programming relies on array methods that don't support continue directly. Method 1: Using filter() and forEach() The most functional approach is to filter out unwanted elements first, then process the remaining items: Functional Loop Continue body { ...

Read More

Compare two objects in JavaScript and return a number between 0 and 100 representing the percentage of similarity

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 626 Views

When comparing objects in JavaScript, we often need to determine their similarity as a percentage. This is useful for data matching, filtering, or recommendation systems. Problem Overview Given two objects, we need to calculate their similarity percentage based on matching key-value pairs. The similarity is calculated by dividing the count of matching properties by the total properties in the smaller object. const a = { Make: "Apple", Model: "iPad", hasScreen: "yes", Review: "Great product!", }; const b = { ...

Read More

Matching strings for similar characters - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 675 Views

We are required to write a JavaScript function that accepts two strings and a number n. The function matches the two strings i.e., it checks if the two strings contains the same characters. The function returns true if both the strings contain the same character irrespective of their order or if they contain at most n different characters, else the function should return false. Example Following is the code − const str = 'some random text'; const str2 = 'some r@ndom text'; const deviationMatching = (first, second, num) => { ...

Read More

Creating 'Copy to Clipboard' feature on a web page with JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 255 Views

The copy to clipboard feature allows users to easily copy text from input fields or other elements on a webpage. This functionality is essential for improving user experience in web applications. Modern Approach Using Clipboard API The modern way to implement copy to clipboard uses the Clipboard API, which is more secure and reliable than the deprecated execCommand method. Copy to Clipboard body { ...

Read More

How to multiply odd index values JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 432 Views

We are required to write a function that takes in an array of Number literals as one and the only argument. The numbers that are situated at even index should be returned as it is. But the numbers situated at the odd index should be returned multiplied by their corresponding indices. For example: If the input is: [5, 10, 15, 20, 25, 30, 50, 100] Then the function should return: [5, 10, 15, 60, 25, 150, 50, 700] Understanding the Logic Let's break down what happens at each ...

Read More

Solve the Sherlock and Array problem in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 740 Views

Watson gives Sherlock an array A of length N. Then he asks him to determine if there exists an element in the array such that the sum of the elements on its left is equal to the sum of the elements on its right. We have to write this function, it should take in an array of Numbers, and any such number exists in the array, it should return its index, otherwise it should return -1. So, let's write the code for this function. Algorithm Approach The efficient approach is to: Calculate the total sum of ...

Read More

Counting elements of an array using a recursive function in JS?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 675 Views

A recursive function calls itself until a base condition is met. In JavaScript, we can use recursion to count array elements by reducing the array size in each recursive call. How Recursive Counting Works The recursive approach works by: Base case: If array is empty, return 0 Recursive case: Return 1 + count of remaining elements Example function countNumberOfElementsUsingRecursive(listOfMarks) { if (listOfMarks.length == 0) { return 0; } return 1 + countNumberOfElementsUsingRecursive(listOfMarks.slice(1)); } ...

Read More
Showing 4661–4670 of 8,392 articles
« Prev 1 465 466 467 468 469 840 Next »
Advertisements