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 350 of 840
Implementing block search in JavaScript
Block Search is a searching algorithm for sorted arrays that improves upon linear search by jumping ahead in fixed steps rather than checking every element. It combines the efficiency of skipping elements with linear search for the final location. How Block Search Works The algorithm divides the array into blocks of size √n and performs these steps: Jump through blocks of size √n until finding a block where the target might exist Perform linear search within that specific block Return the index if found, or -1 if not found Block Search ...
Read MoreBoolean Gates in JavaScript
We are required to write a JavaScript function that takes in an array of Boolean values and a logical operator. Our function should return a Boolean result based on sequentially applying the operator to the values in the array. Problem Given an array of Boolean values and a logical operator (AND, OR, XOR), we need to apply the operator sequentially to all values and return the final result. Solution Here's the implementation that handles three common Boolean operations: const array = [true, true, false]; const op = 'AND'; function logicalCalc(array, op) { ...
Read MoreRepresenting number as the power and product of primes in JavaScript
We need to write a JavaScript function that takes a positive integer and represents it as a product of prime powers. This process is called prime factorization. For a number n, our function should return a string in the format: n = "(p1**n1)(p2**n2)...(pk**nk)" Where p1, p2, p3...pk are prime numbers, n1, n2...nk are their powers, and ** represents exponentiation. Understanding Prime Factorization Prime factorization breaks down a number into its prime factors. For example, 12 = 2² × 3¹, which would be represented as "(2**2)(3)". Implementation Here's a corrected and improved implementation: ...
Read MoreDifference of digits at alternate indices in JavaScript
In JavaScript, we can calculate the difference between the sum of digits at even indices and the sum of digits at odd indices. This is useful for various mathematical operations and data validation scenarios. Problem Statement We need to write a JavaScript function that takes a number and returns the absolute difference between the sum of digits at even positions and the sum of digits at odd positions (counting from right to left, starting at index 0). Example For the number 123456: Even indices (0, 2, 4): digits 6, 4, 2 → sum = 12 ...
Read MoreFinding day of week from date (day, month, year) in JavaScript
We are required to write a JavaScript function that takes in three arguments, namely: day, month and year. Based on these three inputs, our function should find the day of the week on that date. For example: If the inputs are − day = 15, month = 8, year = 1993 Output Then the output should be − const output = 'Sunday' Using Built-in Date Object (Simple Method) The easiest approach is to use JavaScript's built-in Date object: function getDayOfWeek(day, month, year) { ...
Read MoreJavaScript - Determine all possible ways a group of values can be removed from a sequence
We are required to write a JavaScript function that determines how many different ways we can remove a group of values from a sequence, leaving the original sequence in order (stable), and making sure to remove only one instance of each value from the original sequence. For example − If the sequence array is − const arr = [1, 2, 1, 3, 1, 4, 4]; And the array to be removed is − const arr2 = [1, 4, 4]; Then there are three possible ways of doing this without disrupting the ...
Read MoreRecursive Staircase problem in JavaScript
The staircase problem is a classic dynamic programming problem in computer science. Given n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 or 2 stairs at a time, and we need to count the number of ways to reach the top. We'll write a JavaScript function that takes a number n representing the number of stairs and returns the total number of ways to climb them. Understanding the Problem This problem follows the Fibonacci sequence pattern. For n stairs: 1 stair: 1 way (take 1 step) ...
Read MoreMaximum absolute difference of the length of strings from two arrays in JavaScript
Problem We are required to write a JavaScript function that takes in two arrays, a1 and a2 of strings. Each string is composed with letters from a to z. Let x be any string in the first array and y be any string in the second array. Our function should find the value of − max(abs(length(x) − length(y))) Approach To find the maximum absolute difference, we need to: Find the longest string from both arrays Find the shortest string from both arrays ...
Read MoreSum all duplicate values in array in JavaScript
We need to write a JavaScript function that takes an array of numbers with duplicate entries and sums all duplicate values. Each unique number appears once in the result, multiplied by its frequency count. Problem Understanding For array [1, 3, 1, 3, 5, 7, 5, 3, 4]: 1 appears 2 times → 1 × 2 = 2 3 appears 3 times → 3 × 3 = 9 5 appears 2 times → 5 × 2 = 10 7 appears 1 time → 7 × 1 = 7 4 appears 1 time → 4 × 1 = 4 ...
Read MoreHow to group an array of objects by key in JavaScript
Suppose, we have an array of objects containing data about some cars like this: const arr = [ { 'make': 'audi', 'model': 'r8', 'year': '2012' }, { 'make': 'audi', 'model': 'rs5', 'year': '2013' }, { ...
Read More