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 457 of 840
How do I run two or more functions when using 'onclick' JavaScript?
When handling click events in JavaScript, you often need to execute multiple functions. There are several ways to run two or more functions when using the onclick event. Method 1: Using a Wrapper Function Create a single function that calls multiple functions inside it: Multiple Functions onClick Call Functions function callTwoFunctions() { ...
Read MoreHow can I check JavaScript arrays for empty strings?
In JavaScript, arrays can contain empty strings alongside other values. Here are several methods to check for and handle empty strings in arrays. Method 1: Using a Loop to Find Empty Strings The most straightforward approach is to iterate through the array and check each element: var studentDetails = new Array(); studentDetails[0] = "John"; studentDetails[1] = ""; studentDetails[2] = "Smith"; studentDetails[3] = ""; studentDetails[4] = "UK"; function arrayHasEmptyStrings(studentDetails) { for (var index = 0; index < studentDetails.length; index++) { if (studentDetails[index] == "") ...
Read MoreCreate empty array of a given size in JavaScript
In JavaScript, you can create an empty array of a given size using several approaches. The most common method is using the Array() constructor. Using Array Constructor The new Array(size) creates an array with the specified length, but all elements are undefined (empty slots). var numberArray = new Array(5); console.log("Array length:", numberArray.length); console.log("Array contents:", numberArray); console.log("First element:", numberArray[0]); Array length: 5 Array contents: [ ] First element: undefined Filling the Array with Values After creating an empty array, you can assign values to specific positions or replace the entire ...
Read MoreFind the Symmetric difference between two arrays - JavaScript
In Mathematics, the symmetric difference of two sets, say A and B is represented by A △ B. It is defined as the set of all elements which belong either to A or to B but not to both. For example: const A = [1, 2, 3, 4, 5, 6, 7, 8]; const B = [1, 3, 5, 6, 7, 8, 9]; The symmetric difference of A and B will be: const diff = [2, 4, 9] Using For Loops with indexOf() This approach iterates through both arrays and checks ...
Read MoreProper use of flex properties when nesting flex containers
A flex container is always the parent and a flex item is always the child. The scope of a flex formatting context is limited to a parent/child relationship. Descendants of a flex container beyond the children are not part of flex layout and will not accept flex properties. There are certain flex properties that apply only to flex containers: justify-content flex-wrap flex-direction align-items align-content There are certain flex properties that apply only to flex items: ...
Read MoreFunction returning another function in JavaScript
In JavaScript, functions can be assigned to variables, passed as arguments to other functions, and returned from other functions. This behavior allows us to create higher-order functions (HOFs), which are functions that return other functions. These higher-order functions enable powerful patterns like callbacks, function memoization, and data transformation. In this article, we will learn about functions returning other functions in JavaScript and explore practical examples of their usage. Basic Function Returning Function A function that returns another function creates a closure, where the inner function has access to the outer function's variables: function createGreeter(greeting) { ...
Read MoreDetect the ENTER key in a text input field with JavaScript
Detecting the ENTER key in a text input field is a common requirement in web development. You can accomplish this using JavaScript event listeners and checking for the specific key code or key value. HTML Setup First, let's create a simple text input field: Method 1: Using keyCode (Legacy) The traditional approach uses keyCode property with value 13 for the ENTER key: ENTER Key Detection ...
Read MoreFinding lunar sum of Numbers - JavaScript
The concept of lunar sum says that the sum of two numbers is calculated by taking the larger digit from each corresponding position, instead of adding them together. How Lunar Sum Works For each digit position, we compare the digits from both numbers and take the maximum value. Let's see how this works with an example: a = 879 and b = 768 Position-wise comparison: Position 1: max(8, 7) = 8 Position 2: max(7, 6) = 7 Position 3: max(9, 8) = 9 Lunar Sum: 879 Implementation ...
Read MoreHow to reverse a portion of an array in JavaScript?
We are required to write a JavaScript function that takes in an array, a start index and an end index. The function should reverse the portion of the array between the start index and end index. For example, if the array is: const arr = [2, 6, 5, 8, 3, 5, 2, 6, 7]; And the start index and end index are 3, 7 respectively, then the array should be reversed to: const output = [2, 6, 5, 2, 5, 3, 8, 6, 7]; Using Array Splice Method This approach ...
Read MoreValidating a file size in JavaScript while uploading
File size validation is essential for web applications to prevent users from uploading files that are too large for your server or application limits. JavaScript provides built-in methods to check file size before upload begins. How File Size Validation Works When a user selects a file using an HTML file input, JavaScript can access the file's properties through the files property. The size property returns the file size in bytes, which we can then validate against our requirements. Basic File Size Validation ...
Read More