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 307 of 652
Function 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 MoreCheck if string ends with desired character in JavaScript
In JavaScript, you can check if a string ends with a specific character using several built-in methods. The most common approaches are using endsWith(), charAt(), or bracket notation. Understanding the Problem We need to determine if a given string ends with a specific character. For example, checking if "Hello World!" ends with "!" should return true, while checking if "Hello World." ends with "!" should return false. Using endsWith() Method (Recommended) The endsWith() method is the most straightforward approach: const str1 = "Hello, Tutorials Point!"; const str2 = "Hello, World."; const desiredChar = "!"; ...
Read MoreAbsolute Values Sum Minimization in JavaScript
In JavaScript, finding the value that minimizes the sum of absolute differences from all elements in an array is a fundamental optimization problem. The optimal solution is to find the median of the array. Understanding the Problem Given an array of numbers, we need to find a value x that minimizes the sum of |arr[i] - x| for all elements in the array. Mathematically, this optimal value is always the median of the sorted array. Sum Minimization Concept 1 ...
Read MoreCheck whether we can form string2 by deleting some characters from string1 without reordering the characters of any string - JavaScript
In this problem, we need to determine whether we can form string2 by removing some characters from string1 without changing the order of characters in either string. This is essentially checking if string2 is a subsequence of string1. Understanding the Problem We want to check if the second string can be formed from the first string while preserving character order. Let's look at some examples: // Example 1: Can form "ale" from "apple" let s1 = "apple"; let s2 = "ale"; console.log("Can form '" + s2 + "' from '" + s1 + "'?"); // Expected: ...
Read More