Javascript Articles

Page 478 of 534

Load a text file and find number of characters in the file - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

Suppose we have a data.txt file that lives in the same directory as our NodeJS file. Suppose the content of that file is − Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s. We are required to write a ...

Read More

Count number of factors of a number - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 505 Views

We are required to write a JavaScript function that takes in a number and returns the count of numbers that exactly divides the input number. For example − If the number is 12, then its factors are − 1, 2, 3, 4, 6, 12 Therefore, the output should be 6. Method 1: Basic Approach This method checks every number from 1 to the given number to see if it divides evenly: function countFactorsBasic(num) { let count = 0; for (let i = 1; i { let count = 0; let flag = 2; while (flag

Read More

JavaScript - Check if array is sorted (irrespective of the order of sorting)

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 539 Views

In JavaScript, checking if an array is sorted regardless of order (ascending or descending) requires comparing adjacent elements to determine if they follow a consistent pattern. We need to identify whether the array is sorted in ascending order, descending order, or not sorted at all. Understanding the Problem An array can be considered sorted if all elements follow either: Ascending order: each element is greater than or equal to the previous one Descending order: each element is less than or equal to the previous one Method 1: Check Both Directions This approach checks if ...

Read More

Twice repetitive word count in a string - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

We are required to write a JavaScript function that takes in a string that contains some words that are repeated twice, we need to count such words. For example, if the input string is: const str = "car bus jeep car jeep bus motorbike truck"; Then the output should be: 3 The function counts words that appear more than once in the string. In this case, "car", "bus", and "jeep" each appear twice. Using Array Methods Here's a solution that splits the string into words and counts duplicates: ...

Read More

Find the element that appears once in sorted array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 602 Views

Suppose, we have a sorted array of literals like this − const arr = [2, 2, 3, 3, 3, 5, 5, 6, 7, 8, 9]; We are required to write a JavaScript function that takes in one such array and returns the first number that appears only once in the array. If there is no such number in the array, we should return false. For this array, the output should be 6 Approach 1: Linear Scan with State Tracking This approach uses a boolean flag to track whether we've encountered duplicates of the ...

Read More

Maximum Possible Sum of Products in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 241 Views

We are given two arrays say, arr1 and arr2 of positive numbers. The number of values in both the arrays are the same. We are required to write a function that finds the maximum sum of products of their elements. Each element in arr1 has to be multiplied with exactly one element in arr2 and vice versa such that each element of both the arrays appears exactly once and the sum of product produced is maximum. Problem Understanding For example: if, arr1 = [5, 1, 3, 4, 2] and, arr2 = [8, 10, 9, 7, ...

Read More

Partially reversing an array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 509 Views

Suppose, we have an array of literals like this: const arr = [3, 5, 5, 2, 23, 4, 7, 8, 8, 9]; console.log("Original array:", arr); Original array: [3, 5, 5, 2, 23, 4, 7, 8, 8, 9] We are required to write a JavaScript function that takes in such an array and a number, say n (n must be less than or equal to the length of array). And the function should reverse the first n elements of the array within. For example, if for this array, the number is 4, then ...

Read More

Move string capital letters to front maintaining the relative order - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 365 Views

We need to write a JavaScript function that takes a string with uppercase and lowercase letters and returns a new string with all uppercase letters moved to the front while maintaining their relative order. For example, if the input string is: const str = 'heLLO woRlD'; Then the output should be: const output = 'LLORDhe wol'; Algorithm Explanation The approach uses an array and a tracking index. We iterate through each character, and if it's uppercase, we insert it at the current capital position and increment the index. Lowercase characters ...

Read More

Removing 0s from start and end - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 178 Views

We are required to write a JavaScript function that takes in a number as a string and returns a new number string with all the leading and trailing 0s removed. For example: If the input is − const strNum = '054954000' Then the output should be − const output = '54954' Using substring() Method This approach uses two pointers to find the first non-zero character from the start and end, then extracts the substring between them. const strNum = '054954000'; const removeZero = (str = '') => ...

Read More

Reverse word starting with particular characters - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 259 Views

We need to write a JavaScript function that takes a sentence string and a character, then reverses all words starting with that specific character. For example, if we have the string: const str = 'hello world, how are you'; And we want to reverse words starting with 'h', the output should be: const output = 'olleh world, woh are you'; Notice that "hello" becomes "olleh" and "how" becomes "woh" because both start with 'h'. Solution We'll split the string into words, check each word's first character, and reverse those ...

Read More
Showing 4771–4780 of 5,340 articles
« Prev 1 476 477 478 479 480 534 Next »
Advertisements