Javascript Articles

Page 82 of 534

Counting the number of palindromes that can be constructed from a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 391 Views

We are required to write a JavaScript function that takes in a string of characters as the first argument, say str, and a number, say num, as the second argument. The function should count the number of palindrome strings all exactly of length num can be constructed from the given string str. The function should then finally return the count. Problem Example If the input string and the number is: const str = 'ij'; const num = 4; Then the output should be: 4 because those four possible palindrome ...

Read More

Counting How Many Numbers Are Smaller Than the Current Number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 316 Views

In JavaScript, we often need to count how many elements in an array are smaller than each current element. This creates a new array where each position contains the count of smaller elements from the original array. Problem Statement Given an array of numbers, create a new array where each element represents the count of numbers smaller than the corresponding element in the original array. For example, if the input array is: [2, 7, 3, 1, 56, 4, 7, 8] The output should be: [1, 4, 2, 0, 7, 3, 4, ...

Read More

Finding two numbers that produce equal to the sum of rest in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 351 Views

We have a sequence of numbers from 1 to any arbitrary number. We need to find pairs of numbers (m and n) from this sequence such that the sum of all remaining numbers equals the product of these two numbers. Problem Statement Given a number num, find all pairs [m, n] where: sum(1 to num) - (m + n) = m * n For example, if num = 10: Sum of 1 to 10 = 55 For pair [6, 7]: 55 - (6 + 7) = 42 and 6 × 7 = 42 ...

Read More

Number of digits that divide the complete number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 202 Views

We are required to write a JavaScript function that takes in a number as the first and the only argument. The function should count and return the number of digits present in the number that completely divide the number. For example, if the input number is 148, the output should be 2 because 148 is exactly divisible by 1 and 4 but not 8. Example Input and Output Input: 148 Output: 2 This is because: 148 ÷ 1 = 148 (divisible) 148 ÷ 4 = 37 (divisible) ...

Read More

Determining a pangram string in JavaScript

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

A pangram is a string that contains every letter of the English alphabet at least once. Examples include "The quick brown fox jumps over the lazy dog" and "Pack my box with five dozen liquor jugs". We need to write a JavaScript function that determines whether a given string is a pangram. For this problem, we'll focus on the 26 letters of the English alphabet, ignoring case sensitivity. How It Works The algorithm creates an array of all 26 letters, then iterates through the input string. For each letter found in the string, it removes that letter ...

Read More

Calculating the weight of a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 921 Views

In JavaScript, calculating the weight of a string means finding the sum of alphabetical positions of all characters. Each letter's weight is its position in the alphabet (a=1, b=2, c=3, etc.). Understanding Character Weight The weight of an English alphabet character is its 1-based index position: 'a' has weight 1 'b' has weight 2 'c' has weight 3 'z' has weight 26 Method 1: Using indexOf() This approach uses a reference string to find each character's position: const str = 'this is a string'; const calculateWeight = (str = '') ...

Read More

2 Key keyboard problem in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 255 Views

In this problem, we start with a notepad containing one character 'A' and need to reach exactly 'n' characters using only two operations: Copy All and Paste. The goal is to find the minimum number of steps required. Problem Understanding Given a notepad with one 'A', we can perform: Copy All − Copy all characters currently on the notepad Paste − Paste the previously copied characters We need to find the minimum steps to get exactly 'n' A's on the notepad. Example Walkthrough For ...

Read More

Squaring every digit of a number using split() in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 263 Views

We are required to write a JavaScript function that takes in a number as the first and the only argument. The function should then square every digit of the number, append them and yield the new number. For example − If the input number is − const num = 12349; Then the output should be − const output = 1491681; because '1' + '4' + '9' + '16' + '81' = 1491681 How It Works The solution uses split('') to convert each digit into an array element, then ...

Read More

Calculating time taken to type words in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 338 Views

In JavaScript, we can calculate the time taken to type words on a custom keyboard where keys are arranged alphabetically (a-z) instead of the traditional QWERTY layout. Problem Setup We make two key assumptions: The fingertip starts at index 0 (key 'a') Time to move between keys equals the absolute difference of their positions. For example, moving from 'a' (index 0) to 'k' (index 10) takes |0 - 10| = 10 time units Example Walkthrough For the string 'dab', the movements are: 'a' ...

Read More

Map anagrams to one another in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 267 Views

One array is an anagram of another if we can rearrange the elements of that array to achieve the other array. For example: [1, 2, 3] and [2, 1, 3] are anagrams of each other. We need to write a JavaScript function that takes two anagram arrays and returns a mapping array. The mapping array contains the index of each element from the first array as it appears in the second array. Problem Example If the two input arrays are: const arr1 = [23, 39, 57, 43, 61]; const arr2 = ...

Read More
Showing 811–820 of 5,340 articles
« Prev 1 80 81 82 83 84 534 Next »
Advertisements