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 481 of 840
Map an integer from decimal base to hexadecimal with custom mapping JavaScript
Usually when we convert a decimal to hexadecimal (base 16) we use the set 0123456789ABCDEF to map the number. We are required to write a function that does exactly the same but provides user the freedom to use any scale rather than the one mentioned above. For example − The hexadecimal notation of the decimal 363 is 16B But if the user decides to use, say, a scale 'qwertyuiopasdfgh' instead of '0123456789ABCDEF', the number 363, then will be represented by wus That's what we are required to do. So, let's do this by ...
Read MoreConvert nested array to string - JavaScript
Converting a nested array to a string in JavaScript involves flattening all nested elements and concatenating their values. This is commonly needed when processing complex data structures. Problem Statement We need to write a JavaScript function that takes a nested array of literals and converts it to a single string by concatenating all values, regardless of nesting depth. const arr = [ 'hello', [ 'world', 'how', [ 'are', 'you', [ ...
Read MoreFinding the difference between two arrays - JavaScript
Finding the difference between two arrays means identifying elements that exist in one array but not in the other. This is a common operation when comparing datasets or finding unique values. We have two arrays of numbers like these: const arr1 = [12, 54, 2, 4, 6, 34, 3]; const arr2 = [54, 2, 5, 12, 4, 1, 3, 34]; We need to write a JavaScript function that takes in two such arrays and returns the elements that are not common to both arrays. Using indexOf() Method The traditional approach uses nested loops ...
Read MoreJavaScript Check for case insensitive values?
JavaScript provides several methods to check for case insensitive values. The most common approaches use toLowerCase(), toUpperCase(), or regular expressions. Using toLowerCase() Method Convert both values to lowercase before comparison: let name1 = "JOHN"; let name2 = "john"; console.log(name1.toLowerCase() === name2.toLowerCase()); // true console.log("Hello".toLowerCase() === "HELLO".toLowerCase()); // true true true Using Regular Expression Use the i flag for case insensitive matching: let allNames = ['john', 'John', 'JOHN']; let makeRegularExpression = new RegExp(allNames.join("|"), "i"); let hasValue = makeRegularExpression.test("JOHN"); console.log("Is Present=" + hasValue); // Direct regex approach let ...
Read MoreConvert object to a Map - JavaScript
In JavaScript, you can convert an object to a Map using several approaches. Maps offer better performance for frequent additions and deletions compared to objects. Sample Object Let's start with this example object: const obj = { name: "Vikas", age: 45, occupation: "Frontend Developer", address: "Tilak Nagar, New Delhi", experience: 23, salary: "98000" }; Method 1: Using Object.entries() (Recommended) The most concise approach uses Object.entries() which returns key-value pairs ...
Read MoreMerging boolean array with AND operator - JavaScript
Let's say, we have an array of arrays of boolean like this − const arr = [[true, false, false], [false, false, false], [false, false, true]]; We are required to write a function that merges this array of arrays into a one-dimensional array by combining the corresponding elements of each subarray using the AND (&&) operator. Let's write the code for this function. We will be using Array.prototype.reduce() function to achieve this. Example Following is the code − const arr = [[true, false, false], [false, false, false], [false, false, true]]; const ...
Read MoreHow to make a function that returns the factorial of each integer in an array JavaScript
We are here required to write a JavaScript function that takes in an array of numbers and returns another array with the factorial of corresponding elements of the array. We will first write a recursive method that takes in a number and returns its factorial and then we will iterate over the array, calculating the factorial of each element of array and then finally we will return the new array of factorials. Therefore, let's write the code for this Creating the Factorial Function First, we'll create a recursive function to calculate the factorial of a single number: ...
Read MoreUpdating copied object also updates the parent object in JavaScript?
When copying objects in JavaScript, understanding shallow vs deep copying is crucial. Simply assigning an object creates a reference, not a true copy, which can lead to unexpected behavior where modifying the "copy" also updates the original. The Problem with Direct Assignment Direct assignment creates a reference to the same object in memory: var original = { name: 'John', age: 30 }; var copy = original; // This creates a reference, not a copy copy.name = 'Smith'; console.log("Original:", original); console.log("Copy:", copy); Original: { name: 'Smith', age: 30 } Copy: { name: ...
Read MoreAlternate addition multiplication in an array - JavaScript
We are required to write a JavaScript function that takes in an array of numbers and returns the alternative multiplicative sum of the elements. For example, if the array is: const arr = [1, 2, 4, 1, 2, 3, 4, 3]; Then the output should be calculated like this: 1*2 + 4*1 + 2*3 + 4*3 2 + 4 + 6 + 12 And the final output should be: 24 How It Works The algorithm pairs consecutive elements starting from index 0. Each pair is multiplied ...
Read MorePrime numbers upto n - JavaScript
Let's say, we are required to write a JavaScript function that takes in a number, say n, and returns an array containing all the prime numbers up to n. For example − If the number n is 24, then the output should be − const output = [2, 3, 5, 7, 11, 13, 17, 19, 23]; Method 1: Basic Prime Check Approach This approach uses a helper function to check if each number is prime: const num = 24; const isPrime = num => { let count ...
Read More