Web Development Articles

Page 466 of 801

Corner digit number difference - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 168 Views

We are required to write a JavaScript function that takes in a number, constructs a new number from the first and last digit of that number and returns the difference between the original number and the number thus formed. For example: If the input is 34567 Then the corner digits number will be: 37 And the output will be: 34530 Algorithm The solution involves extracting the first and last digits, combining them to form a corner number, then calculating the difference. Example Following is the code: ...

Read More

Write an algorithm that takes an array and moves all of the zeros to the end JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 357 Views

We need to write a function that moves all zeros in an array to the end while maintaining the order of non-zero elements. This algorithm should work in-place without using extra space. Problem Overview Given an array with mixed numbers including zeros, we want to push all zeros to the end while keeping other elements in their relative order. For example, [1, 0, 3, 0, 5] should become [1, 3, 5, 0, 0]. Method 1: Using splice() and push() This approach removes zeros when found and adds them to the end: const arr = ...

Read More

Check three consecutive numbers - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 551 Views

We are required to write a JavaScript function that takes in a Number, say n, and we are required to check whether there exist such three consecutive natural numbers (not decimal/floating point) whose sum equals to n. If there exist such numbers, our function should return them, otherwise it should return false. Following is the code − How It Works For three consecutive numbers to sum to n, we need: x + (x+1) + (x+2) = n, which simplifies to 3x + 3 = n. Therefore, n must be divisible by 3 and greater than 5 (since ...

Read More

Is there any more efficient way to code this "2 Sum" Questions JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 324 Views

Our job is to write a function that solves the two-sum problem in at most linear time. Two Sum Problem Given an array of integers, we have to find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers that add up to the target, and if no two elements add up to the target, our function should return an empty array. Brute Force Approach - O(n²) The naive approach uses nested loops to check every pair of elements: const bruteForceTwoSum ...

Read More

Sum of distinct elements of an array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 566 Views

We are required to write a JavaScript function that takes in one such array and counts the sum of all distinct elements of the array. For example: Suppose, we have an array of numbers like this − const arr = [1, 5, 2, 1, 2, 3, 4, 5, 7, 8, 7, 1]; The distinct elements are: 1, 5, 2, 3, 4, 7, 8. Their sum is: 1 + 5 + 2 + 3 + 4 + 7 + 8 = 30. Using lastIndexOf() Method This approach checks if the current index matches the ...

Read More

Converting string to MORSE code in JavaScript

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

What is Morse Code? Morse code is a method used in telecommunications to encode text characters as standardized sequences of two different signal durations, called dots and dashes. Each letter of the alphabet has a unique pattern of dots (.) and dashes (-). To convert a string to Morse code in JavaScript, we need an object that maps all alphabets to their Morse code equivalents, then iterate through the input string to build the encoded result. Morse Code Map First, let's create an object containing all alphabet-to-Morse mappings: const morseCode = { ...

Read More

Left right subarray sum product - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 191 Views

We are required to write a JavaScript function that takes in an array of numbers of length N (N should be even) and divides the array into two sub-arrays (left and right) containing N/2 elements each, calculates the sum of each sub-array, and then multiplies both sums together. For example: If the input array is: const arr = [1, 2, 3, 4] The calculation would be: Left subarray: [1, 2] → sum = 1 + 2 = 3 Right subarray: [3, 4] → sum = 3 + 4 = 7 Product: 3 × ...

Read More

Return the largest array between arrays JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 391 Views

We have an array of arrays that contains some numbers, we have to write a function that takes in that array and returns the index of the subarray that has the maximum sum. If more than one subarray has the same maximum sum, we have to return the index of first such subarray. Problem Overview Given multiple arrays nested within a main array, we need to: Calculate the sum of each subarray Find which subarray has the largest sum Return the index of that subarray ...

Read More

Adjacent elements of array whose sum is closest to 0 - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 191 Views

We are required to write a JavaScript function that takes in an array of numbers and returns a subarray of two adjacent elements from the original array whose sum is closest to 0. If the length of the array is less than 2, we should return the whole array. Problem Statement For example: If the input array is − const arr = [4, 4, 12, 3, 3, 1, 5, -4, 2, 2]; Here, the sum of adjacent pair [5, -4] is 1 which is closest to 0 for any two adjacent elements of ...

Read More

Create a Calculator function in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 914 Views

We have to write a function, say calculator() that takes in one of the four characters (+, - , *, / ) as the first argument and any number of Number literals after that. Our job is to perform the operation specified as the first argument over those numbers and return the result. If the operation is multiplication or addition, we are required to perform the same operation with every element. But if the operation is subtraction or division, we have to consider the first element as neutral and subtract all other elements from it or divide it by ...

Read More
Showing 4651–4660 of 8,010 articles
« Prev 1 464 465 466 467 468 801 Next »
Advertisements