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 415 of 840
Lookbehind Assertions JavaScript Regular Expressions
Lookbehind assertions in JavaScript regular expressions allow you to match a pattern only when it's preceded by a specific pattern. They use the syntax (? Syntax // Positive lookbehind - match if preceded by pattern (?Example: Extracting Prices with Positive Lookbehind This example matches numbers that are preceded by a dollar sign: Lookbehind Assertions The prices are $50, $99, $121 and $150. But 25 and 75 are not prices. Extract Prices ...
Read MoreSum of nested object values in Array using JavaScript
When working with complex nested data structures in JavaScript, you often need to sum values from deeply nested objects within arrays. This guide demonstrates various approaches to calculate the sum of nested object values efficiently. Understanding the Data Structure Consider a JSON object with nested arrays and objects where we need to sum the costNum values: let json = { storeData: [ { items: [ ...
Read MoreAggregate records in JavaScript
Aggregating records in JavaScript involves combining multiple objects with the same identifier into a single object with accumulated values. This is commonly used for data analysis tasks like summing transactions by person or grouping sales by product. Let's say we have an array of objects that contains information about some random transactions carried out by some people: const transactions = [{ name: 'Rakesh', amount: 1500 }, { name: 'Rajesh', amount: 1200 }, { name: 'Ramesh', ...
Read MoreHow to count digits of given number? JavaScript
The requirements here are simple, we are required to write a JavaScript function that takes in a number and returns the number of digits in it. For example − The number of digits in 4567 is 4 The number of digits in 423467 is 6 The number of digits in 457 is 3 Let's explore different approaches to count digits in JavaScript. Method 1: Using Recursive Approach This method uses recursion to divide the number by 10 until it reaches zero: const num = 2353454; const digits = (num, count = ...
Read MoreSplit First name and Last name using JavaScript?
In JavaScript, you can split a full name into first and last name using the split() method. This method divides a string based on a specified separator and returns an array of substrings. Syntax string.split(separator) Basic Example var studentFullName = "John Smith"; var details = studentFullName.split(' '); console.log("Student First Name = " + details[0]); console.log("Student Last Name = " + details[1]); Student First Name = John Student Last Name = Smith Handling Multiple Names For names with more than two parts, you can extract the first ...
Read MoreChecking Oddish and Evenish numbers - JavaScript
A number is Oddish if the sum of all of its digits is odd, and a number is Evenish if the sum of all of its digits is even. We need to write a function that determines whether a number is Oddish or Evenish. The function should return true for Oddish values and false for Evenish values. How It Works The algorithm extracts each digit from the number, adds them up, and checks if the sum is odd or even: Extract digits using modulo (num % 10) and integer division (Math.floor(num / 10)) Sum all ...
Read MoreNamed capture groups JavaScript Regular Expressions
Named capture groups in JavaScript regular expressions allow you to assign names to captured groups, making your code more readable and maintainable. Instead of accessing groups by index, you can reference them by meaningful names. Syntax // Named capture group syntax /(?pattern)/ // Accessing named groups match.groups.name Basic Example Named Capture Groups const text = "The year I graduated was 2012."; ...
Read MoreAccess previously iterated element within array.map in JavaScript?
In JavaScript, you can access previously iterated elements within array.map() using the index parameter. The map() method provides both the current element and its index, allowing you to reference earlier elements in the array. Let's say the following is our array: var details = [ {subjectId:110, subjectName: 'Java' }, {subjectId:111, subjectName: 'Javascript' }, {subjectId:112, subjectName: 'MySQL' }, {subjectId:113, subjectName: 'MongoDB' } ]; Using Index to Access Previous Elements The key is to use the index parameter in the map() callback to access ...
Read MoreReplace commas with JavaScript Regex?
In JavaScript, you can replace commas in strings using the replace() method combined with regular expressions. This is particularly useful when you need to replace specific comma patterns, such as the last comma in a string. Problem Statement Consider these example strings with commas: "My Favorite subject is, " "My Favorite subject is, and teacher name is Adam Smith" "My Favorite subject is, and got the marks 89" We want to replace the last comma in each string with " JavaScript". Using Regular Expression to Replace Last Comma The regular expression /, ...
Read MoreNeutralisation of strings - JavaScript
In JavaScript, string neutralisation involves evaluating a string containing only '+' and '-' characters to determine the overall sign. The concept is based on mathematical sign multiplication: like signs produce '+', unlike signs produce '-'. How Neutralisation Works The neutralisation follows these rules: ++ results in + (positive × positive = positive) -- results in + (negative × negative = positive) +- or -+ results in - (positive × negative = negative) Example String Let's work with the following string: const str = '+++-+-++---+-+--+-'; Implementation Here's how to implement ...
Read More