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 459 of 801
Validate input: replace all 'a' with '@' and 'i' with '!'JavaScript
We need to write a function validate() that takes a string as input and returns a new string where all occurrences of 'a' are replaced with '@' and all occurrences of 'i' are replaced with '!'. This is a classic string manipulation problem that can be solved using different approaches. Let's explore the most common methods. Using For Loop The traditional approach iterates through each character and builds a new string: const string = 'Hello, is it raining in Amsterdam?'; const validate = (str) => { let validatedString = ''; ...
Read MoreHow Do I Find the Largest Number in a 3D JavaScript Array?
To find the largest number in a 3D JavaScript array, we can use flat(Infinity) to flatten all nested levels, then apply Math.max() to find the maximum value. The Problem 3D arrays contain multiple levels of nested arrays, making it difficult to directly compare all numbers. We need to flatten the structure first. var theValuesIn3DArray = [75, [18, 89], [56, [97], [99]]]; Using flat() with Math.max() The flat(Infinity) method flattens all nested levels, and the spread operator ... passes individual elements to Math.max(). var theValuesIn3DArray = [75, [18, 89], [56, [97], [99]]]; ...
Read MoreHow to sum elements at the same index in array of arrays into a single array? JavaScript
We have an array of arrays and are required to write a function that takes in this array and returns a new array that represents the sum of corresponding elements of original array. If the original array is: [ [43, 2, 21], [1, 2, 4, 54], [5, 84, 2], [11, 5, 3, 1] ] Then the output should be: [60, 93, 30, 55] Let's explore different approaches to solve this problem. Using forEach() Method The most straightforward approach is to iterate through each sub-array and accumulate ...
Read MoreChecking for co-prime numbers - JavaScript
Two numbers are said to be co-primes if there exists no common prime factor amongst them (1 is not a prime number). For example: 4 and 5 are co-primes 9 and 14 are co-primes 18 and 35 are co-primes 21 and 57 are not co-prime because they have 3 as the common prime factor We are required to write a function that takes in two numbers and returns true if they are co-primes otherwise returns false. Understanding Co-prime Numbers Two numbers are co-prime if their greatest common divisor (GCD) is 1. This means ...
Read MoreGet the property of the difference between two objects in JavaScript
When working with JavaScript objects, you often need to compare them and identify which properties have different values. This is useful for tracking changes, validation, or debugging purposes. Problem Overview Given two objects with similar key-value pairs, we need to write a function that finds the first key with different values between the objects. If all values match, the function should return -1. Here are sample objects to demonstrate the concept: const obj1 = { name: 'Rahul Sharma', id: '12342fe4554ggf', isEmployed: true, ...
Read MoreFind even odd index digit difference - JavaScript
We are required to write a JavaScript function that takes in a number and returns the difference of the sums of the digits at even place and the digits at odd place. Understanding the Problem Given a number, we need to: Sum all digits at even positions (0, 2, 4, ...) Sum all digits at odd positions (1, 3, 5, ...) Return the absolute difference between these sums Example For the number 345336: Position 0: 6 (even) Position 1: 3 (odd) Position 2: 3 (even) Position 3: 5 (odd) Position 4: 4 (even) ...
Read MoreMerge two arrays with alternating Values in JavaScript
Merging two arrays with alternating values means creating a new array that picks elements from each array in turn: first element from array1, first element from array2, second element from array1, and so on. Using While Loop with Index Tracking This approach uses separate counters to track positions in each array and alternates between them: const arr1 = [34, 21, 2, 56, 17]; const arr2 = [12, 86, 1, 54, 28]; let run = 0, first = 0, second = 0; const newArr = []; while(run < arr1.length + arr2.length){ if(first ...
Read MoreSum all duplicate value in array - JavaScript
We are required to write a JavaScript function that takes in an array of numbers with duplicate entries and sums all the duplicate entries to one index. For example − If the input array is − const input = [1, 3, 1, 3, 5, 7, 5, 4]; Then the output should be − const output = [2, 6, 7, 10, 4]; Using Map to Track and Sum Duplicates The most efficient approach is to use a Map to count occurrences and then multiply each unique value by its count: ...
Read MoreHighest and lowest in an array JavaScript
In JavaScript, finding the difference between the highest and lowest values in an array is a common task. This article demonstrates multiple approaches to calculate this difference efficiently. Using Math.max() and Math.min() with Spread Operator The most straightforward approach uses the spread operator with Math.max() and Math.min(): const arr = [23, 54, 65, 76, 87, 87, 431, -6, 22, 4, -454]; const arrayDifference = (arr) => { const max = Math.max(...arr); const min = Math.min(...arr); return max - min; }; console.log("Array:", arr); ...
Read MoreInverting signs in array - JavaScript
We are required to write a JavaScript function that takes in an array of positive as well as negative Numbers and changes the positive numbers to corresponding negative numbers and the negative numbers to corresponding positive numbers in place. Let's write the code for this function — Example Following is the code — const arr = [12, 5, 3, -1, 54, -43, -2, 34, -1, 4, -4]; const changeSign = arr => { arr.forEach((el, ind) => { arr[ind] *= -1; ...
Read More