JavaScript Algorithm - Removing Negatives from the Array

AmitDiwan
Updated on 15-Mar-2026 23:19:00

374 Views

Given an array X of multiple values (e.g. [-3, 5, 1, 3, 2, 10]), we need to write a function that removes any negative values from the array using only the pop() method without creating temporary arrays. The challenge is to modify the original array in-place, maintaining only positive numbers while using just the pop() method for removal. Algorithm Approach The algorithm works in two phases: Remove all negative values from the end of the array For remaining negatives, replace them with the last positive element and pop Complete Implementation function ... Read More

Get max value per key in a JavaScript array

AmitDiwan
Updated on 15-Mar-2026 23:19:00

530 Views

When working with arrays of objects in JavaScript, you might need to find the object with the maximum value for a specific property within each group. This is a common task when analyzing data grouped by categories. Consider this array of fruit data: const arr = [ {a:1, b:"apples"}, {a:3, b:"apples"}, {a:4, b:"apples"}, {a:1, b:"bananas"}, {a:3, b:"bananas"}, {a:5, b:"bananas"}, {a:6, b:"bananas"}, {a:3, b:"oranges"}, ... Read More

How to test if a parameter is provided to a function in JavaScript?

Yaswanth Varma
Updated on 15-Mar-2026 23:19:00

4K+ Views

Testing if a parameter is provided to a function in JavaScript is an important part of writing effective code. It allows you to ensure that the arguments passed into your functions are valid and properly formatted for use within the function. In this article, we will discuss different methods for testing whether or not a parameter has been provided to a function in JavaScript. Let us understand the term "Parameter". A parameter is a named entity in a function definition that specifies an argument which the function can accept. It acts as a variable within the function and its ... Read More

Trigger an event IMMEDIATELY on mouse click, not after I let go of the mouse - JavaScript?

AmitDiwan
Updated on 15-Mar-2026 23:19:00

347 Views

By default, the click event fires when you press and then release the mouse button. To trigger an event immediately when the mouse button is pressed down, use the mousedown event instead. The Difference Between click and mousedown The click event requires a complete click cycle (press down + release), while mousedown fires immediately when the button is pressed. Using mousedown Event Here's how to trigger an event immediately on mouse press: Mousedown Event Example ... Read More

Join in nested array in JavaScript

AmitDiwan
Updated on 15-Mar-2026 23:19:00

544 Views

Joining elements from nested arrays in JavaScript requires flattening the array structure first. This article explores different approaches to join nested array elements with a semicolon separator. The Problem Consider this nested array: const arr = ['zero', ['one', 'two', 'three', ['four', ['five', 'six', ['seven']]]]]; console.log("Original nested array:", arr); Original nested array: [ 'zero', [ 'one', 'two', 'three', [ 'four', [Array] ] ] ] We need to extract all elements and join them with semicolons to get: zero;one;two;three;four;five;six;seven; Using flat() Method (Modern Approach) The flat(Infinity) method flattens ... Read More

Preparing encoding and decoding algorithms for shortening URLs in JavaScript

AmitDiwan
Updated on 15-Mar-2026 23:19:00

933 Views

URL shortening services like bit.ly and TinyURL take long URLs and convert them into shorter, more manageable links. This process involves encoding the original URL into a compact format and later decoding it back when accessed. To implement a basic URL shortening system in JavaScript, we need two main functions: encrypt() → takes the original URL and returns a shortened unique URL decrypt() → takes the shortened URL and converts it back to the original URL Basic Implementation Using Base64 Encoding Here's a simple approach using ... Read More

Rearranging digits to form the greatest number using JavaScript

AmitDiwan
Updated on 15-Mar-2026 23:19:00

782 Views

Problem We are required to write a JavaScript function that takes in one positive three-digit integer and rearranges its digits to get the maximum possible number. Approach To create the maximum number, we need to arrange the digits in descending order. We can convert the number to a string, split it into individual digits, sort them in descending order, and join them back. Example Following is the code − const num = 149; const maxRedigit = function(num) { if(num < 100 || num > 999) ... Read More

Is the digit divisible by the previous digit of the number in JavaScript

AmitDiwan
Updated on 15-Mar-2026 23:19:00

296 Views

Problem We need to write a JavaScript function that takes a number and checks if each digit is divisible by the digit on its left. The function returns an array of booleans, where the first element is always false since there's no digit before the first one. Example Let's check the number 73312 digit by digit: const num = 73312; const divisibleByPrevious = (n = 1) => { const str = n.toString(); const arr = [false]; for(let i = 1; ... Read More

How to wait resize end event and then perform an action using JavaScript?

Shubham Vora
Updated on 15-Mar-2026 23:19:00

3K+ Views

When resizing a browser window, the 'resize' event fires continuously during the resize operation. This can cause performance issues if you execute heavy JavaScript code on every resize event. The solution is to wait until the resize operation ends before performing any action. The most common approach is using setTimeout() and clearTimeout() to create a debounced resize handler that only executes after the user stops resizing. Syntax let timeoutId; window.addEventListener('resize', () => { // Clear the previous timeout clearTimeout(timeoutId); // Set a ... Read More

How to join JavaScript array of string

AmitDiwan
Updated on 15-Mar-2026 23:19:00

208 Views

We need to write a JavaScript function that joins an array of strings, replaces all whitespaces with dashes "-", and returns the formatted string. For example: If the array is − const arr = ["QA testing promotion ", " Twitter ", "Facebook ", "Test"]; Then the output should be − const output = "QA-testing-promotion-Twitter-Facebook-Test"; Method 1: Using Loop and String Concatenation This approach joins the array elements and processes each character to replace spaces with dashes: const arr = ["QA testing promotion ", " Twitter ", "Facebook ", ... Read More

Advertisements