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
Web Development Articles
Page 219 of 801
Reversing vowels in a string JavaScript
We are required to write a JavaScript function that takes a string as input and reverse only the vowels of a string. Problem Example If the input string is: const str = 'Hello'; Then the output should be: 'Holle' Notice how 'e' and 'o' swap positions while consonants 'H', 'l', 'l' remain in their original places. Solution Using Two Pointers The most efficient approach uses two pointers from opposite ends of the string: const str = 'Hello'; const reverseVowels = (str = '') => { ...
Read MoreFinding square root of a non-negative number without using Math.sqrt() JavaScript
We are required to write a JavaScript function that takes in a non-negative integer and computes and returns its square root without using Math.sqrt(). We can floor off a floating-point number to an integer. For example: For the number 15, we need not return the precise value, we can just return the nearest smaller integer value that will be 3, in case of 15. We will make use of the binary search algorithm to converge to the square root of the given number. Binary Search Approach The binary search method works by maintaining a range [left, ...
Read MoreFind the Length of the Longest possible palindrome string JavaScript
Given a string s which consists of lowercase or uppercase letters, we are required to return the length of the longest palindrome that can be built with those letters. Letters are case sensitive, for example, "Aa" is not considered a palindrome here. For example, if the input string is "abccccdd", the output should be 7 because one longest palindrome that can be built is "dccaccd", whose length is 7. Algorithm Explanation The key insight is that a palindrome can have pairs of characters (which appear on both sides) plus at most one character in the middle. We ...
Read MoreFinding perfect numbers in JavaScript
A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself. A divisor of an integer x is an integer that can divide x evenly. For example: 28 is a perfect number, because 28 = 1 + 2 + 4 + 7 + 14 We are required to write a JavaScript function that takes in a number, say n, and determines whether or not n is a perfect number. How Perfect Numbers Work To check if a number is perfect, we need to: ...
Read MoreSort in multi-dimensional arrays in JavaScript
Sorting multi-dimensional arrays in JavaScript requires handling each subarray while maintaining specific sorting rules. This article demonstrates how to sort elements within subarrays based on custom conditions. Problem Statement Suppose we have the following array of arrays: const arr = [ ["A", "F", "A", "H", "F", "F"], ["F", "A", "A", "F", "F", "H"] ]; We need to write a JavaScript function that sorts all subarrays according to these rules: If the elements are not either "A" or "F", they should maintain their position If the element is either ...
Read MoreIs uppercase used correctly JavaScript
In JavaScript, we can determine whether a string follows correct uppercase usage based on three specific rules. A string is considered to have correct uppercase usage if it meets any of these criteria: All letters in a word are capitals, like "INDIA". All letters in a word are not capitals, like "example". Only the first letter in a word is capital, like "Ramesh". We need to write a JavaScript function that takes a string and determines whether it complies with any of these three ...
Read MoreImplementing binary search in JavaScript to return the index if the searched number exist
We are required to write a JavaScript function that takes in a sorted array of numbers as the first argument and a search number as the second argument. If the search number exists in the array, we need to return its index in the array, otherwise we need to return -1. We have to do this making use of the binary search algorithm. The binary search algorithm is basically a divide and conquer algorithm which recursively divides the array into halves until it converges to a singleton element. The sorting of array is necessary for binary search ...
Read MoreIs the second string a rotated version of the first string JavaScript
We are required to write a JavaScript function that takes in two strings, say str1 and str2. We are required to determine whether or not the second string is a rotated version of the first string. For example − If the input strings are − const str1 = 'abcde'; const str2 = 'cdeab'; Then the output should be true because str2 is indeed made by shifting 'ab' to the end of string in str1. Understanding String Rotation A string rotation means taking some characters from the beginning of a string and moving them ...
Read MoreMerge JavaScript objects with the same key value and count them
Suppose we have an array of objects like this: const arr = [{ "value": 10, "id": "111", "name": "BlackCat", }, { "value": 10, "id": "111", "name": "BlackCat", }, { "value": 15, "id": "777", "name": "WhiteCat", }]; We are required to write a JavaScript function that takes in one such array. The function should then merge all those objects together that ...
Read MoreMerge JSON array date based JavaScript
When working with JSON arrays containing objects with date properties, you often need to merge objects that share the same date. This is common when combining data from different sources or consolidating time-series data. Suppose we have the following array of objects: const arr = [ { "date": "2010-01-01", "price": 30 }, { "date": "2010-02-01", ...
Read More