AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 361 of 840

Are mean mode of a dataset equal in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 229 Views

We are required to write a JavaScript function that takes in an array of numbers and checks if the mean and mode are equal. The function should calculate both statistical measures and return true if they match, false otherwise. For example − If the input array is − const arr = [5, 3, 3, 3, 1]; Then the output for this array should be true because both the mean and mode of this array are 3. Understanding Mean and Mode The mean is the average of all numbers in the dataset. The ...

Read More

Can all array elements mesh together in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 194 Views

Two words can mesh together if the ending substring of the first word is the starting substring of the second word. For instance, "robinhood" and "hoodie" can mesh together because "hood" appears at the end of "robinhood" and at the start of "hoodie". We need to write a JavaScript function that takes an array of strings and checks if all consecutive words mesh together. If they do, the function returns the meshed letters as a string, otherwise it returns an empty string. How It Works The solution uses a regular expression to find overlapping substrings between consecutive ...

Read More

Summing cubes of natural numbers within a range in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 222 Views

Problem We are required to write a JavaScript function that takes in a range array of two numbers. Our function should find the sum of all the cubes of the numbers that falls in the specified range. Understanding the Problem Given a range [a, b], we need to calculate a³ + (a+1)³ + (a+2)³ + ... + b³. For example, with range [4, 11], we calculate 4³ + 5³ + 6³ + 7³ + 8³ + 9³ + 10³ + 11³. Solution Using For Loop Here's the implementation that iterates through the range and sums the cubes: const range = [4, 11]; const sumCubes = ([l, h]) => { const findCube = num => num * num * num; let sum = 0; for(let i = l; i

Read More

Difference between sum and product of an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 271 Views

We are required to write a JavaScript function that takes in an array of Numbers as the only argument. The function should calculate the sum of all numbers in the array and the product of all numbers. Then the function should return the absolute difference between the sum and the product. Example Following is the code − const arr = [1, 4, 1, 2, 1, 6, 3]; const sumProductDifference = (arr = []) => { const creds = arr.reduce((acc, val) => { let { ...

Read More

Implementing a custom function like Array.prototype.filter() function in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 986 Views

In JavaScript, the Array.prototype.filter() method creates a new array with elements that pass a test function. Let's implement a custom version to understand how it works internally. Problem We need to create a custom function that mimics Array.prototype.filter(). The function should: Live on the Array prototype Accept a callback function as an argument Call the callback for each array element with the element and its index Include elements in the result array only when the callback returns true Implementation Here's how we can implement our custom filter function: const arr = ...

Read More

Merge two objects in JavaScript ignoring undefined values

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

When merging two objects in JavaScript, you may want to ignore undefined values to avoid overwriting valid data. The spread operator alone doesn't handle this logic, so we need a custom approach. The Problem Consider these two objects where some properties have undefined values: const A = { activity: 'purchased', count: undefined, time: '09:05:33' }; const B = { activity: 'purchased', count: '51', time: undefined }; // Simple spread operator overwrites values const simpleSpread = { ...A, ...B }; console.log("Simple spread:", simpleSpread); Simple spread: { activity: 'purchased', count: '51', time: undefined } ...

Read More

Join two objects by key in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 898 Views

When working with related data structures, you often need to join objects from different arrays based on matching keys. This is common when dealing with parent-child relationships or normalized data. Suppose we have two arrays - one containing child objects and another containing parent objects: const child = [{ id: 1, name: 'somename', parent: { id: 2 }, }, { id: 2, name: 'some child ...

Read More

Flattening a deeply nested array of literals in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 428 Views

We are required to write a JavaScript function that takes in a nested array of literals as the only argument. The function should construct a new array that contains all the literal elements present in the input array but without nesting. For example − If the input array is − const arr = [ 1, 3, [5, 6, [7, [6, 5], 4], 3], [4] ]; Then the output array should be − [1, 3, 5, 6, 7, 6, 5, 4, 3, 4] Method 1: Recursive Approach ...

Read More

Finding the maximum number using at most one swap in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 295 Views

We are required to write a JavaScript function that takes in a number as the first and the only argument. The task of our function is to perform at most one swap between any two digits of the number and yield the maximum possible number. If the number is already the maximum possible number, we should return the number itself. For example, if the input number is: const num = 1625; Then the output should be: const output = 6125; We swapped 1 and 6, and this is the only ...

Read More

Counting number of triangle sides in an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 291 Views

Problem We are required to write a JavaScript function that takes in an array of numbers, arr, as the first and the only argument. The task of our function is to count the number of triplets chosen from the array that can make triangles if we take them as side lengths of a triangle. For example, if the input to the function is − const arr = [2, 2, 3, 4]; Then the output should be − const output = 3; Triangle Validity Rule For three sides to ...

Read More
Showing 3601–3610 of 8,392 articles
« Prev 1 359 360 361 362 363 840 Next »
Advertisements