AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 418 of 840

Return index of first repeating character in a string - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 290 Views

We are required to write a JavaScript function that takes in a string and returns the index of first character that appears twice in the string. If there is no such character then we should return -1. Following is our string: const str = 'Hello world, how are you'; Example Following is the code: const str = 'Hello world, how are you'; const firstRepeating = str => { const map = new Map(); for(let i = 0; i < str.length; i++){ ...

Read More

JavaScript unescape() with example

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 303 Views

The JavaScript unescape() function is used to decode strings that were encoded with the escape() function. This function is deprecated since JavaScript 1.5 and should not be used in modern code. Use decodeURIComponent() instead. Syntax unescape(encodedString) Parameter: encodedString - The string to be decoded. Return Value: A new decoded string. Example with unescape() (Deprecated) JavaScript unescape() Example body { ...

Read More

Binding an object's method to a click handler in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 287 Views

Binding an object's method to a click handler is essential when you need to maintain the correct this context within the method. This ensures the method can access the object's properties and other methods properly. Understanding the Context Problem When directly assigning an object method to an event handler, the this context gets lost and refers to the event target instead of the original object. Example Binding Object Methods to Click Handlers ...

Read More

Getting elements of an array depending on corresponding values of another JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 645 Views

Suppose we have two arrays, for example consider the following two: const array1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']; const array2 = [ 1, 0, 0, 1 , 0, 0, 1, 0]; Both arrays are bound to have the same length. We need to write a function that, when provided with an element from the second array, returns a subarray from the first array containing all elements whose index corresponds to the index of the element we took as an argument in the second array. For example: findSubArray(0) should return: ...

Read More

addEventListener() not working more than once with a button in JavaScript?

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

The addEventListener() method works multiple times by design. If it seems to work only once, the issue is usually in your event handler function or how you're setting up the listener. Common Problem: Removing the Element A frequent mistake is removing or replacing the button element inside the event handler, which destroys the event listener: addEventListener Problem Bad Example - Works Once ...

Read More

How to divide an unknown integer into a given number of even parts using JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 461 Views

When dividing an integer into equal parts, you may need to distribute the remainder across some parts to ensure all values are used. This can be achieved using the modular operator and array manipulation. How It Works The algorithm divides the integer by the number of parts to get a base value. If there's a remainder, it distributes the extra values by adding 1 to some parts. Example function divideInteger(value, parts) { var baseValue; var remainder = value % parts; ...

Read More

Casting a string to snake case - JavaScript

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

Snake case is a naming convention where words are separated by underscores (_) and all letters are lowercase. For example, "Hello World" becomes "hello_world". This article demonstrates how to convert any string to snake case using JavaScript. Syntax function toSnakeCase(str) { return str.split(' ') .map(word => word.toLowerCase()) .join('_'); } Example Here's a complete example that converts a sentence to snake case: ...

Read More

Can we share a method between JavaScript objects in an array?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 196 Views

Yes, we can share methods between JavaScript objects in an array using prototypes or by defining shared methods outside the objects. This approach promotes code reusability and memory efficiency. Using Prototype Methods The most common way to share methods is through prototype inheritance. When you define a method on a constructor's prototype, all instances share that method. Shared Methods in JavaScript body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result { ...

Read More

JavaScript How to get all 'name' values in a JSON array?

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

In JavaScript, extracting specific values from a JSON array is a common task. When working with nested objects, you can use the map() method to iterate through the array and extract the desired properties. Sample JSON Data Let's consider the following JSON array with customer information: var details = [ { "customerDetails": [ { "customerName": "John ...

Read More

Checking a radio in radio group with JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 634 Views

In JavaScript, you can programmatically check a radio button in a radio group by setting its checked property to true. This is useful for form validation, user interactions, or setting default selections dynamically. HTML Radio Button Group Structure First, let's look at a typical radio button group structure: Gender: Male Female Method 1: Using getElementsByName() The most direct approach is to use getElementsByName() to target radio buttons by their name attribute: ...

Read More
Showing 4171–4180 of 8,392 articles
« Prev 1 416 417 418 419 420 840 Next »
Advertisements