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 368 of 840
Combining two arrays in JavaScript
In JavaScript, combining two arrays means pairing corresponding elements from each array to create a new array of subarrays. This is commonly known as "zipping" arrays together. For example, if we have two arrays of the same length, we can combine their elements at matching indices to form pairs. If the two arrays are: const arr1 = ['a', 'b', 'c']; const arr2 = [1, 2, 3]; Then the expected output should be: [ ['a', 1], ['b', 2], ['c', 3] ] ...
Read MoreFinding the sum of two numbers without using '+', '-', '/', '*' in JavaScript
We are required to write a JavaScript function add() that takes in two numbers m and n. The function should, without using the four basic arithmetic operations add the two numbers taken as input and return the sum. The Challenge Without using +, -, *, or /, we need to implement addition using bitwise operations. This approach mimics how computers perform addition at the hardware level using binary logic. Example The code for this will be − const m = 67, n = 33; const add = (x, y) => { while(y !== 0){ let carry = x & y; x = x ^ y; y = carry
Read MoreMaximum length product of unique words in JavaScript
We need to find two strings from an array that share no common characters and have the maximum product of their lengths. This problem efficiently uses bitwise operations to represent character sets. Problem Statement Given an array of lowercase strings, find two strings with no common characters that have the maximum length product. Return 0 if no such pair exists. For example: const arr = ["karl", "n", "the", "car", "mint", "alpha"]; // Expected output: 20 (mint: 4 chars × alpha: 5 chars = 20) How It Works The solution uses bit manipulation ...
Read MoreRemove json element - JavaScript?
In JavaScript, you can remove elements from JSON objects or arrays using different methods. This article demonstrates how to remove properties from JSON objects and elements from JSON arrays. Sample JSON Data Let's work with the following JSON array containing customer information: var details = [ { customerName: "Chris", customerAge: 32 }, { customerName: "David", ...
Read MoreHow to find capitalized words and add a character before that in a given sentence using JavaScript?
When working with strings that contain capitalized words, you might need to insert a character (like a comma) before each capital letter. This is useful for parsing concatenated sentences or formatting text data. Problem Statement Given a string with capitalized words, we want to add a comma before each capital letter that appears after another character: const str = "Connecting to server Connection has been successful We found result"; The goal is to transform this into: "Connecting to server, Connection has been successful, We found result" Solution Using Regular Expression We can ...
Read MoreDeviations in two JavaScript arrays in JavaScript
We have two arrays of numbers and need to find elements that exist in one array but not in both. This is called finding the symmetric difference or deviation between arrays. 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 two arrays and returns elements that are not common to both arrays. Using indexOf() Method The basic approach uses indexOf() to check if elements exist in the other array: const arr1 ...
Read MoreES6 Default Parameters in nested objects – JavaScript
ES6 allows you to set default parameters for nested objects using destructuring assignment. This feature helps handle cases where nested properties might be undefined or missing. Syntax function myFunction({ outerKey: { innerProperty = defaultValue, anotherProperty = anotherDefault } = {} } = {}) { // Function body } Example Here's how to implement default parameters in nested objects: function ...
Read MoreConvert JSON to another JSON format with recursion JavaScript
Suppose, we have the following JSON object − const obj = { "context": { "device": { "localeCountryCode": "AX", "datetime": "3047-09-29T07:09:52.498Z" }, "currentLocation": { "country": "KM", ...
Read MoreBeautiful Arrangement of Numbers in JavaScript
A beautiful arrangement is a permutation of numbers from 1 to num where each position satisfies specific divisibility rules. This problem involves finding all valid arrangements using backtracking. Definition For an array with num integers from 1 to num, a beautiful arrangement requires that for each position i (1-indexed): The number at position i is divisible by i, OR i is divisible by the number at position i Problem Statement Write a JavaScript function that takes a number and returns the count of all possible ...
Read MoreLimiting duplicate character occurrence to once in JavaScript
In JavaScript, removing duplicate characters while maintaining lexicographical order requires a strategic approach. This problem asks us to keep only one occurrence of each character, choosing positions that result in the lexicographically smallest string. Problem Statement We need to write a JavaScript function that takes a string and returns a new string where each character appears only once. The key constraint is that the result must be lexicographically smallest among all possible combinations. For example, with input 'cbacdcbc', the output should be 'acdb'. Understanding the Algorithm The solution uses a greedy approach: ...
Read More