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
Javascript Articles
Page 168 of 534
Length of shortest unsorted array in JavaScript
We are required to write a JavaScript function that takes in an array of numbers and finds the length of the shortest continuous subarray that, when sorted, makes the entire array sorted in ascending order. Our function needs to find the length of one continuous subarray such that if we only sort this subarray in ascending order, then the whole array will be sorted in ascending order, too. Problem Example For example, if the input array is: const arr = [3, 7, 5, 9, 11, 10, 16]; console.log("Original array:", arr); Original array: ...
Read MoreReshaping 2-D array in JavaScript
In JavaScript, reshaping a 2-D array means converting it to a new matrix with different dimensions while preserving the original element order. This is useful for data manipulation and matrix operations. Problem Statement We need to write a JavaScript function that takes a 2-D array and reshapes it into a new matrix with specified rows and columns. The elements should maintain their row-traversing order from the original array. For example, if we have: const arr = [ [6, 7], [8, 9] ]; const r = 1, c ...
Read MoreFind and return the longest length of set in JavaScript
In JavaScript, finding the longest length of a set involves traversing array elements in a cycle pattern where each element points to the next index. This problem requires tracking visited elements to avoid infinite loops and find the maximum cycle length. Problem Statement Given an array of length N containing all integers from 0 to N-1, we need to find the longest set S where S[i] = {A[i], A[A[i]], A[A[A[i]]], ...}. We start from index i, then move to A[i], then to A[A[i]], and continue until we encounter a duplicate element. Example Input and Output For ...
Read MoreFinding minimum time difference in an array in JavaScript
We are required to write a JavaScript function that takes in an array of 24-hour clock time points in "Hour:Minutes" format. Our function should find the minimum minutes difference between any two time points in the array. For example, if the input to the function is: const arr = ["23:59", "00:00"]; Then the output should be 1 minute, because the minimum difference between these times is 1 minute (from 23:59 to 00:00). Understanding the Problem The key challenge is handling the circular nature of time. When comparing "23:59" and "00:00", we need to ...
Read MoreCreating a chained operation class in JavaScript
This article demonstrates how to create a fluent interface class in JavaScript that allows chaining numeric values and mathematical operations. The Streak class enables natural language-like mathematical expressions. Problem We need to create a custom data type Streak in JavaScript that supports method chaining with values and operations alternately. The supported values are: → one, two, three, four, five, six, seven, eight, nine The supported operations are: → plus, minus For example, this expression: Streak.one.plus.five.minus.three; Should evaluate to: 3 How It ...
Read MoreFinding all peaks and their positions in an array in JavaScript
A peak (local maximum) in an array is an element that is greater than both its neighbors. Finding all peaks and their positions is useful for data analysis, signal processing, and identifying trends. Problem Statement Given an array of integers, we need to find all local maxima (peaks) and return an object containing two arrays: maximas (the peak values) and positions (their indices). Consider this array: const arr = [4, 3, 4, 7, 5, 2, 3, 4, 3, 2, 3, 4]; console.log("Array:", arr); Array: [4, 3, 4, 7, 5, 2, 3, 4, ...
Read MoreFinding the final direction of movement in JavaScript
Problem We need to write a JavaScript function that takes an array of directional characters and finds the final direction after canceling out opposite movements. The array contains only 4 possible characters: 'N' → stands for North direction 'S' → stands for South direction 'W' → stands for West direction 'E' → stands for East direction Each character represents a unit move in that direction. When opposite directions ('S' and 'N') or ('E' and 'W') appear adjacently, they cancel each other out. Our ...
Read MoreCounting steps to make number palindrome in JavaScript
We are required to write a JavaScript function that takes in a number, num, as the first and the only argument. Our function should return the number of special steps needed to obtain a palindrome. The special step is: "reverse the digits, and add to the original number". If the resulting number is not a palindrome, repeat the procedure with the sum until the resulting number is a palindrome. Problem Example For example, if the input to the function is: const num = 87; The expected output is 4 steps because: ...
Read MoreRemoving parentheses from mathematical expressions in JavaScript
When working with mathematical expressions in JavaScript, you may need to remove parentheses while preserving the correct operations and operands. This involves carefully tracking signs and handling nested expressions. Problem We need to write a JavaScript function that takes a string of mathematical expressions and removes parentheses while keeping operations and operands in their correct positions with proper signs. For example, if the input is: const str = 'u-(v-w-(x+y))-z'; The expected output should be: u-v+w+x+y-z How It Works The algorithm uses a stack to track sign changes when ...
Read MoreLimiting elements occurrences to n times in JavaScript
Problem We are required to write a JavaScript function that takes in an array of integers, arr, that may contain duplicates as the first argument, and a number, num, as the second and final argument. The task of our function is to iterate through the array and check whether there exists some number that appears for more than n times in the array. If there exists any such element, we should delete its extra occurrences to limit its occurrence to at most num. For example, if the input to the function is: Input ...
Read More