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
Articles by AmitDiwan
Page 329 of 840
Constructing full name from first name, last name and optional middle name in JavaScript
We need to write a JavaScript function that takes in three strings: first name, last name, and an optional middle name, then returns the full name constructed from these inputs. Problem The challenge is handling the optional middle name parameter. If no middle name is provided, we should construct the full name with just the first and last names, avoiding extra spaces. Using Array Filter Method This approach uses an array to collect all name parts, filters out empty values, and joins them with spaces: const firstName = 'Vijay'; const lastName = 'Raj'; ...
Read MoreSplitting a string into maximum parts in JavaScript
In JavaScript, partitioning a string into maximum parts means dividing it so that each letter appears in only one partition. This creates the maximum number of non-overlapping sections possible. Problem Statement We need to write a JavaScript function that takes a string and partitions it into as many parts as possible, where each letter appears in at most one part. The function returns an array of integers representing the size of these parts. Input Example: const str = "ababcbacadefegdehijhklij"; Expected Output: [9, 7, 8] Explanation: The partitions are "ababcbaca" ...
Read MoreGenerating random string of specified length in JavaScript
We are required to write a JavaScript function that takes in a number n and returns a random string of length n containing no other than the 26 English lowercase alphabets. Therefore, let's write the code for this function − Example The code for this will be − const num = 8; const randomNameGenerator = num => { let res = ''; for(let i = 0; i < num; i++){ const random = Math.floor(Math.random() * 26); ...
Read MoreConverting number of corresponding string without using library function in JavaScript
Converting a number to a string without using built-in functions like String() or toString() requires extracting individual digits and building the result character by character. Problem We need to write a JavaScript function that takes a number and converts it to its corresponding string representation without using the built-in functions String() or toString() or direct string concatenation. Approach The solution extracts digits from right to left using the modulo operator (%) and integer division. Each digit is converted to its character representation and prepended to build the final string. Example const num = ...
Read MoreFinding the height based on width and screen size ratio (width:height) in JavaScript
We are required to write a JavaScript function that takes in the width of the screen as the first argument and the aspect ratio (w:h) as the second argument. Based on these two inputs our function should return the height of the screen. Understanding Aspect Ratios An aspect ratio defines the proportional relationship between width and height. Common ratios include 16:9 (widescreen), 4:3 (traditional), and 21:9 (ultrawide). The formula is: height = (width × ratio_height) / ratio_width. Example Following is the code − const ratio = '18:11'; const width = 2417; const findHeight ...
Read MoreConvert mixed case string to lower case in JavaScript
In JavaScript, there are multiple ways to convert a mixed-case string to lowercase. The most common approach is using the built-in toLowerCase() method, but you can also implement a custom solution using character codes. Using the Built-in toLowerCase() Method The simplest and most efficient way is to use JavaScript's built-in toLowerCase() method: const str = 'ABcD123'; const output = str.toLowerCase(); console.log(output); abcd123 Custom Implementation Using Character Codes For educational purposes, here's how to implement a custom convertToLower() function that converts uppercase letters (ASCII 65-90) to lowercase by adding 32 to ...
Read MoreFinding nearest prime to a specified number in JavaScript
We are required to write a JavaScript function that takes in a number and returns the first prime number that appears after n. For example: If the number is 24, then the output should be 29. Therefore, let's write the code for this function − Understanding Prime Numbers A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. Examples include 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, etc. Example The code for this will be − const num = ...
Read MoreFinding quarter based on month index in JavaScript
We are required to write a JavaScript function that takes in the 1-based month index and return the quarter which the month falls in. Understanding Quarters A year is divided into four quarters, each containing three months: Q1: January, February, March (months 1-3) Q2: April, May, June (months 4-6) Q3: July, August, September (months 7-9) Q4: October, November, December (months 10-12) Method 1: Using if-else Statements const month = 7; const findQuarter = (month = 1) => { ...
Read MoreRealtime moving average of an array of numbers in JavaScript
A moving average calculates the average of elements from the start of an array up to each position. For each element at index i, we compute the average of elements from index 0 to i. Problem We need to write a JavaScript function that takes an array of numbers and returns a new array containing the cumulative moving average at each position. Input: [1, 2, 3, 4, 5] Output: [1, 1.5, 2, 2.5, 3] The first element is the average of just the first element (1/1 = 1). The second element is the average ...
Read MoreFinding distance to next greater element in JavaScript
Problem We are required to write a JavaScript function that takes in an array of numbers, arr, as the first and the only argument. Our function should construct a new array for the input in which each corresponding element is the distance to the next greater element than the current element, and if there is no greater element to the right of the current element, we should push 0 for that corresponding element in the res array and finally we should return this array. Example Input and Output Input const arr = [12, 13, 14, 11, ...
Read More