Object Oriented Programming Articles

Page 100 of 589

Find duplicate element in a progression of first n terms JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 208 Views

When you have an array of first n natural numbers with one duplicate element, finding the duplicate efficiently is a common programming challenge. We'll explore two mathematical approaches that solve this in linear time. Method 1: Using Array.prototype.reduce() This approach uses a mathematical trick with the reduce function to find the duplicate in a single pass: const arr = [2, 3, 1, 2]; const duplicate = a => a.reduce((acc, val, ind) => val + acc - (ind + 1)) + a.length - 1; console.log(duplicate(arr)); 2 How the Reduce Method Works ...

Read More

Compute the sum of elements of an array which can be null or undefined JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 850 Views

When working with arrays containing numbers, null, and undefined values, we need to compute sums while treating these falsy values as zero. This is a common scenario when processing data from APIs or user inputs. Let's say we have an array of arrays, each containing some numbers along with some undefined and null values. We need to create a new array that contains the sum of each corresponding sub-array elements, treating undefined and null as 0. Following is the sample array: const arr = [[ 12, 56, undefined, 5 ], [ ...

Read More

How to get the product of two integers without using * JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 385 Views

We are required to write a function that takes in two numbers and returns their product, but without using the (*) operator. Method 1: Using Division Operator We know that multiplication and division are inverse operations, so if we divide a number by another number's inverse, it's the same as multiplying the two numbers. The mathematical concept: a × b = a ÷ (1 ÷ b) const a = 20, b = 45; const product = (a, b) => a / (1 / b); console.log(product(a, b)); 900 Method 2: Using ...

Read More

Replace a letter with its alphabet position JavaScript

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

We are required to write a function that takes in a string, trims it off any whitespaces, converts it to lowercase and returns an array of numbers describing corresponding characters positions in the english alphabets, any whitespace or special character within the string should be ignored. Problem Example Input → 'Hello world!' Output → [8, 5, 12, 12, 15, 23, 15, 18, 12, 4] How It Works The algorithm processes each character by: Converting to lowercase to handle both cases uniformly Getting ASCII code using charCodeAt() ...

Read More

How to add a character to the beginning of every word in a string in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 942 Views

We need to write a function that takes two strings and returns a new string with the second argument prepended to every word of the first string. For example: Input → 'hello stranger, how are you', '@@' Output → '@@hello @@stranger, @@how @@are @@you' If the second argument is not provided, we'll use '#' as the default character. Solution Using split() and map() The most straightforward approach is to split the string into words, prepend the character to each word, and join them back: const str = 'hello stranger, how are ...

Read More

How to get only the first n% of an array in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 309 Views

We are required to write a function that takes in an array arr and a number n between 0 and 100 (both inclusive) and returns the n% part of the array. Like if the second argument is 0, we should expect an empty array, complete array if it's 100, half if 50, like that. And if the second argument is not provided it should default to 50. Therefore, the code for this will be − Syntax const byPercent = (arr, n = 50) => { const requiredLength = Math.floor((arr.length * n) / ...

Read More

How to disable future dates in JavaScript Datepicker?

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

Disabling future dates in a JavaScript datepicker prevents users from selecting dates beyond today. This is commonly needed for applications like booking systems, report generators, or any form where only past or current dates are valid. Understanding the maxDate Property The maxDate property sets the maximum selectable date in a datepicker. By setting it to the current date, all future dates become disabled and unclickable. Example: jQuery UI Datepicker ...

Read More

Find average of each array within an array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 279 Views

We are required to write a function getAverage() that accepts an array of arrays of numbers and we are required to return a new array of numbers that contains the average of corresponding subarrays. Let's write the code for this. We will map over the original array, reducing the subarray to their averages like this − Example const arr = [[1, 54, 65, 432, 7, 43, 43, 54], [2, 3], [4, 5, 6, 7]]; const secondArr = [[545, 65, 5, 7], [0, 0, 0, 0], []]; const getAverage = (arr) => { ...

Read More

How to disable default behavior of when you press enter with JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 401 Views

To disable the default behavior when the Enter key is pressed, you need to use the keydown event listener along with preventDefault(). This prevents the browser from executing its default action for the Enter key. How It Works The preventDefault() method cancels the default action that belongs to the event. When combined with checking for the Enter key (event.key === "Enter"), it stops the browser's default behavior like form submission or input validation. Example Disable Enter Key Default ...

Read More

Recursion in array to find odd numbers and push to new variable JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 496 Views

We are required to write a recursive function, say pushRecursively(), which takes in an array of numbers and returns an object containing odd and even properties where odd is an array of odd numbers from input array and even an array of even numbers from input array. This has to be done using recursion and no type of loop method should be used. Syntax const pushRecursively = (arr, len = 0, odd = [], even = []) => { // Base case: if we've processed all elements if (len ...

Read More
Showing 991–1000 of 5,881 articles
« Prev 1 98 99 100 101 102 589 Next »
Advertisements