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 440 of 840
How to sum all elements in a nested array? JavaScript
Let's say, we are supposed to write a function that takes in a nested array of Numbers and returns the sum of all the numbers. We are required to do this without using the Array.prototype.flat() method. Let's write the code for this function − Using Recursive Approach const arr = [ 5, 7, [ 4, [2], 8, [1, 3], 2 ], [ 9, [] ], 1, 8 ]; const findNestedSum = ...
Read MoreChild node count in JavaScript?
In JavaScript, you can get the count of child elements using the children.length property. This property returns the number of direct child elements, excluding text nodes and comments. Syntax element.children.length Example: Counting Child Elements Child Node Count Example List Of Subject Names are as follows: Javascript MySQL ...
Read MoreRenaming imports and exports in JavaScript
JavaScript ES6 modules allow you to rename imports and exports using the as keyword. This is useful for avoiding naming conflicts, improving code readability, or providing more descriptive names for functions and variables. Note − You need to run a localhost server to run this example due to ES6 module requirements. Syntax For renaming exports: export { originalName as newName }; For renaming imports: import { originalName as newName } from './module.js'; Example Let's create a complete example with three files demonstrating both import and export renaming: ...
Read MoreCheck if object contains all keys in JavaScript array
We are required to write a function containsAll() that takes in two arguments, first an object and second an array of strings. It returns a boolean based on the fact whether or not the object contains all the properties that are mentioned as strings in the array. So, let's write the code for this. We will iterate over the array, checking for the existence of each element in the object, if we found a string that's not a key of object, we exit and return false, otherwise we return true. Example const obj = { ...
Read MoreReturn the largest array between arrays JavaScript
We have an array of arrays that contains some numbers, we have to write a function that takes in that array and returns the index of the subarray that has the maximum sum. If more than one subarray has the same maximum sum, we have to return the index of first such subarray. Problem Overview Given multiple arrays nested within a main array, we need to: Calculate the sum of each subarray Find which subarray has the largest sum Return the index of that subarray ...
Read MoreSet scroll view with intervals in JavaScript
Setting a scroll view with intervals in JavaScript allows you to automatically scroll content and add new elements at regular time intervals. This technique uses scrollTop and scrollHeight properties along with setInterval(). Key Properties The main properties for controlling scroll behavior are: scrollTop - Gets or sets the number of pixels scrolled from the top scrollHeight - Returns the total height of scrollable content Example Auto Scroll with Intervals ...
Read MoreBeginning and end pairs in array - JavaScript
We are required to write a JavaScript function that takes in an array of Number / String literals and returns another array of arrays. With each subarray containing exactly two elements, the nth element from start and nth from last. For example, if we have an array: const arr = [1, 2, 3, 4, 5, 6]; Then the output should be: const output = [[1, 6], [2, 5], [3, 4]]; How It Works The algorithm pairs elements from both ends of the array moving inward: ...
Read MoreFinding the nth day from today - JavaScript (JS Date)
We are required to write a JavaScript function that takes in a number n as the only input. The function should first find the current day (using Date Object in JavaScript) and then the function should return the day n days from today. For example − If today is Monday and n = 2, Then the output should be − Wednesday Understanding the Problem JavaScript's Date.getDay() returns 0 for Sunday, 1 for Monday, and so on. We need to handle the modulo operation correctly to cycle through weekdays. Method 1: ...
Read MoreJavaScript Auto-filling one field same as other
Auto-filling form fields is a common requirement in web forms, especially when users need to copy address information between billing and shipping sections. This can be achieved using JavaScript event listeners and form field manipulation. Example body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; padding: 20px; } ...
Read Moreloose equality in JavaScript
The loose equality operator == compares two values after performing type coercion, converting them to a common type before comparison. This differs from strict equality (===) which compares both value and type. Syntax operand1 == operand2 How Loose Equality Works When using ==, JavaScript follows specific conversion rules: If types are the same, compare values directly If one is a number and the other a string, convert string to number If one is boolean, convert to number first null and undefined are equal to each other Example ...
Read More