Javascript Articles

Page 479 of 534

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

Distance to nearest vowel in a string - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 507 Views

We are required to write a JavaScript function that takes in a string with at least one vowel, and for each character in the string we have to map a number representing its nearest distance from a vowel. For example: If the string is − const str = 'vatghvf'; Then the output should be − const output = [1, 0, 1, 2, 3, 4, 5]; How It Works The algorithm works by: Finding all vowel positions in the string For each character, calculating the minimum distance to any vowel ...

Read More

Calculating resistance of n devices - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 278 Views

In Physics, the equivalent resistance of resistors varies based on their connection type. When resistors are connected in series, their resistances add directly: R_series = R1 + R2 + R3 For parallel connections, we use the reciprocal formula: 1/R_parallel = (1/R1) + (1/R2) + (1/R3) We need to create a JavaScript function that calculates equivalent resistance for any number of resistors in either series or parallel configuration. Series Connection Implementation For series resistors, we simply sum all resistance values: const calculateSeries = (...resistors) => { ...

Read More

Finding greatest digit by recursion - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 359 Views

We are required to write a JavaScript recursive function that takes in a number and returns the greatest digit in the number. For example: If the number is − 45654356 Then the return value should be 6 How It Works The recursive approach extracts digits one by one from right to left using modulo (%) and integer division. Each digit is compared with the current maximum, and the function calls itself with the remaining digits. Example Following is the code − const num = 45654356; const greatestDigit = (num ...

Read More

Sum of prime numbers between a range - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 562 Views

We are required to write a JavaScript function that takes in two numbers, say a and b and returns the sum of all the prime numbers that fall between a and b. We should include a and b if they are prime as well. 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, etc. Algorithm Overview To solve this problem, we need two functions: isPrime() - checks if a ...

Read More

Find the Sum of fractions - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 727 Views

In JavaScript, we can calculate the sum of fractions by finding a common denominator and adding the numerators. This tutorial shows how to add fractions represented as arrays without converting to decimals. Problem Statement Given an array of arrays where each subarray contains two numbers representing a fraction, we need to find their sum in fraction form. const arr = [[12, 56], [3, 45], [23, 2], [2, 6], [2, 8]]; // Represents fractions: 12/56, 3/45, 23/2, 2/6, 2/8 Algorithm Overview To add fractions a/b + c/d, we use the formula: (a*d + c*b) ...

Read More

Checking smooth sentences in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 308 Views

We are required to write a JavaScript function that checks whether a sentence is smooth or not. A sentence is smooth when the first letter of each word in the sentence is same as the last letter of its preceding word. How It Works A smooth sentence follows this pattern: if word A ends with letter 'x', then word B must start with letter 'x'. For example, "this stringt tries" is smooth because "this" ends with 's' and "stringt" starts with 's', "stringt" ends with 't' and "tries" starts with 't'. Example Following is the code ...

Read More

Can array form consecutive sequence - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 270 Views

We are required to write a JavaScript function that takes in an array of numbers and checks if the elements of the array can be rearranged to form a sequence of consecutive numbers or not. For example, if we have an array [3, 1, 4, 2, 5], these numbers can be rearranged as [1, 2, 3, 4, 5] to form a consecutive sequence. Problem Understanding A consecutive sequence means each number is exactly 1 more than the previous number. To check this, we need to: Sort the array in ascending order ...

Read More

Check for Valid hex code - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 788 Views

A string can be considered as a valid hex code if it contains no characters other than the 0-9 digits and a-f alphabets (case-insensitive). Hex codes are commonly used in web development for colors, cryptographic hashes, and other applications. For example: '3423ad' is a valid hex code '4234es' is an invalid hex code (contains 'e' and 's') We need to write a JavaScript function that takes in a string and checks whether it's a valid hex code or not. Method 1: Using includes() Method This approach checks each character against a string containing ...

Read More

Finding the element larger than all elements on right - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 210 Views

We are required to write a JavaScript function that takes in an array of numbers and returns a subarray that contains all the elements from the original array that are larger than all the elements on their right. Problem Understanding For each element in the array, we need to check if it's greater than every element that comes after it. If yes, include it in the result. The last element is always included since there are no elements to its right. Algorithm Approach We can solve this efficiently by traversing the array from right to left, ...

Read More
Showing 4781–4790 of 5,340 articles
« Prev 1 477 478 479 480 481 534 Next »
Advertisements