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
Front End Technology Articles
Page 306 of 652
Sorting numbers in ascending order and strings in alphabetical order in an array in JavaScript
In JavaScript, sorting arrays that contain both numbers and strings requires a custom approach since the default sort() method converts all elements to strings. This article explores two effective methods to sort mixed arrays with numbers first (ascending) followed by strings (alphabetical). Problem Statement We need to create a JavaScript function that sorts an array containing both numbers and strings. The requirements are: Numbers should appear before strings Numbers should be sorted in ascending order Strings should be sorted alphabetically Sample Input: ["zebra", 21, "apple", 5, "orange", 13, "banana", 2] Sample ...
Read MoreChecking if a string can be made palindrome in JavaScript
In JavaScript, determining if a string can be made into a palindrome by removing at most one character is a common algorithmic problem. A palindrome reads the same forwards and backwards, like "racecar" or "level". Problem Statement Given a string, we need to check if it can become a palindrome by removing at most one character. The function should return true if possible, false otherwise. Sample Input: "racecar" Sample Output: true Using Two Pointers (Recommended) The most efficient approach uses two pointers starting from both ends of the string, moving towards the center while comparing ...
Read MoreCounting matching substrings in JavaScript
Counting matching substrings in JavaScript is essential for text analysis and string manipulation tasks. This article explores different methods to count subsequences within a string, helping developers choose the most appropriate approach for their needs. Problem Statement We need a JavaScript function that counts subsequences in a given string. The function takes a string "str" and an array of strings "arr" as input. It examines each element in "arr" and determines how many strings are subsequences of "str". A subsequence is formed by removing characters from the original string while maintaining the relative order of remaining characters. ...
Read MoreIn-place Algorithm to Move Zeros to End of List in JavaScript
Moving zeros to the end of an array while maintaining the order of non-zero elements is a common programming problem in JavaScript. This article explores two efficient approaches to solve this problem: the in-place two-pointer method and the count-and-fill approach. Problem Statement Given an array containing integers including zeros, we need to move all zeros to the end while preserving the relative order of non-zero elements. The goal is to achieve this efficiently, ideally in a single pass through the array. For example, given the input array: [2, 5, 0, 1, 0, 8, 0, 3] ...
Read MoreLargest sum of subarrays in JavaScript
Finding the maximum sum subarray is a classic algorithmic problem in JavaScript. Given an array of numbers, we need to find the contiguous subarray with the largest sum. This problem is commonly known as the Maximum Subarray Problem and has practical applications in data analysis and optimization. Problem Statement Given an array of integers, find the contiguous subarray with the maximum sum. For example, given the array: [-3, 4, 2, -1, 6, -5, 3, -2, 7, 1] The maximum subarray sum would be: 15 This comes from the subarray [4, ...
Read MoreNumber pattern in JavaScript
Number patterns in JavaScript are essential for developers who want to create visually appealing outputs and understand algorithmic thinking. These patterns involve generating sequences of numbers that follow specific rules or arrangements, helping developers grasp mathematical concepts that underlie complex algorithms. What are Number Patterns? Number patterns are sequences of numbers arranged in specific formations, typically displayed in triangular or pyramid structures. Each pattern follows a mathematical rule that determines how numbers are positioned and incremented. Pattern 1: Zero-Padded Triangle This pattern creates a triangular structure where each row contains numbers from 1 to the row ...
Read MorePicking index randomly from array in JavaScript
In JavaScript, randomly selecting an index from an array is a common requirement for applications that need unpredictable behavior, such as shuffling content, random sampling, or creating dynamic user experiences. This article explores several effective methods to achieve this functionality. Problem Statement We need an efficient way to randomly select an index from a JavaScript array, where each index has an equal probability of being chosen. JavaScript doesn't provide a built-in method for this, so we'll implement our own solutions. Sample Input: Consider an array: let arr = [12, 45, 7, 89, 34, 56] Sample Output: ...
Read MoreReturning reverse array of integers using JavaScript
In JavaScript, creating an array of integers in reverse order (from a number down to 1) is a common programming task. This article explores multiple approaches to generate descending arrays efficiently, from basic loops to built-in array methods. Problem Statement Create a JavaScript function that generates an array of natural numbers starting from a given number and descending down to 1. Sample Input: const num = 5; Sample Output: const output = [5, 4, 3, 2, 1]; Method 1: Using For Loop The most straightforward approach uses a decrementing for ...
Read MoreValidating a password using JavaScript
Password validation is a critical security feature in web applications. JavaScript provides powerful tools to implement client-side password validation that checks for complexity requirements like length, character types, and special symbols. This helps ensure users create strong passwords that protect their accounts from unauthorized access. Problem Statement We need to create a JavaScript function that validates passwords based on these criteria: Password length must be between 6 and 20 characters Must contain at least one digit (0-9) Must contain at least one lowercase letter (a-z) Must contain at ...
Read MoreConstructing an array of smaller elements than the corresponding elements based on input array in JavaScript
In JavaScript, constructing an array where each element represents the count of smaller numbers to the right is a common algorithmic problem. This technique is useful for data analysis, ranking systems, and competitive programming challenges. Problem Statement Given an input array of numbers, create an output array where each element represents the count of numbers smaller than the current element and located to its right in the input array. Sample Input: Input Array: [5, 2, 6, 1] Sample Output: Output Array: [2, 1, 1, 0] For element 5: two ...
Read More