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 304 of 840
Counting adjacent pairs of words in JavaScript
We are required to write a JavaScript function that takes in a string str that represents a sentence as the only argument. Our function should count and return the adjacent pair of identical words present in the string str. Our function should check the words ignoring their case, which means 'it' and 'It' should be counted as identical. Problem Statement For example, if the input to the function is: const str = 'This this is a a sample string'; The expected output should be: 2 Output Explanation: Because the ...
Read MoreRepeated sum of Number's digits in JavaScript
We are required to write a JavaScript function that recursively sums up the digits of a number until it reduces to a single digit number. We are required to do so without converting the number to String or any other data type. How It Works The algorithm uses two functions: sumDigit(): Recursively extracts and sums all digits of a number sumRepeatedly(): Repeatedly calls sumDigit() until result is a single digit Example Let's implement the solution step by step: const num = 546767643; const sumDigit = (num, sum = 0) => ...
Read MoreCalculating Josephus Permutations efficiently in JavaScript
This problem takes its name from arguably the most important event in the life of the ancient historian Josephus. According to his tale, he and his 40 soldiers were trapped in a cave by the Romans during a siege. Refusing to surrender to the enemy, they instead opted for mass suicide, with a twist — they formed a circle and proceeded to kill one man every three, until one last man was left (and that it was supposed to kill himself to end the act). Josephus and another man were the last two and, as we now know ...
Read MoreSplitting number into n parts close to each other in JavaScript
We are required to write a JavaScript function that takes in a number, num, as the first argument and another number, parts, as the second argument. Our function should split the number num into exactly parts numbers while keeping these conditions in mind: The numbers should be as close as possible The numbers should be even (if possible) The ordering of numbers is not important. Problem Analysis To split a number into n parts as evenly as possible, we need to: Calculate ...
Read MoreHow to split comma and semicolon separated string into a two-dimensional array in JavaScript ?
Let's say we have a variable "users" that contains the following string of text where each user is separated by a semicolon and each attribute of each users is separated by a comma − const users = 'Bob, 1234, Bob@example.com;Mark, 5678, Mark@example.com'; We are required to write a JavaScript function that takes in one such string and splits this into a multidimensional array that looks like this − const arr = [ ['Bob', 1234, 'Bob@example.com'], ['Mark', 5678, 'Mark@example.com'] ]; Method 1: Using split() and for Loop ...
Read MoreTribonacci series in JavaScript
The tribonacci sequence is a generalization of the Fibonacci sequence where each term is the sum of the three preceding terms. Unlike Fibonacci which uses two preceding terms, tribonacci uses three. The standard tribonacci series starts with 0, 1, 1, and each subsequent term is the sum of the previous three terms: 0, 1, 1, 2, 4, 7, 13, 24, 44, 81, 149... How It Works Each term after the first three follows the formula: T(n) = T(n-1) + T(n-2) + T(n-3) Where T(0) = 0, T(1) = 1, T(2) = 1 ...
Read MoreCrack Alphabets fight problem in JavaScript
Problem Consider a situation where armies of two teams of alphabets are fighting each other. Each soldier has a specific weight based on their letter: Team A (Positive Weights) Soldier Weight A ...
Read MoreSquared concatenation of a Number in JavaScript
We are required to write a JavaScript function that takes in a number and returns a new number in which all the digits of the original number are squared and concatenated. For example: If the number is − 99 Then the output should be − 8181 because 9² is 81 and 9² is 81, so concatenating them gives 8181. Example Let's implement this function step by step: const num = 9119; const squared = num => { const numStr = String(num); ...
Read MoreFinding the least common multiple of a range of numbers in JavaScript?
We are required to write a JavaScript function that takes in an array of exactly two numbers specifying a range. The function should then calculate the least common multiple of all the numbers within that range and return the final result. Understanding LCM and GCD To find the LCM of multiple numbers, we first need helper functions for GCD (Greatest Common Divisor) and LCM of two numbers: GCD: The largest number that divides both numbers evenly LCM: The smallest positive number that is divisible by both numbers Formula: LCM(a, b) = (a × b) / ...
Read MoreFinding n subarrays with equal sum in JavaScript
We are required to write a JavaScript function that takes in an array of integers as the first argument and an integer as the second argument. The function should check whether we can create n (second argument) subarrays from the original array such that all the subarrays have the equal sum. For example − If the inputs are − const arr = [4, 3, 2, 3, 5, 2, 1]; const num = 4; The output should be true because the subarrays are: [5], [1, 4], [2, 3], [2, 3] all having sum equal ...
Read More