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 425 of 840
Avoid Unexpected string concatenation in JavaScript?
JavaScript string concatenation can lead to unexpected results when mixing strings and numbers. Using template literals with backticks provides a cleaner, more predictable approach than traditional concatenation methods. The Problem with Traditional Concatenation When using the + operator, JavaScript may perform string concatenation instead of numeric addition: let name = "John"; let age = 25; let score = 10; // Unexpected string concatenation console.log("Age: " + age + score); // "Age: 2510" (not 35!) console.log(name + " is " + age + " years old"); Age: 2510 John is 25 years ...
Read MoreConvert a list of string coords into two float lists of Lat/Longitude coordinates in JavaScript?
When working with coordinate data in JavaScript, you often need to parse string coordinates and separate them into latitude and longitude arrays. This is common when processing GPS data or API responses. Input Data Format Let's start with a list of coordinate strings in "latitude, longitude" format: var listOfStrings = ["10.45322, -6.8766363", "78.93664664, -9.74646646", "7888.7664664, -10.64664632"]; console.log("Input coordinates:"); console.log(listOfStrings); Input coordinates: [ '10.45322, -6.8766363', '78.93664664, -9.74646646', '7888.7664664, -10.64664632' ] Method 1: Using forEach with split() and map() This approach uses split() to separate coordinates and map(Number) to convert strings to ...
Read MoreFind the Sum of fractions - JavaScript
In JavaScript, we can calculate the sum of fractions by finding a common denominator and adding the numerators. This tutorial shows how to add fractions represented as arrays without converting to decimals. Problem Statement Given an array of arrays where each subarray contains two numbers representing a fraction, we need to find their sum in fraction form. const arr = [[12, 56], [3, 45], [23, 2], [2, 6], [2, 8]]; // Represents fractions: 12/56, 3/45, 23/2, 2/6, 2/8 Algorithm Overview To add fractions a/b + c/d, we use the formula: (a*d + c*b) ...
Read MoreHow to test and execute a regular expression in JavaScript?
JavaScript provides two main methods for testing and executing regular expressions: test() and exec(). The test() method returns a boolean indicating if a pattern matches, while exec() returns detailed match information or null. Regular Expression Methods There are two primary ways to work with regular expressions in JavaScript: test() - Returns true/false if pattern matches exec() - Returns match details or null Example: Using test() and exec() Regular Expression Testing ...
Read MoreCan we assign new property to an object using deconstruction in JavaScript?
You can assign new properties to an object using destructuring in JavaScript. This technique allows you to extract values from one object and assign them as properties to another object. Basic Syntax // Destructuring assignment to object properties ({ property1: targetObj.newProp1, property2: targetObj.newProp2 } = sourceObj); Example Object Destructuring Assignment body { ...
Read MoreHow to Sort object of objects by its key value JavaScript
Let's say, we have an object with keys as string literals and their values as objects as well like this − const companies = { 'landwaves ltd': {employees: 1200, worth: '1.2m', CEO: 'Rajiv Bansal'}, 'colin & co': {employees: 200, worth: '0.2m', CEO: 'Sukesh Maheshwari'}, 'motilal biscuits': {employees: 975, worth: '1m', CEO: 'Rahul Gupta'}, 'numbtree': {employees: 1500, worth: '1.5m', CEO: 'Jay Kumar'}, 'solace pvt ltd': {employees: 1800, worth: '1.65m', CEO: 'Arvind Sangal'}, 'ambicure': {employees: 170, worth: '0.1m', CEO: 'Preetam Chawla'}, ...
Read MoreHow to find out what character key is pressed in JavaScript?
To find out which character key is pressed in JavaScript, you can use various methods. The modern approach uses the key property, while legacy methods relied on keyCode. Modern Approach: Using event.key The key property directly returns the character that was pressed, making it the preferred method: Key Detection - Modern Method Type in the input field below: ...
Read MoreFrom a list of IDs with empty and non-empty values, retrieve specific ID records in JavaScript
When working with arrays of objects containing IDs, you often need to filter records based on specific ID values. JavaScript's filter() method provides an efficient way to retrieve all objects matching a particular ID, even when the array contains empty or null ID values. Sample Data Let's work with the following array that contains both valid and empty ID values: var details = [ {id: 101, name: "John", age: 21}, {id: 111, name: "David", age: 24}, {id: 1, name: "Mike", age: 22}, ...
Read MoreJavaScript - Find the smallest n digit number or greater
We are required to write a JavaScript function that takes in a number as the first argument, say n, and an array of numbers as the second argument. The function should return the smallest n digit number which is a multiple of all the elements specified in the array. If there exist no such n digit element then we should return the smallest such element. For example: If the array is: const arr = [12, 4, 5, 10, 9] For both n = 2 and n = 3, the output should be 180 Example Following ...
Read MoreOrder an array of words based on another array of words JavaScript
When working with arrays in JavaScript, you may need to reorder an array of objects based on the order specified in another array. This is useful for sorting data according to custom priorities or sequences. Let's say we have an array of objects sorted by their id property: const unordered = [{ id: 1, string: 'sometimes' }, { id: 2, string: 'be' }, { id: 3, string: 'can' }, { ...
Read More