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
Javascript Articles
Page 166 of 534
Swapping adjacent binary bits of a decimal to yield another decimal using JavaScript
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 MoreCounting prime numbers that reduce to 1 within a range using JavaScript
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 MoreReversing a string while maintaining the position of spaces in JavaScript
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 MoreRemoving consecutive duplicates from strings in an array using JavaScript
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 MoreSorting string of words based on the number present in each word using JavaScript
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 MoreRepresenting number as the power and product of primes in JavaScript
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 MoreFinding array number that have no matching positive or negative number in the array using JavaScript
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 MoreFinding the character with longest consecutive repetitions in a string and its length using JavaScript
We need to write a JavaScript function that finds the character with the longest consecutive repetitions in a string and returns both the character and its count as an array. Problem Given a string, we want to identify which character appears the most times consecutively and return an array containing the character and its consecutive count. Example Here's a solution that tracks consecutive characters and finds the maximum: const str = 'tdfdffddffsdsfffffsdsdsddddd'; const findConsecutiveCount = (str = '') => { if (!str) return ['', 0]; ...
Read MoreCounting the number of letters that occupy their positions in the alphabets for array of strings using JavaScript
We are required to write a JavaScript function that takes in an array of strings of English lowercase alphabets. Our function should map the input array to an array whose corresponding elements are the count of the number of characters that had the same 1-based index in the string as their 1-based index in the alphabets. For instance, this count for the string 'akcle' will be 3 because the characters 'a', 'c' and 'e' have 1-based index of 1, 3 and 5 respectively both in the string and the English alphabets. Understanding the Problem We need ...
Read MoreSorting 2-D array of strings and finding the diagonal element using JavaScript
Problem We are required to write a JavaScript function that takes in an array of n strings. Each string in the array consists of exactly n characters. Our function should first sort the array in alphabetical order, then return the string formed by the characters present at the principal diagonal starting from the top left corner. Understanding the Process The solution involves two main steps: Sort the array of strings alphabetically Extract diagonal characters where row index equals column index (i === j) Example ...
Read More