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 437 of 840
Dot notation vs Bracket notation in JavaScript
The dot notation and bracket notation are both methods for accessing object properties in JavaScript. While dot notation is cleaner and more commonly used, bracket notation provides greater flexibility, especially when working with dynamic property names or property names that contain special characters. Syntax Here's the basic syntax for both notations: // Dot notation object.propertyName // Bracket notation object["propertyName"] object[variableName] Basic Example: Accessing Properties Dot vs Bracket Notation ...
Read MoreFind duplicate element in a progression of first n terms JavaScript
When you have an array of first n natural numbers with one duplicate element, finding the duplicate efficiently is a common programming challenge. We'll explore two mathematical approaches that solve this in linear time. Method 1: Using Array.prototype.reduce() This approach uses a mathematical trick with the reduce function to find the duplicate in a single pass: const arr = [2, 3, 1, 2]; const duplicate = a => a.reduce((acc, val, ind) => val + acc - (ind + 1)) + a.length - 1; console.log(duplicate(arr)); 2 How the Reduce Method Works ...
Read MoreLoop through array and edit string JavaScript
Let's say, we have to write a function, say translate() that accepts a string as the first argument and any number of words after that. The string will actually contain n $ signs like this ā This $0 is more $1 just a $2. Then there will be 3 strings which will replace the corresponding places. For example ā If the function call is like this: translate('This $0 is more $1 just a $2.', 'game', 'than', 'game'); The output of the function should be: This game is more ...
Read MoreConcatenate two arrays of objects and remove repeated data from an attribute in JavaScript?
When concatenating two arrays of objects, you often need to remove duplicates based on a specific attribute. This article demonstrates how to merge arrays while handling duplicate entries using map() and find() methods. The Challenge Consider two arrays of product objects where some products appear in both arrays with different properties. We want to concatenate them but prioritize data from the second array when duplicates exist. Example: Merging Product Arrays Let's merge two product arrays and remove duplicates based on productId: var details1 = [ { ...
Read MoreBlock Scoping in JavaScript.
Block scope is an area between two curly braces { } which can be between loops, if conditions, or switch statements. The let and const keywords introduced in ES2015 allow us to create block-scoped variables that can only be accessed within that specific block. Understanding Block Scope Variables declared with let and const have block scope, meaning they exist only within the nearest enclosing block. This is different from var, which has function scope. Example: Block Scope with let Block Scoping Example ...
Read MoreSetting object members in JavaScript
In JavaScript, you can set object members (properties and methods) in several ways. This allows you to dynamically add or modify object properties after creation. Syntax // Dot notation object.propertyName = value; // Bracket notation object["propertyName"] = value; // Setting methods object.methodName = function() { // method body }; Example: Setting Object Properties Setting Object Members ...
Read MoreSorting objects according to days name JavaScript
Let's say, we have an array of objects that contains data about the humidity over the seven days of a week. The data, however, sits randomly in the array right now. We are supposed to sort the array of objects according to the days like data for Monday comes first, then Tuesday, Wednesday and lastly Sunday. Sample Data Following is our array: const weather = [{ day: 'Wednesday', humidity: 60 }, { day: 'Saturday', humidity: 50 }, { ...
Read MoreIs there any more efficient way to code this "2 Sum" Questions JavaScript
Our job is to write a function that solves the two-sum problem in at most linear time. Two Sum Problem Given an array of integers, we have to find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers that add up to the target, and if no two elements add up to the target, our function should return an empty array. Brute Force Approach - O(n²) The naive approach uses nested loops to check every pair of elements: const bruteForceTwoSum ...
Read MoreGet the first element of array in JavaScript
In JavaScript, there are several ways to get the first element of an array. The most common and efficient approaches include using bracket notation, the at() method, or destructuring assignment. Using Bracket Notation (Most Common) The simplest way is to access the element at index 0 using bracket notation: let fruits = ["apple", "banana", "orange"]; let firstFruit = fruits[0]; console.log(firstFruit); // Handle empty arrays let emptyArray = []; let firstElement = emptyArray[0]; console.log(firstElement); apple undefined Using the at() Method The at() method provides a modern alternative for accessing array ...
Read MoreExplain JavaScript Error Object.
The Error object is thrown by the JavaScript interpreter when a script error occurs. It can also be used to create custom exceptions. Understanding Error objects helps in debugging and error handling in JavaScript applications. Error Object Properties Property Description name Sets or returns the name/type of the error message Sets or returns the error message as a string Example: Catching and Displaying Error Properties JavaScript Error Object body { ...
Read More