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 249 of 534
Returning 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 MoreFunction that returns the minimum and maximum value of an array in JavaScript
Finding the minimum and maximum values in an array is a fundamental task in JavaScript programming. Whether you're analyzing data, implementing algorithms, or building interactive applications, having efficient methods to determine array extremes is essential. Problem Statement Create a JavaScript function that takes an array of numbers as input and returns both the minimum and maximum values. The function should handle arrays of any length efficiently. Sample Input: const inputArray = [5, 2, 9, 1, 7, 4]; Sample Output: const minValue = 1; const maxValue = 9; Using Math.min() and Math.max() ...
Read MoreRepeating string for specific number of times using JavaScript
JavaScript provides several methods to repeat a string for a specific number of times. This functionality is useful for generating patterns, creating padding, or building repeated text sequences in web applications. Problem Statement Write a JavaScript function that takes a string and a positive integer as input and returns a new string that repeats the input string for the specified number of times. If the input string is empty or the specified number of times is zero, the function should return an empty string. Sample Input: String: "Hello" Number of Times: 3 Sample Output: ...
Read MoreRepresenting seconds in HH:MM:SS in JavaScript
Converting seconds to HH:MM:SS format is a common requirement in JavaScript applications for displaying durations, timers, and time intervals in a readable format. This article explores three practical methods to achieve this conversion. Problem Statement Given a number representing the duration in seconds, the task is to write a JavaScript function that converts the given duration into the format HH:MM:SS, where HH represents hours, MM represents minutes, and SS represents seconds. Sample Input: const durationInSeconds = 3665; Sample Output: 01:01:05 In this case, the input durationInSeconds is 3665, which represents a ...
Read MoreRetrieving the decimal part only of a number in JavaScript
Precision and accuracy play vital roles in numerical computations, and in JavaScript programming, the ability to extract the decimal part of a number is a crucial skill. Whether it is for rounding, comparison, or further manipulation, retrieving only the decimal part of a number can significantly enhance the precision and control over calculations. Problem Statement Given a number in JavaScript, retrieve the decimal part only and return it as a separate number. Sample Input: num = 3.14159 Sample Output: ...
Read MoreUsing Kadane’s algorithm to find maximum sum of subarray in JavaScript
Kadane's algorithm is a powerful dynamic programming technique used to find the maximum sum of a contiguous subarray within an array. This algorithm is particularly useful for solving problems involving sequences of numbers where you need to find the optimal contiguous subsequence. Problem Statement Given an array of integers, find the contiguous subarray with the maximum sum and return that sum. Sample Input: arr = [-2, 1, -3, 4, -1, 2, 1, -5, 4] Sample Output: Maximum sum: 6 (from subarray [4, -1, 2, 1]) Method 1: Naive Approach The ...
Read MoreAlgorithm to add binary arrays in JavaScript
In this problem statement, our target is to add two binary arrays with the help of JavaScript. So we will break down the problem step by step to ensure understanding. What is a Binary Array? A binary array is an array that represents a binary number. In a binary number system we have only two possible digits, 0 and 1. The binary array will have elements which can only be 0s and 1s. Every item in the array represents a bit of the binary number. For example, we have a binary array [1, 0, 1, 0]. This ...
Read MoreAverage with the Reduce Method in JavaScript
In JavaScript, calculating the average of array elements using the reduce() method is an efficient and elegant approach. The reduce() method iterates through the array once to compute the sum, which we then divide by the array length. Understanding the Problem We need to calculate the average value of all elements in an array using JavaScript's reduce() method. For example, given the array [1, 2, 3, 4, 5], the average would be (1 + 2 + 3 + 4 + 5) / 5 = 15 / 5 = 3. Algorithm Step 1: Create a function that ...
Read More