The generator.throw() method in JavaScript.

AmitDiwan
Updated on 15-Mar-2026 23:18:59

223 Views

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 More

Convert 2D array to object using map or reduce in JavaScript

AmitDiwan
Updated on 15-Mar-2026 23:18:59

1K+ Views

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 More

How to force JavaScript to do math instead of putting two strings together?

AmitDiwan
Updated on 15-Mar-2026 23:18:59

503 Views

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 More

Find even odd index digit difference - JavaScript

AmitDiwan
Updated on 15-Mar-2026 23:18:59

244 Views

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 More

Splitting last n digits of each value in the array in JavaScript

AmitDiwan
Updated on 15-Mar-2026 23:18:59

173 Views

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 More

How to search the value of the type attribute of a link in JavaScript?

Jennifer Nicholas
Updated on 15-Mar-2026 23:18:59

268 Views

To search or retrieve the value of the type attribute of a link in JavaScript, you can use the type property of the anchor element. This attribute specifies the MIME type of the linked resource. Syntax element.type Example: Getting Type Attribute Value Qries var x = document.getElementById("qriesid").type; document.write("Value of the type attribute: " + x); ... Read More

How to define multiple style rules for a single element in CSS?

Samual Sam
Updated on 15-Mar-2026 23:18:59

1K+ Views

You may need to define multiple style rules for a single element. You can define these rules to combine multiple properties and corresponding values into a single block as defined in the following example: h1 { color: #36C; font-weight: normal; letter-spacing: .4em; ... Read More

How to reduce arrays in JavaScript?

AmitDiwan
Updated on 15-Mar-2026 23:18:59

207 Views

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 More

Convert array of arrays to array of objects grouped together JavaScript

AmitDiwan
Updated on 15-Mar-2026 23:18:59

1K+ Views

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 More

Get n numbers from array starting from given point JavaScript

AmitDiwan
Updated on 15-Mar-2026 23:18:59

332 Views

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 More

Advertisements