AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 338 of 840

Sum of all multiples in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 346 Views

We need to write a JavaScript function that takes a number n as the first argument, followed by any number of divisor arguments. The function should sum all numbers up to n that are divisible by any of the specified divisors. Problem Statement Given a number n and multiple divisors, find the sum of all numbers from 1 to n that are multiples of any of the divisors. For example, if we call: sumMultiples(15, 2, 3) We need to find numbers up to 15 that are divisible by either 2 or 3: ...

Read More

Finding minimum steps to make array elements equal in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 361 Views

We are required to write a JavaScript function that takes in a number, num as the only argument. The function should first construct an array of n elements based on the following rule: arr[i] = (2 * i) + 1; Therefore, if the input number is 5, then the array should be: const arr = [1, 3, 5, 7, 9]; Our function is supposed to calculate and return the minimum number of steps it should take so that all the elements of the array become equal. Understanding the Problem Let ...

Read More

Creating all possible unique permutations of a string in JavaScript

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

We are required to write a JavaScript function that takes in a string str. Our function should create all permutations of the input string and remove duplicates, if present. This means, we have to shuffle all letters from the input in all possible orders. Problem When generating permutations of strings with repeated characters, we need to avoid duplicate permutations. For example, the string "aabb" should not generate identical permutations like "aabb" appearing multiple times. Algorithm Approach The solution uses recursion with duplicate checking: For each character position, try placing each unique character Skip duplicate ...

Read More

Can split array into consecutive subsequences in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 319 Views

We are required to write a JavaScript function that takes in an array of sorted integers and determines if we can split it into consecutive subsequences of at least 3 elements each. Problem Statement Our function should return true if we can split the array into 1 or more subsequences where each subsequence consists of consecutive integers and has length at least 3, false otherwise. Input: const arr = [1, 2, 3, 3, 4, 5]; Expected Output: true Explanation: We can split the array into two consecutive subsequences: 1, 2, ...

Read More

How to add float numbers using JavaScript?

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

In JavaScript, floating-point numbers can be added directly using the addition operator (+). The parseFloat() function converts strings to floating-point numbers, but is not required when working with numeric literals. Example 1: Direct Addition The simplest way to add float numbers is using the addition operator directly: let inputFloat1 = 2.3; let inputFloat2 = 3.5; console.log("The two float values are:", inputFloat1, "and", inputFloat2); let result = inputFloat1 + inputFloat2; console.log("The sum of the float values is:", result); The two float values are: 2.3 and 3.5 The sum of the float values ...

Read More

Use jQuery to get values of selected checkboxes?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 514 Views

jQuery provides an efficient way to get values of selected checkboxes using the :checked selector combined with checkbox input elements. This is useful for forms where users can select multiple options. Syntax $('input[type="checkbox"]:checked') // or $('input[name="checkboxName"]:checked') Basic Example Here's a complete example that demonstrates getting values of selected checkboxes: jQuery Checkbox Values Select your hobbies: ...

Read More

How to turn a JSON object into a JavaScript array in JavaScript ?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 661 Views

Converting a JSON object with numeric string keys into a JavaScript array is a common operation. This is particularly useful when dealing with API responses or data structures that use indexed properties. The Problem Consider this JSON object where index keys are mapped to string values: const obj = { "0": "Rakesh", "1": "Dinesh", "2": "Mohit", "3": "Rajan", "4": "Ashish" }; console.log(typeof obj); // "object" object Method 1: Using Object.keys() and forEach() This approach iterates through object keys and maps ...

Read More

Finding even length numbers from an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 540 Views

We are required to write a JavaScript function that takes in an array of integers as the first and the only argument. The function should then construct and return a new array that contains only those elements from the original array that have an even number of digits. For example, if we have an array with numbers of different digit lengths, we need to filter out only those numbers whose digit count is even (2, 4, 6, etc.). Problem Example If the input array is: const arr = [12, 6, 123, 3457, 234, 2]; ...

Read More

Finding two closest elements to a specific number in an array using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 877 Views

Finding the two closest elements to a specific number in an array is a common algorithmic problem. This article demonstrates how to solve it efficiently using JavaScript's array methods and mathematical calculations. Problem We are required to write a JavaScript function that takes in an array of sorted integers, arr, as the first argument and a target number, as the second argument. Our function should return an array of exactly two numbers that exists in the array arr and are closest to target. The output array should also be sorted in increasing order. For example, if ...

Read More

How to add a property to a JavaScript object using a variable as the name?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 378 Views

In JavaScript, you can add properties to objects using either dot notation or bracket notation. However, when you need to use a variable as the property name, bracket notation is the only viable approach. The Problem with Dot Notation Dot notation treats everything after the dot as a literal property name, not a variable: let obj = {a: "value1"}; let propertyName = "dynamicProp"; // This creates a property literally named "propertyName" obj.propertyName = "value2"; console.log(obj); console.log("Property 'dynamicProp' exists:", obj.dynamicProp); // undefined { a: 'value1', propertyName: 'value2' } Property 'dynamicProp' exists: undefined ...

Read More
Showing 3371–3380 of 8,392 articles
« Prev 1 336 337 338 339 340 840 Next »
Advertisements