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 162 of 534
Smallest number formed by shuffling one digit at most in JavaScript
We need to write a JavaScript function that takes a positive number and can perform at most one operation: remove a digit from any position and insert it at another position to create the smallest possible number. Problem Statement Given a positive number, we can choose any digit, remove it from its current position, and insert it at any other position (including the same position). The goal is to find the smallest number possible with this single operation. Algorithm Approach The strategy is to find the smallest digit in the number and move it to the ...
Read MoreReturning the value of (count of positive / sum of negatives) for an array in JavaScript
Problem We are required to write a JavaScript function that takes in an array of integers (positives and negatives) and our function should return an array, where the first element is the count of positives numbers and the second element is sum of negative numbers. Using Array.reduce() Method The most efficient approach uses the reduce() method to iterate through the array once and accumulate both values simultaneously. const arr = [1, 2, 1, -2, -4, 2, -6, 2, -4, 9]; const posNeg = (arr = []) => { const result = arr.reduce((acc, ...
Read MoreValidating a boggle word using JavaScript
A Boggle board is a 2D array of individual characters. We need to validate whether a given word can be formed by connecting adjacent cells (horizontally, vertically, or diagonally) without reusing any previously used cells. Problem Given a Boggle board like this: const board = [ ["I", "L", "A", "W"], ["B", "N", "G", "E"], ["I", "U", "A", "O"], ["A", "S", "R", "L"] ]; We need to check if words like "BINGO" or "LINGO" are valid. Valid words are ...
Read MoreNext multiple of 5 and binary concatenation in JavaScript
We need to write a JavaScript function that takes a number and finds the next higher multiple of 5 by concatenating the shortest possible binary string to the number's binary representation. Problem Given a number n, we want to: Convert n to binary representation Append the shortest binary string to make it divisible by 5 Return the resulting decimal number How It Works The algorithm generates all possible binary combinations of increasing length until it finds one that, when concatenated to the original number's binary form, creates a number divisible by 5. Example ...
Read MoreFinding the longest non-negative sum sequence using JavaScript
Problem We are required to write a JavaScript function that takes in an array containing a sequence of integers, each element of which contains a possible value ranging between -1 and 1. Our function should return the size of the longest sub-section of that sequence with a sum of zero or higher. Understanding the Algorithm The solution uses a prefix sum approach with an index mapping technique. It tracks cumulative sums and uses an array to store the first occurrence of each sum value, allowing efficient calculation of subarray lengths. Example Following is the ...
Read MoreFinding all solutions of a Diophantine equation using JavaScript
Problem We need to write a JavaScript function that takes a number n and finds all integer solutions (x, y) for the Diophantine equation: x^2 - 4y^2 = n The function should return an array of all such pairs [x, y]. Mathematical Approach To solve x² - 4y² = n, we can factorize it as: x^2 - 4y^2 = (x + 2y)(x - 2y) = n Let a = x - 2y and b = x + 2y, then: a × b = n x = (a + ...
Read MoreFinding one missing number in a scrambled sequence using JavaScript
We are required to write a JavaScript function that takes in an array of numbers containing numbers from 1 to n. The problem is that one number from the array goes missing and the array is not sorted as well. Our function should find and return that one number missing from the array. Problem Statement Given an array of numbers from 1 to n with one missing number, find the missing number. The array is not sorted and contains n-1 elements instead of n. Using Sum Formula Method The most efficient approach uses the mathematical formula ...
Read MoreIntegers have sum of squared divisors as perfect square in JavaScript
Problem We need to write a JavaScript function that takes a range specified by two numbers m and n, and finds all integers between m and n where the sum of their squared divisors is itself a perfect square. The function should return an array of subarrays, where each subarray contains the number and the sum of its squared divisors. Understanding the Concept For a number to qualify, we need to: Find all divisors of the number Square each divisor and sum them up ...
Read MoreFinding the length of the diagonal of a cuboid using JavaScript
We need to write a JavaScript function that calculates the diagonal length of a cuboid (rectangular box) given its length, width, and height dimensions. Problem We are required to write a JavaScript function that takes in the length, width and height of a cuboid and return the length of its diagonal. Formula The space diagonal of a cuboid is calculated using the 3D Pythagorean theorem: diagonal = √(length² + width² + height²) Example Following is the code: const height = 10; const width = 12; const length = 15; ...
Read MoreSquared and square rooted sum of numbers of an array in JavaScript
Problem We are required to write a JavaScript function that takes in an array of numbers. Our function should take each number in the array and square it if it is even, or square root the number if it is odd and then return the sum of all the new numbers rounded to two decimal places. Example Following is the code − const arr = [45, 2, 13, 5, 14, 1, 20]; const squareAndRootSum = (arr = []) => { const res = arr.map(el => { ...
Read More