AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 319 of 840

Splitting number to contain continuous odd or even number using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 315 Views

Problem We need to write a JavaScript function that takes a positive number and splits it into continuous segments of odd or even digits. The function should create a new segment whenever it encounters a digit with different parity (odd becomes even or even becomes odd). Example For the number 124579: 1 (odd) - starts first segment 2, 4 (even) - starts second segment 5, 7, 9 (odd) - starts third segment Solution Following is the code: const num = 124579; const splitDifferent = (num = 1) => { const ...

Read More

How to add an element horizontally in HTML page using JavaScript?

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

We can use the JavaScript method "createElement" to create a new element. Then, we can use the method "appendChild" to add the element to a parent element on the HTML page. To position the element horizontally, we can use CSS styles such as "display:inline-block" or "float:left/right" on the newly created element. Suppose a situation where we want to depict a graphical representation of a Linked List data structure. Every time the user clicks on a button, a new node, represented by a green circle in our case, should get prepended to the list of nodes horizontally. And the ...

Read More

Sort an array according to another array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 424 Views

When working with arrays in JavaScript, you may need to sort one array based on the order of elements in another array. This is useful when you have a reference array that defines the desired ordering. Suppose we have two arrays like these: const arr1 = [1, 3, 2, 4, 5, 6]; const arr2 = [1, 2, 5]; console.log("Original arr1:", arr1); console.log("Reference arr2:", arr2); Original arr1: [ 1, 3, 2, 4, 5, 6 ] Reference arr2: [ 1, 2, 5 ] We want to sort arr1 so that elements appearing in ...

Read More

Finding the longest valid parentheses JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 387 Views

Given a string containing just the characters '(' and ')', we find the length of the longest valid (well-formed) parentheses substring. A set of parentheses qualifies to be a well-formed parentheses, if and only if, for each opening parentheses, it contains a closing parentheses. For example: '(())()' is a well-formed parentheses '())' is not a well-formed parentheses '()()()' is a well-formed parentheses Algorithm Approach The solution uses a stack-based approach to track invalid parentheses positions. We push indices of unmatched characters onto the stack, then calculate the longest valid substring between these invalid ...

Read More

Unique intersection of arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 369 Views

We are required to write a JavaScript function that takes in two arrays of numbers, let's say arr1 and arr2. The function should find the intersection between the elements of the array. i.e., the elements that appear in both the arrays. The only condition is that if we encountered one element before as intersected, we should not consider it again even if appears again in both the arrays. For example − If the input arrays are − const arr1 = [1, 5, 7, 3, 1]; const arr2 = [1, 7, 3, 1, 6]; ...

Read More

Calculating a number from its factorial in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 228 Views

We are required to write a JavaScript function that takes in a number as the only argument. The function should check whether there exists any number whose factorial is the number taken as input. If there exists any such number, we should return that number otherwise we should return -1. Problem Understanding Given a number, we need to find if it's a factorial of some integer. For example, 720 is the factorial of 6 because 6! = 6 × 5 × 4 × 3 × 2 × 1 = 720. If the input is: ...

Read More

Largest rectangle sum smaller than num in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 216 Views

We are required to write a JavaScript function that takes in a 2-D array of Numbers as the first argument and a target sum number as the second argument. Our function should find out that rectangle from the 2-D array which has the greatest sum among all rectangles in the array but just less than or equal to the target sum specified by the second argument to the function. Problem Statement Given a 2D matrix and a target number, find the largest rectangle sum that is smaller than or equal to the target. The function should return ...

Read More

Constructing a sentence based on array of words and punctuations using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 883 Views

We need to write a JavaScript function that takes an array of words and punctuations and constructs a proper sentence following specific spacing and punctuation rules. Problem Statement Our function should join array elements to construct a sentence based on the following rules: There must always be a space between words There must not be a space between a comma and the word on the left There must always be one and only one period at the end of a sentence Solution Here's how ...

Read More

How to break JavaScript Code into several lines?

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

We can break JavaScript code into several lines to improve readability and maintainability. JavaScript provides several methods to split long statements across multiple lines without breaking the code's functionality. Methods to Break JavaScript Code Using Backslash (\) Line Continuation The backslash character at the end of a line tells JavaScript to continue reading the next line as part of the same statement: let longString = "This is a very long string that needs to be \ broken into several lines for better readability"; console.log(longString); This is a very long string that ...

Read More

How to filter out common array in array of arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 311 Views

When working with arrays of arrays in JavaScript, you may need to remove duplicate subarrays and keep only unique ones. This is useful for data deduplication and filtering operations. The Problem Suppose we have an array of arrays with duplicate subarrays: const arr = [ [ "Serta", "Black Friday" ], [ "Serta", ...

Read More
Showing 3181–3190 of 8,392 articles
« Prev 1 317 318 319 320 321 840 Next »
Advertisements