Web Development Articles

Page 264 of 801

Summing numbers present in a string separated by spaces using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 701 Views

We are required to write a JavaScript function that takes in a string which has integers inside it separated by spaces. The task of our function is to convert each integer in the string into an integer and return their sum. Problem Given a string containing numbers separated by spaces, we need to: Split the string into individual number strings Convert each string to a number Calculate the sum of all numbers Method 1: Using split() and reduce() const str = '1 5 ...

Read More

All ways to divide array of strings into parts in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 336 Views

In JavaScript, dividing an array into two non-empty parts can be accomplished through various approaches. This tutorial explores different methods to generate all possible ways to split an array of strings into exactly two parts. Problem Statement We need to create a JavaScript function that takes an array of strings with at least two elements and returns all possible ways to divide it into two non-empty parts. For example, with the array ["az", "toto", "picaro", "zone", "kiwi"], we want to generate: (az) | (toto picaro zone kiwi) (az toto) | (picaro zone kiwi) ...

Read More

Finding the count of total upside down numbers in a range using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 302 Views

Upside Down Numbers Upside down numbers are special numbers that remain identical when rotated 180 degrees. Only certain digits can form upside down numbers: 0, 1, 6, 8, and 9, where 6 becomes 9 and vice versa when rotated. For example, 69 becomes 69 when rotated, and 8108 becomes 8018 (which is not the same, so it's not upside down). Problem Statement We need to write a JavaScript function that takes a range of two numbers and returns the count of all upside down numbers within that range. Understanding Valid Digits When rotated 180 ...

Read More

Swapping adjacent binary bits of a decimal to yield another decimal using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 238 Views

Problem We are required to write a JavaScript function that takes in a number and swaps its adjacent binary bits to construct a new binary representation. The function should return the decimal equivalent of the modified binary. How It Works The algorithm converts the decimal number to binary, pairs adjacent bits, swaps each pair, and converts back to decimal. If the binary representation has an odd number of bits, we pad it with a leading zero to ensure complete pairs. Example Let's trace through number 13: 13 in binary: 1101 Pair adjacent bits: ...

Read More

Counting prime numbers that reduce to 1 within a range using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 229 Views

Problem We need to write a JavaScript function that takes a range array of two numbers and returns the count of prime numbers whose squared sum of digits eventually reduces to 1 through repeated calculation. For example, 23 is a prime number and: 2² + 3² = 4 + 9 = 13 1² + 3² = 1 + 9 = 10 1² + 0² = 1 + 0 = 1 Since the process eventually reaches 1, the number 23 qualifies as a "happy prime". Understanding the Solution The solution involves three key functions: ...

Read More

Reversing a string while maintaining the position of spaces in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 873 Views

Problem We are required to write a JavaScript function that takes in a string that might contain some spaces. Our function should reverse the non-space characters while keeping spaces in their original positions. Understanding the Task The goal is to reverse only the alphabetic characters while maintaining the exact positions of spaces. For example, "this is normal string" becomes "gnir ts lamron sisiht" - spaces remain at positions 4, 7, and 14. Example Following is the code: const str = 'this is normal string'; const reverseWordsWithin = (str = '') => { ...

Read More

Removing consecutive duplicates from strings in an array using JavaScript

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

Problem We are required to write a JavaScript function that takes in an array of strings. Our function should remove the duplicate characters that appear consecutively in the strings and return the new modified array of strings. Example Following is the code − const arr = ["kelless", "keenness"]; const removeConsecutiveDuplicates = (arr = []) => { const map = []; const res = []; arr.map(el => { el.split('').reduce((acc, value, index, arr) => { ...

Read More

Sorting string of words based on the number present in each word using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 671 Views

We are required to write a JavaScript function that takes in a string that represents a sentence. Our function should sort this sentence. Each word in the sentence string contains an integer. Our function should sort the string such that the word that contains the smallest integer is placed first and then in the increasing order. Problem Statement Given a string containing words with embedded numbers, we need to sort the words based on the numerical values they contain. For example, "is2 Thi1s T4est 3a" should become "Thi1s is2 3a T4est" because the numbers are in order ...

Read More

Representing number as the power and product of primes in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 272 Views

We need to write a JavaScript function that takes a positive integer and represents it as a product of prime powers. This process is called prime factorization. For a number n, our function should return a string in the format: n = "(p1**n1)(p2**n2)...(pk**nk)" Where p1, p2, p3...pk are prime numbers, n1, n2...nk are their powers, and ** represents exponentiation. Understanding Prime Factorization Prime factorization breaks down a number into its prime factors. For example, 12 = 2² × 3¹, which would be represented as "(2**2)(3)". Implementation Here's a corrected and improved implementation: ...

Read More

Finding array number that have no matching positive or negative number in the array using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 217 Views

We need to write a JavaScript function that finds a number in an array that doesn't have its positive or negative counterpart. For example, in an array containing both 1 and -1, both 2 and -2, but only 3 (without -3), we should return 3. Problem Given an array of integers where each number has its negative or positive complement, except for exactly one number, our function should find and return that unpaired number. Example Input Consider the array [1, -1, 2, -2, 3]. Here, 1 has -1, 2 has -2, but 3 has no -3, ...

Read More
Showing 2631–2640 of 8,010 articles
« Prev 1 262 263 264 265 266 801 Next »
Advertisements