Web Development Articles

Page 477 of 801

Code to find the center of an array without using ES6 functions - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 393 Views

We are required to write an array function midElement() that returns the middlemost element of the array without accessing its length property and without using any kind of built-in loops. If the array contains an odd number of elements, we return the one middlemost element, or if the array contains an even number of elements, we return an array of two middlemost elements. How It Works The solution uses recursion to count array elements by incrementing an index until reaching undefined, then calculates the middle position(s) based on whether the count is odd or even. Example ...

Read More

Smallest positive value that cannot be represented as sum of subarray JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 210 Views

We have a sorted array of positive integers like this: const arr = [1, 3, 6, 10, 11, 15]; We are required to write a function, say findSmallest() that takes in one such array and returns the smallest positive integer that cannot be represented as the sum of some subarray of this original array. For example, for the array written above, 2 is the smallest positive integer which cannot be reached by summing any subarray of this original array. Algorithm Approach Since the array is sorted, we can achieve the solution to this ...

Read More

Counting below / par elements from an array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 248 Views

We are required to write a function that counts how many elements in an array are below or at/above a given number. Following is our array of Numbers − const array = [54, 54, 65, 73, 43, 78, 54, 54, 76, 3, 23, 78]; For example, if the number is 60, there should be 7 elements below it − 54, 54, 43, 54, 54, 3, 23 and 5 elements at or above it − 65, 73, 78, 76, 78 Using Array.reduce() Method The reduce() method processes ...

Read More

Remove all characters of first string from second JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 436 Views

When working with strings in JavaScript, you may need to remove all characters from one string that appear in another string. This is useful for filtering, data cleaning, or text processing tasks. Let's say we have two strings with characters in no specific order. We need to write a function that takes these two strings and returns a modified version of the second string with all characters from the first string removed. Example Strings const first = "hello world"; const second = "hey there"; Using Array Methods (Recommended) The most straightforward approach uses ...

Read More

Merging boolean array with AND operator - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 829 Views

Let's say, we have an array of arrays of boolean like this − const arr = [[true, false, false], [false, false, false], [false, false, true]]; We are required to write a function that merges this array of arrays into a one-dimensional array by combining the corresponding elements of each subarray using the AND (&&) operator. Let's write the code for this function. We will be using Array.prototype.reduce() function to achieve this. Example Following is the code − const arr = [[true, false, false], [false, false, false], [false, false, true]]; const ...

Read More

JavaScript Determine the array having majority element and return TRUE if its in the same array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 209 Views

We are required to write a JavaScript function that takes in an array of numbers with repetitive values and returns the number that appears for more than (n/2) number of times where n is the length of the array. If there is no such element in the array, our function should return false. This problem is commonly known as finding the "majority element" in an array. A majority element is one that appears more than half the time in the array. Algorithm Approach We'll use the Boyer-Moore Voting Algorithm, which works in two phases: ...

Read More

Prime numbers upto n - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 697 Views

Let's say, we are required to write a JavaScript function that takes in a number, say n, and returns an array containing all the prime numbers up to n. For example − If the number n is 24, then the output should be − const output = [2, 3, 5, 7, 11, 13, 17, 19, 23]; Method 1: Basic Prime Check Approach This approach uses a helper function to check if each number is prime: const num = 24; const isPrime = num => { let count ...

Read More

Return TRUE if the first string starts with a specific second string JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 259 Views

We are required to write a JavaScript function that takes in two strings and checks whether the first string starts with the second string or not. For example: If the two strings are: "Disaster management report" "Disas" Then our function should return true There are multiple ways to check if a string starts with another string in JavaScript. Let's explore the most common approaches. Using startsWith() Method (Recommended) The startsWith() method is the built-in and most straightforward way to check if a string starts with another string: const first = 'The ...

Read More

Replacing all special characters with their ASCII value in a string - JavaScript

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

We are required to write a JavaScript function that takes in a string that might contain some special characters. The function should return a new string with all special characters replaced with their corresponding ASCII value. Understanding Special Characters Special characters are symbols that are not letters, numbers, or spaces. Examples include !, @, #, $, etc. Each character has a unique ASCII code that can be retrieved using the charCodeAt() method. Example Following is the code: const str = 'Th!s !s @ str!ng th@t cont@!ns some special characters!!'; const specialToASCII = str ...

Read More

Check for Subarray in the original array with 0 sum JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 505 Views

We are required to write a JavaScript function that takes in an array of Numbers with some positive and negative values. We are required to determine whether there exists a subarray in the original array whose net sum is 0 or not. Our function should return a boolean on this basis. Approach The approach here is simple. We iterate over the array using a for loop, calculate the cumulative sum up to that particular element. And if any point the cumulative becomes 0 or attains a value it has previously attained, then there exists a subarray with ...

Read More
Showing 4761–4770 of 8,010 articles
« Prev 1 475 476 477 478 479 801 Next »
Advertisements