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 468 of 840
Compare array of objects - JavaScript
We have two arrays of objects like these: const blocks = [ { id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }, ] const containers = [ { block: { id: 1 } }, { block: { id: 2 } }, { block: { id: 3 } }, ] We are required to write a function that checks each object of blocks array with the block key of each ...
Read MoreThe generator.throw() method in JavaScript.
The generator.throw() method allows you to inject an error into a generator function at the current yield point. When called, it throws the specified error inside the generator and returns an object with done and value properties. Syntax generator.throw(exception) Parameters exception: The error object to be thrown inside the generator function. Basic Example function* simpleGenerator() { try { console.log("Before first yield"); yield 1; ...
Read MoreConvert 2D array to object using map or reduce in JavaScript
Converting a 2D array to an object is a common task in JavaScript. Let's say we have a two-dimensional array that contains data about people's ages. The data is given by the following 2D array: const data = [ ['Rahul', 23], ['Vikky', 27], ['Sanjay', 29], ['Jay', 19], ['Dinesh', 21], ['Sandeep', 45], ['Umesh', 32], ['Rohit', 28], ]; console.log(data); [ [ 'Rahul', 23 ], [ 'Vikky', 27 ], [ 'Sanjay', 29 ], ...
Read MoreHow to force JavaScript to do math instead of putting two strings together?
JavaScript often concatenates when you expect addition because the + operator works differently with strings and numbers. Here are several methods to force mathematical addition instead of string concatenation. The Problem When JavaScript encounters the + operator with strings, it performs concatenation instead of addition: console.log("3" + "5"); // "35" (concatenation) console.log(3 + 5); // 8 (addition) console.log("3" + 5); // "35" (string wins, concatenation) 35 8 35 Using the Unary Plus Operator ...
Read MoreFind even odd index digit difference - JavaScript
We are required to write a JavaScript function that takes in a number and returns the difference of the sums of the digits at even place and the digits at odd place. Understanding the Problem Given a number, we need to: Sum all digits at even positions (0, 2, 4, ...) Sum all digits at odd positions (1, 3, 5, ...) Return the absolute difference between these sums Example For the number 345336: Position 0: 6 (even) Position 1: 3 (odd) Position 2: 3 (even) Position 3: 5 (odd) Position 4: 4 (even) ...
Read MoreSplitting last n digits of each value in the array in JavaScript
We have an array of numbers and need to extract the last n digits from each value. If a number has fewer than n digits, it remains unchanged. const arr = [56768, 5465, 5467, 3, 878, 878, 34435, 78799]; console.log("Original array:", arr); Original array: [ 56768, 5465, 5467, 3, 878, 878, 34435, 78799 ] We need a function that takes an array and a number n, then returns the last n digits of each element. If an element has fewer than n digits, it stays the same. Example with n = 2 ...
Read MoreHow to reduce arrays in JavaScript?
The reduce() method transforms an array into a single value by applying a function to each element. It's commonly used for calculations like summing numbers, finding maximums, or combining data. Syntax array.reduce(callback(accumulator, currentValue, index, array), initialValue) Parameters: callback - Function executed on each element accumulator - Accumulated result from previous iterations currentValue - Current element being processed initialValue (optional) - Starting value for accumulator Example: Summing Array Elements ...
Read MoreConvert array of arrays to array of objects grouped together JavaScript
Let's say, we have a two-dimensional array that contains data about some colors and fruits like this: const data = [ ['orange', 'fruit'], ['red', 'color'], ['green', 'color'], ['orange', 'color'], ['banana', 'fruit'], ['blue', 'color'], ['lemon', 'fruit'], ['mango', 'fruit'], ['lemon', 'color'], ]; console.log("Original data:", data); Original data: [ [ 'orange', 'fruit' ], [ 'red', 'color' ], [ 'green', 'color' ], [ 'orange', 'color' ], [ 'banana', 'fruit' ], ...
Read MoreGet n numbers from array starting from given point JavaScript
We need to create a custom Array method that extracts n elements from an array starting at a given index, moving in a specified direction (left or right). When we reach array boundaries, the function wraps around to continue from the other end. Problem Requirements The function should take three parameters: n - number of elements to extract m - starting index (must be ≤ array.length - 1) direction - either 'left' or 'right' For example, if we have [0, 1, 2, 3, 4, 5, 6, 7] and call get(4, 6, 'right'), it should return ...
Read MoreDisplay whether the office is closed or open right now on the basis of current time with JavaScript ternary operator
Let's say we are matching the current date and time with the business hours. We need to display whether the office is closed or open right now on the basis of current time. We can get the current hour from the Date object and use the JavaScript ternary operator to determine if the office is open or closed based on business hours. Syntax const currentHour = new Date().getHours(); const status = (condition) ? 'Open' : 'Closed'; Example Office ...
Read More