AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 327 of 840

Finding two missing numbers that appears only once and twice respectively in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 250 Views

We need to write a JavaScript function that finds two numbers in an array where all other numbers appear three times, except one number that appears twice and another that appears only once. Problem Statement Given an array where most numbers appear three times, find the two numbers that appear exactly twice and exactly once respectively. Example Let's solve this step by step: const arr = [1, 1, 1, 2, 2, 3]; const findMissing = (arr = []) => { let x = 0; // number appearing once ...

Read More

Making two sequences increasing in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 271 Views

In JavaScript, making two sequences strictly increasing by swapping elements at the same indices is a dynamic programming problem. A sequence is strictly increasing if each element is greater than the previous one. Problem Statement Given two arrays arr1 and arr2, we can swap elements at any index i between the arrays. The goal is to find the minimum number of swaps needed to make both arrays strictly increasing. Understanding the Example Consider the input arrays: const arr1 = [1, 3, 5, 4]; const arr2 = [1, 2, 3, 7]; console.log("Original arrays:"); console.log("arr1:", ...

Read More

Finding two prime numbers with a specific number gap in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 280 Views

Finding prime numbers with a specific gap is a common programming challenge. This involves identifying two prime numbers where their difference equals a given value within a specified range. Problem We need to write a JavaScript function that takes a gap number and a range array as arguments. The function should return the first pair of prime numbers that have an absolute difference equal to the gap and fall within the specified range. Algorithm Approach The solution involves two main steps: Find all prime numbers within the given range Search for consecutive primes with the specified gap ...

Read More

Removing n characters from a string in alphabetical order in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 347 Views

We need to write a JavaScript function that removes a specified number of characters from a string in alphabetical order. This means removing all 'a' characters first, then 'b', then 'c', and so on until we've removed the desired count. Problem Statement Given a lowercase alphabet string and a number, remove characters from the string in alphabetical order (starting with 'a', then 'b', 'c', etc.) until the specified count is reached. Example Let's implement a function that removes characters alphabetically: const str = 'abascus'; const num = 4; const removeAlphabetically = (str = ...

Read More

Creating permutations by changing case in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 312 Views

We are required to write a JavaScript function that takes in a string of characters as input and generates all possible permutations by changing the case of alphabetic characters. Our function can transform every letter individually to be lowercase or uppercase to create different strings. Non-alphabetic characters remain unchanged. We should return a list of all possible strings we could create. Problem Statement Given a string containing letters and numbers, generate all possible case permutations where each letter can be either uppercase or lowercase. Input: const str = 'k1l2'; Expected Output: ["k1l2", ...

Read More

How to capture HTML Canvas as gif/jpg/png/pdf with JavaScript?

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

We can use the canvas.toDataURL() method to capture HTML canvas content as different image formats like PNG, JPEG, WebP, and GIF. This method converts the canvas drawing into a data URL that can be saved or displayed as an image. Syntax canvas.toDataURL(type, encoderOptions) Parameters type (optional): The image format. Defaults to "image/png" encoderOptions (optional): Quality setting for lossy formats (0-1) Capture HTML Canvas as PNG PNG format provides lossless compression and supports transparency. Set the type to image/png: Download as ...

Read More

How to Add Commas Between a List of Items Dynamically in JavaScript?

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

In web development, you often need to display lists of items with commas separating them, like "Item 1, Item 2, Item 3". This can be achieved using CSS or JavaScript to dynamically add commas between list items. HTML Structure Consider the following HTML list structure: Item 1 Item 2 Item 3 Item 4 We'll explore two approaches to add commas between these items dynamically. Using CSS (Recommended) The CSS approach uses the ::before pseudo-element to insert commas before ...

Read More

How to call the key of an object but return it as a method, not a string in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 864 Views

In JavaScript, you can call object methods dynamically using bracket notation instead of dot notation. This allows you to access and execute object methods using string keys, which is useful when the method name is determined at runtime. Basic Dynamic Method Access You can use bracket notation obj[key]() to call methods dynamically: const obj = { greet: function() { console.log("Hello!"); }, farewell: function() { console.log("Goodbye!"); ...

Read More

Finding the continuity of two arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 206 Views

We are required to write a JavaScript function that takes in two arrays of numbers. The function should return true if the two arrays upon combining can form a consecutive sequence, false otherwise. For example: If the arrays are − const arr1 = [4, 6, 2, 9, 3]; const arr2 = [1, 5, 8, 7]; When combined and sorted, these arrays form [1, 2, 3, 4, 5, 6, 7, 8, 9], which is a consecutive sequence. Therefore, the output should be true. Understanding Consecutive Sequences A consecutive sequence means each number is exactly ...

Read More

Finding the nth element of the lucas number sequence in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 327 Views

Lucas numbers are a sequence similar to Fibonacci numbers but with different starting values. They follow a specific mathematical pattern where each number is the sum of the two preceding ones. Lucas Number Sequence Definition The Lucas sequence is defined as: L(0) = 2 L(1) = 1 L(n) = L(n-1) + L(n-2) for n ≥ 2 The sequence starts: 2, 1, 3, 4, 7, 11, 18, 29, 47, 76, 123... Problem Statement We need to write a JavaScript function that takes a number n and returns the nth Lucas number from the ...

Read More
Showing 3261–3270 of 8,392 articles
« Prev 1 325 326 327 328 329 840 Next »
Advertisements