AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 414 of 840

Finding special array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 586 Views

An array is a special array if: - All the elements at odd indices are odd - All the elements at even indices are even We need to write a JavaScript function that takes an array and checks if it's a special array or not. Understanding the Logic For an array to be special: Index 0 (even): element must be even Index 1 (odd): element must be odd Index 2 (even): element must be even Index 3 (odd): element must be odd The key insight is that arr[i] % 2 should ...

Read More

Return the maximum number in each array using map JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 796 Views

We have an array of arrays of Numbers like this one: const arr = [ [12, 56, 34, 88], [65, 66, 32, 98], [43, 87, 65, 43], [32, 98, 76, 83], [65, 89, 32, 54], ]; We are required to write a function that maps over this array of arrays and returns an array that contains the maximum (greatest) element from each subarray. Using Math.max() with Spread Operator The most straightforward approach is to use Math.max() with the spread operator to find the maximum ...

Read More

How to check whether multiple values exist within a JavaScript array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

We are required to write a JavaScript function that takes in two arrays of Numbers and checks whether all the elements of the first array exist in the second or not. Following are our arrays − const arr1 = [34, 78, 89]; const arr2 = [78, 67, 34, 99, 56, 89]; Let's write the code and check for multiple values − Using indexOf() Method const arr1 = [34, 78, 89]; const arr2 = [78, 67, 34, 99, 56, 89]; const contains = (first, second) => { const ...

Read More

Enter key press event in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 27K+ Views

The Enter key press event in JavaScript allows you to detect when users press the Enter key (keycode 13) and execute specific functions. This is commonly used for form submissions, search functionality, or triggering actions without clicking a button. Basic Syntax You can handle the Enter key using the onkeypress event handler: onkeypress="yourFunctionName(event)" The Enter key has a keycode of 13, which you can check using event.keyCode. Method 1: Using onkeypress Attribute Enter Key ...

Read More

Removing 0s from start and end - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 187 Views

We are required to write a JavaScript function that takes in a number as a string and returns a new number string with all the leading and trailing 0s removed. For example: If the input is − const strNum = '054954000' Then the output should be − const output = '54954' Using substring() Method This approach uses two pointers to find the first non-zero character from the start and end, then extracts the substring between them. const strNum = '054954000'; const removeZero = (str = '') => ...

Read More

Unicode Property Escapes JavaScript Regular Expressions

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 264 Views

Unicode property escapes in JavaScript regular expressions allow you to match characters based on their Unicode properties using the u flag. This feature enables precise matching of characters by their Unicode categories, scripts, or properties. Syntax /\p{PropertyName}/u /\P{PropertyName}/u // Negated form The \p{} matches characters with the specified property, while \P{} matches characters WITHOUT that property. Common Unicode Properties Property Description Example Characters Letter Any letter A, B, α, β, 中 Number Any number 1, 2, ①, ② Emoji_Presentation Emoji characters 😀, 🌟, 🎉 ...

Read More

Implement Private properties using closures in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 196 Views

In JavaScript, closures provide a powerful way to create private properties that cannot be accessed directly from outside a function. This technique encapsulates data and prevents external code from modifying internal variables. What are Private Properties? Private properties are variables that can only be accessed and modified through specific methods, not directly from outside the containing scope. Closures enable this by keeping variables alive in memory even after the outer function has finished executing. How Closures Create Privacy When an inner function references variables from its outer function, it forms a closure. The outer function's variables ...

Read More

Convert an array of binary numbers to corresponding integer in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 6K+ Views

Let's say, we have an array of Numbers that contains 0 and 1 − const arr = [0, 1, 0, 1]; We are required to write an Array function, toBinary() that returns the corresponding decimal integer for the array it is used with. For example − If the array is − const arr = [1, 0, 1, 1]; Then the output should be 11 because the decimal representation of binary 1011 is 11. Therefore, let's write the code for this function. Method 1: Using parseInt() with join() In ...

Read More

Add two consecutive elements from the original array and display the result in a new array with JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 383 Views

We are required to write a JavaScript function that takes in an array of Numbers and returns a new array with elements as the sum of two consecutive elements from the original array. For example, if the input array is: const arrayOne = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; Then the output should be: const newArrayOne = [1, 5, 9, 13, 17] Let's write the code for this function: Example const arrayOne = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; ...

Read More

Product of two number using HOC - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 320 Views

Higher Order Functions (HOC) in JavaScript are functions that either receive another function as an argument or return a function as their result. When combined with closures, HOCs become a powerful tool for creating reusable and modular code. In this example, we'll create a Higher Order Function that calculates the product of two numbers using the concept of currying, where a function returns another function. Example Here's how to implement a product calculator using HOC: const num1 = 24; const num2 = 5; const productHOC = num1 => { return ...

Read More
Showing 4131–4140 of 8,392 articles
« Prev 1 412 413 414 415 416 840 Next »
Advertisements