Web Development Articles

Page 259 of 801

Number of carries required while adding two numbers in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 413 Views

When adding two numbers on paper, we sometimes need to "carry" a digit to the next column when the sum of digits exceeds 9. This article shows how to count the total number of carries required during addition. Problem We need to write a JavaScript function that takes two numbers and counts how many carries are needed when adding them digit by digit, just like manual addition on paper. For example, when adding 179 and 284: 9 + 4 = 13 (carry 1) 7 + 8 + 1 = 16 (carry 1) 1 + 2 + ...

Read More

Finding the immediate bigger number formed with the same digits in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 371 Views

We need to write a JavaScript function that takes a number and rearranges its digits to form the smallest number that is just bigger than the input number using the same digits. For instance, if the input number is 112, the output should be 121. If no bigger number can be formed (like 321), we return -1. Algorithm Approach The brute force approach involves: Find the maximum possible number by sorting digits in descending order Check each number from input+1 to maximum Return the first number that uses the same digits Example Implementation ...

Read More

Number difference after interchanging their first digits in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 128 Views

We are required to write a JavaScript function that takes in an array of exactly two numbers. Our function should return the absolute difference between the numbers after interchanging their first digits. For instance, for the array [105, 413]: Original numbers: 105 and 413 After interchanging first digits: 405 and 113 The difference will be: |405 - 113| = 292 Algorithm The approach involves converting numbers to strings, extracting first digits, creating new numbers with swapped first digits, and calculating the absolute difference. Example const arr = [105, 413]; const ...

Read More

Searching for target string in a strangely sorted array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 196 Views

We are required to write a JavaScript function that takes in a word target and an array of sorted(by length(increasing), number of uppercase letters(decreasing), natural order) unique words which always contains the target. The task of our function is to find the index(0 based) of the target in the array of words, which would always be in the list. Understanding the Sorting Pattern The array is sorted by three criteria in order: Length: Shorter strings come first Uppercase count: Among same-length strings, more uppercase letters come first ...

Read More

Finding reflection of a point relative to another point in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 447 Views

Point reflection, also known as point symmetry, is a geometric transformation where a point P is reflected across a midpoint Q to create a new point P' that is equidistant from Q but in the opposite direction. P(6, -4) Q(11, 5) ...

Read More

Encoding a number string into a string of 0s and 1s in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 380 Views

We need to write a JavaScript function that encodes a decimal number string into binary based on specific rules. For each digit, we determine the number of bits required, prepend (k-1) zeros followed by 1, then append the digit's binary representation. Encoding Rules For each digit d in the number string: Let k be the number of bits needed to represent d in binary Write (k-1) zeros followed by the digit 1 Write digit d as a binary string Concatenate ...

Read More

Retrieving n smallest numbers from an array in their original order in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 436 Views

We need to write a JavaScript function that takes an array of numbers and a number n, then returns the n smallest numbers while preserving their original order in the array. Problem Given an array of numbers and a value n, we want to find the n smallest numbers but maintain their relative positions from the original array. The result should not be sorted numerically but should appear in the same order as they existed in the input array. Example Here's how we can solve this problem: const arr = [6, 3, 4, 1, ...

Read More

Sum array of rational numbers and returning the result in simplest form in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 224 Views

We are required to write a JavaScript function that takes in an array of exactly two subarrays with two numbers each. Both the subarrays represent a rational number in fractional form. Our function should add the rational numbers and return a new array of two numbers representing the simplest form of the added rational number. Problem When adding two fractions like 1/2 + 1/3, we need to: Find a common denominator Add the numerators Simplify the result using the Greatest Common Factor (GCD) Solution The mathematical formula for adding fractions is: a/b + ...

Read More

Decrypting source message from a code based on some algorithm in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 220 Views

We need to write a JavaScript function that decrypts a message by reversing an encryption algorithm. The encryption process involves reversing the string and replacing letters with their ASCII codes in quotes. Understanding the Encryption Algorithm The original encryption algorithm works as follows: Reverse the message string Replace every letter with its ASCII code in quotes (A becomes '65', h becomes '104') Keep digits and spaces unchanged The Decryption Function To decrypt, we need to reverse this process by applying the same algorithm to the encrypted message: const str = '12 hello ...

Read More

Finding the sum of floors covered by an elevator in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 184 Views

Problem We need to write a JavaScript function that calculates the total number of floors covered by an elevator. The function takes an array representing the floor numbers where the elevator stopped, and returns the sum of distances traveled between consecutive stops. Understanding the Logic The elevator covers floors by moving between consecutive stops. If it goes from floor 7 to floor 1, it covers 6 floors (7-1). If it then goes from floor 1 to floor 7, it covers another 6 floors (7-1). The total distance is the sum of absolute differences between consecutive floors. ...

Read More
Showing 2581–2590 of 8,010 articles
« Prev 1 257 258 259 260 261 801 Next »
Advertisements