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
Articles by AmitDiwan
Page 333 of 840
Swapping string case using a binary number in JavaScript
We need to write a JavaScript function that takes a string and a number, then uses the binary representation of the number to determine which alphabetic characters should have their case swapped. Problem Each bit in the binary representation of the number specifies whether to swap the case for each alphabetic character: If the bit is 1, swap the case (lowercase → uppercase, uppercase → lowercase) If the bit is 0, leave the character as is When we reach the end of the binary representation, start again from the first bit Non-alphabetic characters remain unchanged ...
Read MoreReturning a decimal that have 1s in its binary form only at indices specified by array in JavaScript
Problem We are required to write a JavaScript function that takes in an array of unique non-negative integers. Our function should return a 32-bit integer such that the integer, in its binary representation, has 1 at only those indexes (counted from right) which are in the sequence. Example Following is the code − const arr = [1, 2, 0, 4]; const buildDecimal = (arr = []) => { const bitArr = Array(31).fill(0); let res = 0; arr.forEach(el => { ...
Read MoreSmallest possible length constituting greatest frequency in JavaScript
This problem asks us to find the smallest contiguous subarray that contains the maximum frequency of any element found in the entire array. We need to track element frequencies and their position ranges to determine the shortest span. Problem Understanding Given an array, we must: Find the maximum frequency of any element in the entire array Identify the shortest contiguous subarray where some element appears with this maximum frequency Return the length of this shortest subarray Example Walkthrough For the array [55, 77, 77, 88, 55]: Element 55 appears twice (positions 0 and ...
Read MoreHow does Promise.any() method differs from Promise.race() method in JavaScript?
In this article, you will understand how Promise.any() method differs from Promise.race() method in JavaScript. The Promise.any() method resolves when the first promise succeeds (fulfills), ignoring rejections until all promises fail. The Promise.race() method settles when the first promise completes, regardless of whether it succeeds or fails. Promise.any() Method Promise.any() waits for the first successful promise and ignores rejections. If all promises reject, it throws an AggregateError. console.log("Defining three promise values: promise1, promise2 and promise3"); const promise1 = Promise.resolve(1); const promise2 = new Promise((resolve, reject) => { setTimeout(resolve, 200, 'Promise Two'); }); ...
Read MoreNumber of vowels within an array in JavaScript
We are required to write a JavaScript function that takes in an array of strings, (they may be a single character or greater than that). Our function should simply count all the vowels contained in the array. Example Let us write the code − const arr = ['Amy', 'Dolly', 'Jason', 'Madison', 'Patricia']; const countVowels = (arr = []) => { const legend = 'aeiou'; const isVowel = c => legend.includes(c.toLowerCase()); let count = 0; arr.forEach(el => { for(let ...
Read MoreReturning a range or a number that specifies the square root of a number in JavaScript
We need to write a JavaScript function that takes an integer n and returns either the exact square root (if n is a perfect square) or a range indicating where the square root falls between two consecutive integers. Problem Requirements Return integer k if n is a perfect square, such that k * k == n Return a range (k, k+1) if n is not a perfect square, where k * k < n and n < (k+1) * (k+1) Solution Approach We use Math.sqrt() to calculate the ...
Read MoreAre bits alternating in integer using JavaScript?
We are required to write a JavaScript function that takes in an integer, num, as the first and the only argument. Our function should check whether the binary representation of num has alternating bits − namely, if two adjacent bits will always have different values. For example, if the input to the function is 5, the output should be true because the binary form of 5 is 101 which has alternating bits (1-0-1). Understanding Alternating Bits Alternating bits mean that no two adjacent bits in the binary representation are the same. For instance: 5 ...
Read MoreHow to iterate an array of objects and build a new one in JavaScript ?
Suppose, we have an array of objects like this: const arr = [ { "customer": "Customer 1", "project": "1" }, { "customer": "Customer 2", "project": "2" }, { "customer": "Customer 2", ...
Read MoreAdding paragraph tag to substrings within a string in JavaScript
We are required to write a JavaScript function that takes in a string str as the first argument and an array of strings, arr as the second argument. We need to add a closed pair of paragraph tag and to wrap the substrings in str that exist in arr. If two such substrings overlap, we need to wrap them together by only one pair of closed paragraph tag. Also, if two substrings wrapped by paragraph tags are consecutive, we need to combine them. For example, if the input string and the array are: const ...
Read MoreAccumulating some value over using a callback function and initial value in JavaScript
We need to write a JavaScript function that takes an array, a callback function, and an initial value to accumulate values over array iteration, similar to Array.prototype.reduce(). Problem The goal is to create a custom reduce function that processes each array element through a callback function, accumulating a result from an initial value. Understanding Array Reduce The reduce method applies a callback function to each array element, building up a single result value. The callback receives the accumulator (previous result) and current element as parameters. Custom Reduce Implementation const arr = [1, 2, ...
Read More