Articles on Trending Technologies

Technical articles with clear explanations and examples

Merge JavaScript objects with the same key value and count them

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

Suppose we have an array of objects like this: const arr = [{ "value": 10, "id": "111", "name": "BlackCat", }, { "value": 10, "id": "111", "name": "BlackCat", }, { "value": 15, "id": "777", "name": "WhiteCat", }]; We are required to write a JavaScript function that takes in one such array. The function should then merge all those objects together that ...

Read More

Checking the validity of parentheses in JavaScript

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

We are required to write a JavaScript function that takes in a string containing just the characters: '(', ')', '{', '}', '[' and ']' Our function should determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brackets. Open brackets must be closed in the correct order. ...

Read More

Sum of All Possible Odd Length Subarrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 447 Views

We are required to write a JavaScript function that takes in an array of integers as the only argument. The function should first permute all possible subarrays from the original array that have an odd length. And then the function should find the combined sum of all the elements of those subarrays and return the sum. For example, if the input array is: const arr = [1, 2, 3]; Then the output should be: const output = 12; Because the desired subarrays are [1], [2], [3], [1, 2, 3] with ...

Read More

Problem Can we fit remaining passengers in the bus using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 325 Views

Problem We need to write a JavaScript function that determines if a bus can accommodate all waiting passengers. The function takes three parameters: cap − the total capacity of people the bus can hold (excluding the driver) on − the number of people currently on the bus (excluding the driver) wait − the number of people waiting to board the bus If there's enough space for everyone, return 0. Otherwise, return the number of passengers who cannot board. Solution The logic is straightforward: calculate ...

Read More

Longest subarray with unit difference in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 164 Views

We need to find the length of the longest subarray where the difference between maximum and minimum values is exactly 1. This means the subarray can only contain two consecutive numbers (like 3 and 4) or just one unique number repeated. Problem Statement Given an array of numbers, find the length of the longest subarray where the difference between its maximum and minimum values is exactly 1. For example, with input array: const arr = [2, 4, 3, 3, 6, 3, 4, 8]; The longest valid subarray is [4, 3, 3, 3, 4] ...

Read More

How to use the "in" operator in JavaScript?

Mayank Agarwal
Mayank Agarwal
Updated on 15-Mar-2026 245 Views

In this article, we are going to explore the 'in' operator and how to use it in JavaScript. The in operator is an inbuilt operator in JavaScript that is used for checking whether a particular property exists in an object or not. It will return true if the property exists, else false is returned. Syntax prop in object Parameters This operator accepts the following parameters as described below − prop − This parameter holds the string or symbol that represents a property ...

Read More

How to get the sum of the powers of all numbers in JavaScript?

Imran Alam
Imran Alam
Updated on 15-Mar-2026 605 Views

In this tutorial, we'll be discussing how to get the sum of the powers of all the numbers from start to end in JavaScript. We'll be using the built-in Math.pow() method to calculate the powers and the Array reduce() method to sum up the values. Using the Math.pow() Method The Math.pow() method allows us to calculate the power of a number. We can use this method to calculate the powers of all the numbers from start to end. Syntax Math.pow(base, exponent); Parameters Base − The base number. ...

Read More

How to allocate memory to an object whose length is set to 0 - JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 143 Views

In JavaScript, you can manipulate array length to clear memory and allocate new slots. When you set an array's length to 0, it clears all elements. Setting length to a larger value allocates empty slots. Understanding Array Length Property The length property controls how many elements an array can hold. Setting it to 0 removes all elements, while setting it to a larger number creates empty slots filled with undefined. var arrayObject = [ "John", "David", "Mike" ]; console.log("Original array:", arrayObject); console.log("Original ...

Read More

Finding product of an array using recursion in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 685 Views

We are required to write a JavaScript function that takes in an array of numbers. Our function should do the following two things: Make use of a recursive approach. Calculate the product of all the elements in the array. And finally, it should return the product. For example, if the input array is: const arr = [1, 3, 6, 0.2, 2, 5]; Then the output should be: 36 How Recursion Works Recursion breaks down the problem into smaller subproblems. For ...

Read More

Finding the Largest Triple Product Array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 237 Views

We are required to write a JavaScript function that takes in an array of integers as the only argument. Based on the array taken in as input, the function should construct a new array of the same length based on the following criteria. Any corresponding element of the output array should be the product of the three largest numbers encountered thus far. If the corresponding index is less than 3 (we have not encountered three elements yet) then the corresponding value should be -1. Although we can use non-unique values to calculate the product, those non-unique values should ...

Read More
Showing 16501–16510 of 61,297 articles
Advertisements