AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 348 of 840

Iterate through Object keys and manipulate the key values in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 317 Views

When working with arrays of objects in JavaScript, you often need to iterate through object keys and transform their values. This article demonstrates how to extract specific elements from array values within objects. Problem Statement Suppose we have an array of objects where each property contains an array of values: const arr = [ { col1: ["a", "b"], col2: ["c", "d"] }, { ...

Read More

Convert the number or boolean type JSON object from string type to its original in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

Suppose we have a JSON object where numeric and boolean values are stored as strings: const obj = {"name":"sam", "age":"24", "isMarried":"false"}; console.log(obj); { name: 'sam', age: '24', isMarried: 'false' } Here, the age property should be a number and isMarried should be a boolean. Our goal is to write a function that converts these string values back to their correct data types. Method 1: Using parseInt and Boolean Conversion This approach checks each property and converts numeric strings to numbers and boolean strings to booleans: const obj = { ...

Read More

Combination sum problem using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 614 Views

The combination sum problem involves finding all unique combinations from a given array where the numbers sum to a target value. Each number can be used multiple times, making this a classic backtracking problem. Problem Statement Given a set of candidate numbers (without duplicates) and a target number, find all unique combinations where the candidate numbers sum to the target. Constraints: All numbers (including target) are positive integers The same number may be chosen multiple times The solution set must not contain duplicate combinations Example: Input: candidates = ...

Read More

Check if a string is repeating in itself in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 890 Views

In JavaScript, we often need to determine if a string consists of a repeating pattern. This involves checking if the entire string can be formed by repeating a smaller substring multiple times. For example, the string 'carcarcarcar' is made by repeating 'car' four times, so it should return true. However, 'abcdef' has no repeating pattern, so it should return false. Problem Understanding We need to write a function that: Takes a string as input Returns true if the string is formed by repeating a pattern Returns false if no repeating pattern exists Example Input ...

Read More

Preparing numbers from jumbled number names in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 207 Views

Problem Suppose we have the following jumbled number name string: const str = 'TOWNE'; If we rearrange this string, we can find two number names in it: 2 (TWO) and 1 (ONE). Therefore, we expect an output of 21. We need to write a JavaScript function that takes in one such string and returns the numbers present in the string arranged in ascending order. Approach The solution involves: Creating a mapping of number words to their numeric values Generating permutations to check if number words can ...

Read More

Finding and returning uncommon characters between two strings in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

Problem We are required to write a JavaScript function that takes in two strings. Our function should return a new string of characters which is not common to both the strings. Example Following is the code − const str1 = "xyab"; const str2 = "xzca"; const findUncommon = (str1 = '', str2 = '') => { const res = []; for (let i = 0; i < str1.length; i++){ if (!(str2.includes(str1[i]))){ ...

Read More

Maximum Product of Two Numbers in a List of Integers in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 335 Views

We are required to write a JavaScript function that takes in an array of integers as the first and only argument. The function should find the maximum product that can be achieved by multiplying any two elements of the array. The condition is that we have to do this in linear time O(n) and constant space O(1). For example, if the input array is: const arr = [3, 9, 2, 1, 0]; Then the output should be: const output = 27; because it's the greatest product and can be achieved ...

Read More

Returning the value of nth power of iota(i) using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 180 Views

In mathematics, the imaginary unit i is defined as the square root of -1. When calculating powers of i, the results follow a cyclic pattern that repeats every 4 powers. Mathematical Background The imaginary unit i has the following properties: i = √(-1) i² = -1 i³ = -i i⁴ = 1 Since i⁴ = 1, the pattern repeats every 4 powers. This means we can use the modulo operator to determine the result for any power of i. Power Pattern Power (n % 4) Result (iⁿ) 0 ...

Read More

Converting numbers to base-7 representation in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 803 Views

In JavaScript, converting a number to base-7 representation involves repeatedly dividing by 7, similar to binary conversion but using 7 instead of 2. Base-7 uses digits 0-6. Understanding Base-7 Conversion To convert a decimal number to base-7, we divide the number by 7 repeatedly and collect the remainders. The remainders, read in reverse order, form the base-7 representation. Example Here's how to implement base-7 conversion in JavaScript: const base7 = (num = 0) => { let sign = num < 0 ? '-' : ''; num ...

Read More

Search from an array of objects via array of string to get array of objects in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 410 Views

Filtering an array of objects based on keys from another array is a common requirement in JavaScript. This tutorial shows how to extract matching objects using various approaches. Problem Statement Given an array of strings and an array of objects, we need to filter objects whose KEY property exists in the string array: const searchKeys = ['1956888670', '2109171907', '298845084']; const users = [ { KEY: '1262875245', VALUE: 'Vijay Kumar Verma' }, { KEY: '1956888670', VALUE: 'Sivakesava Nallam' }, { KEY: '2109171907', VALUE: 'udm analyst' ...

Read More
Showing 3471–3480 of 8,392 articles
« Prev 1 346 347 348 349 350 840 Next »
Advertisements