AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 406 of 840

How do I write a function that takes an array of values and returns an object JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 625 Views

Let's say, we are required to write a function classifyArray() that takes in an array which contains mixed data types and returns a Map() with the elements grouped by their data types. For example − // if the input array is: const arr = ['class', 2, [7, 8, 9], {"name": "Michael"}, Symbol('foo'), true, false, 'name', 6]; // then the output Map should be: Map(5) { 'string' => [ 'class', 'name' ], 'number' => [ 2, 6 ], 'object' => [ [ 7, 8, 9 ], { name: 'Michael' ...

Read More

Mersenne prime in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 356 Views

In Mathematics, a Mersenne prime is a number that can be written in the form M(n) = 2^n − 1 for some integer n and is actually a prime number. For example − The first four Mersenne primes are 3, 7, 31, and 127, which correspond to n = 2, 3, 5, and 7 respectively: 2² − 1 = 3 2³ − 1 = 7 2⁵ − 1 = 31 2⁷ − 1 = 127 We need to write a JavaScript function that ...

Read More

How do you make a button that adds text in HTML \'input\'?

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

You can make a button that adds text to an HTML input field using JavaScript's document.getElementById() method and event listeners. This approach allows you to dynamically modify input values when users interact with buttons. Basic HTML Structure First, create the HTML elements - a button and an input field: Click to add text Example: Adding Text on Button Click Add Text to Input Click to add "JavaScript" to ...

Read More

Check three consecutive numbers - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 551 Views

We are required to write a JavaScript function that takes in a Number, say n, and we are required to check whether there exist such three consecutive natural numbers (not decimal/floating point) whose sum equals to n. If there exist such numbers, our function should return them, otherwise it should return false. Following is the code − How It Works For three consecutive numbers to sum to n, we need: x + (x+1) + (x+2) = n, which simplifies to 3x + 3 = n. Therefore, n must be divisible by 3 and greater than 5 (since ...

Read More

How to trigger a button click on keyboard "enter" with JavaScript?

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

To trigger a button click when the user presses the "Enter" key, you can use JavaScript's event listeners to detect the keypress and programmatically trigger the button's click event. Basic Example Here's a complete example that triggers a button click when Enter is pressed inside an input field: Trigger Button Click on Enter Trigger Button Click on Enter Example Click ...

Read More

How to pass arguments to anonymous functions in JavaScript?

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

Anonymous functions in JavaScript are functions without a name that can accept parameters just like regular functions. You can pass arguments to them through function calls, event handlers, or higher-order functions. What are Anonymous Functions? Anonymous functions are functions declared without a name. They're often assigned to variables or used as callback functions. Anonymous Functions Anonymous Function Examples Run Examples ...

Read More

Adding two arrays of objects with existing and repeated members of two JavaScript arrays replacing the repeated ones

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 195 Views

When working with arrays of objects in JavaScript, you often need to merge them while avoiding duplicates. This tutorial shows how to combine two arrays of objects and remove duplicates based on a specific property. The Problem Consider two arrays of objects where some objects have the same name property. We want to merge them into one array, keeping only unique entries based on the name field. const first = [{ name: 'Rahul', age: 23 }, { name: 'Ramesh', age: ...

Read More

Check for Power of two in JavaScript

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

We need to write a function, isPowerOfTwo(), that takes a positive number and returns a boolean based on whether the number is a power of 2. For example: isPowerOfTwo(3) // false (3 is not a power of 2) isPowerOfTwo(32) // true (32 = 2^5) isPowerOfTwo(2048) // true (2048 = 2^11) isPowerOfTwo(256) // true (256 = 2^8) isPowerOfTwo(22) // false (22 is not a power of 2) Method 1: Using Recursive Division This approach recursively divides the number by 2 until it reaches 1 (power of 2) ...

Read More

Segregate all 0s on right and 1s on left in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 294 Views

We have an array of Numbers that contains 0, 1 and some other numbers. We are required to write a JavaScript function that takes in this array and brings all 1s to the start and 0s to the end Let's write the code for this function − Example const arr = [3, 2, 1, 8, 9, 0, 1, 9, 0, 2, 1, 0, 2, 0, 1, 0, 1, 1, 4, 0, 3]; const segregate = arr => { const copy = arr.slice(); for(let i = 0; i ...

Read More

Demonstrate nested loops with return statements in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 685 Views

In JavaScript, you can use return statements inside nested loops to exit from functions early. When a return statement is executed inside nested loops, it immediately exits the entire function, not just the current loop. Example let demoForLoop = () => { for(var outer = 1; outer < 100; outer++){ for(var inner = 1; inner { for(let i = 1; i

Read More
Showing 4051–4060 of 8,392 articles
« Prev 1 404 405 406 407 408 840 Next »
Advertisements