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
Web Development Articles
Page 476 of 801
Matching strings for similar characters - JavaScript
We are required to write a JavaScript function that accepts two strings and a number n. The function matches the two strings i.e., it checks if the two strings contains the same characters. The function returns true if both the strings contain the same character irrespective of their order or if they contain at most n different characters, else the function should return false. Example Following is the code − const str = 'some random text'; const str2 = 'some r@ndom text'; const deviationMatching = (first, second, num) => { ...
Read MoreReverse sum array JavaScript
We are required to write a function, say reverseSum() that takes in two arrays of Numbers, let's say first and second and returns a new array that contains: Sum of first element of first array and last element of second array as first element, sum of second element of first array and second last element of second array, and so on. When any of the array runs out of element before the other, we simply push all the remaining elements to the array. Syntax const reverseSum = ...
Read MoreSolution to the clumsy factorial problem in JavaScript
Normally, the factorial of a positive integer n is the product of all positive integers less than or equal to n. For example, factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1. We instead make a clumsy factorial: using the integers in decreasing order, we swap out the multiply operations for a fixed rotation of operations: multiply (*), divide (/), add (+) and subtract (-) in this order. For example, clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + ...
Read MoreSplitting last n digits of each value in the array in JavaScript
We have an array of numbers and need to extract the last n digits from each value. If a number has fewer than n digits, it remains unchanged. const arr = [56768, 5465, 5467, 3, 878, 878, 34435, 78799]; console.log("Original array:", arr); Original array: [ 56768, 5465, 5467, 3, 878, 878, 34435, 78799 ] We need a function that takes an array and a number n, then returns the last n digits of each element. If an element has fewer than n digits, it stays the same. Example with n = 2 ...
Read MoreReverse index value sum of array in JavaScript
Suppose we have an array of numbers like this: const arr = [3, 6, 7, 3, 1, 4, 4, 3, 6, 7]; This array contains 10 elements, so the index of the last element is 9. We need to write a function that calculates the sum of each element multiplied by its reverse index position. The reverse index means we multiply each element by (array.length - currentIndex - 1). For our example: // Element at index 0: 3 * (10-0-1) = 3 * 9 = 27 // Element at index 1: 6 * ...
Read MoreAdding two values at a time from an array - JavaScript
Let's say, we are required to write a JavaScript function that takes in an array of Numbers and returns a new array with elements as sum of two consecutive elements from the original array. For example, if the input array is − const arr = [3, 6, 3, 87, 3, 23, 2, 2, 6, 8]; Then the output should be − const output = [9, 90, 26, 4, 14]; Example Following is the code − const arr = [3, 6, 3, 87, 3, 23, 2, 2, 6, 8]; ...
Read MoreSearching objects by value in JavaScript
When working with complex JavaScript objects, you often need to find all keys that contain a specific value. This is particularly useful for nested objects where the same value might appear at multiple levels. Consider this example object with nested structure: const obj = { "name": "Vivek Sharma", "occupation": "Software Engineer", "age": 23, "contacts": [{ "name": "Mukul Sharma", "occupation": "Software Engineer", ...
Read MoreJavaScript array: Find all elements that appear more than n times
In JavaScript, you can find all elements that appear more than n times in an array using various approaches. This is useful for data analysis, finding duplicates, or filtering frequent items. Problem Overview Given an array of numbers or strings with repeated entries, we need to write a function that takes a positive integer n and returns all elements that appear more than or equal to n times. Using Map() for Frequency Counting The most efficient approach uses a Map to track element frequencies, then filters elements that meet our criteria: const arr = ...
Read MoreCounting the clusters of positive numbers - JavaScript Arrays
Let's say, we have an array of numbers like this − const arr = [-1, -2, -1, 0, -1, -2, -1, -2, -1, 0, 1, 0]; We are required to write a JavaScript function that counts the consecutive groups of non-negative (positives and 0) numbers in the array. Like here we have consecutive non-negatives from index 3 to 3 (only one element, but still a cluster) which forms one group and then from index 9 to end of array forms the second group. Therefore, for this array, the function should return 2. How It Works ...
Read MoreFind the difference of largest and the smallest number in an array without sorting it in JavaScript
We have an array of Numbers that are arranged in pure random order. Our job is to write a function that takes in one such array of Numbers and returns the difference of greatest and smallest numbers present in it, but without sorting the array. We will use the Array.prototype.reduce() function to pick the smallest and greatest numbers from the array and later will return their difference. Using Array.reduce() Method The reduce() method allows us to iterate through the array once and track both maximum and minimum values simultaneously: const arr = [23, 65, 67, ...
Read More