Web Development Articles

Page 498 of 801

Count number of factors of a number - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 517 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 549 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 621 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 249 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 520 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 374 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 190 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 269 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

Casting a string to snake case - JavaScript

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

Snake case is a naming convention where words are separated by underscores (_) and all letters are lowercase. For example, "Hello World" becomes "hello_world". This article demonstrates how to convert any string to snake case using JavaScript. Syntax function toSnakeCase(str) { return str.split(' ') .map(word => word.toLowerCase()) .join('_'); } Example Here's a complete example that converts a sentence to snake case: ...

Read More
Showing 4971–4980 of 8,010 articles
« Prev 1 496 497 498 499 500 801 Next »
Advertisements