Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles on Trending Technologies
Technical articles with clear explanations and examples
Merge JavaScript objects with the same key value and count them
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 MoreChecking the validity of parentheses in JavaScript
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 MoreSum of All Possible Odd Length Subarrays in JavaScript
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 MoreProblem Can we fit remaining passengers in the bus using JavaScript
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 MoreLongest subarray with unit difference in JavaScript
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 MoreHow to use the "in" operator in JavaScript?
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 MoreHow to get the sum of the powers of all numbers in JavaScript?
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 MoreHow to allocate memory to an object whose length is set to 0 - JavaScript?
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 MoreFinding product of an array using recursion in JavaScript
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 MoreFinding the Largest Triple Product Array in JavaScript
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