Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by AmitDiwan
Page 418 of 840
Return index of first repeating character in a string - JavaScript
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 MoreJavaScript unescape() with example
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 MoreBinding an object's method to a click handler in JavaScript
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 MoreGetting elements of an array depending on corresponding values of another JavaScript
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 MoreaddEventListener() not working more than once with a button in JavaScript?
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 MoreHow to divide an unknown integer into a given number of even parts using JavaScript?
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 MoreCasting a string to snake case - JavaScript
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 MoreCan we share a method between JavaScript objects in an array?
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 MoreJavaScript How to get all 'name' values in a JSON array?
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 MoreChecking a radio in radio group with JavaScript?
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