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
Javascript Articles
Page 92 of 534
Count of pairs in an array that have consecutive numbers using JavaScript
Problem We need to write a JavaScript function that takes an array of integers and returns the count of consecutive pairs. A consecutive pair consists of two adjacent elements in the array where one number is exactly one more than the other. Understanding Consecutive Pairs Two numbers are consecutive if their absolute difference is 1. For example, (5, 6), (8, 7), and (-3, -4) are all consecutive pairs. Solution We'll iterate through the array in steps of 2, treating each pair of adjacent elements as a potential consecutive pair: const arr = [1, ...
Read MoreFinding the longest consecutive appearance of a character in another string using JavaScript
Problem We are required to write a JavaScript function that takes in a string as the first argument and a single character as the second argument. Our function should count and return the longest consecutive appearance of the character in the string. Approach We'll iterate through the string and track the current consecutive count and maximum count found so far. When we encounter the target character, we increment the counter. When we encounter a different character, we reset the counter. Example Following is the code − const str = 'abcdaaadse'; const char ...
Read MoreApplying a custom function to each corresponding element of two arrays using JavaScript
We need to write a JavaScript function that applies a custom callback function to corresponding elements of two arrays, creating a new array with the results. Problem We are required to write a JavaScript function that takes in a callback function (which takes in two arguments and returns a value) as the first argument and two arrays of essentially the same length as the second and third argument. Our function should construct and return a new array whose each corresponding element is the return value of the callback function if corresponding numbers of the input array are ...
Read MoreFinding alphabet from ASCII value without using library functions in JavaScript
Problem We are required to write a JavaScript function that takes in a number representing an ASCII value. Our function should return the corresponding alphabet character for that ASCII value (if it exists), or -1 otherwise. The condition here is that we cannot use any built-in function like String.fromCharCode() that directly converts ASCII values to characters. Understanding ASCII Values ASCII values for alphabetic characters follow these ranges: Uppercase letters: A-Z have ASCII values 65-90 Lowercase letters: a-z have ASCII values 97-122 Example Following is the code: const num = ...
Read MoreFinding the length of longest vowel substring in a string using JavaScript
Problem We are required to write a JavaScript function that takes in a string. Our function should return the length of the longest contiguous substring that contains only vowels. Approach The solution uses a sliding window approach to track consecutive vowels. We maintain two variables: cur for the current vowel sequence length and max for the longest sequence found so far. Example Following is the code − const str = 'schooeal'; const findLongestVowel = (str = '') => { let cur = 0; let max ...
Read MoreFinding and returning uncommon characters between two strings in JavaScript
Problem We are required to write a JavaScript function that takes in two strings. Our function should return a new string of characters which is not common to both the strings. Example Following is the code − const str1 = "xyab"; const str2 = "xzca"; const findUncommon = (str1 = '', str2 = '') => { const res = []; for (let i = 0; i < str1.length; i++){ if (!(str2.includes(str1[i]))){ ...
Read MoreReturning the value of nth power of iota(i) using JavaScript
In mathematics, the imaginary unit i is defined as the square root of -1. When calculating powers of i, the results follow a cyclic pattern that repeats every 4 powers. Mathematical Background The imaginary unit i has the following properties: i = √(-1) i² = -1 i³ = -i i⁴ = 1 Since i⁴ = 1, the pattern repeats every 4 powers. This means we can use the modulo operator to determine the result for any power of i. Power Pattern Power (n % 4) Result (iⁿ) 0 ...
Read MoreFrequency of elements of one array that appear in another array using JavaScript
We need to write a JavaScript function that takes two arrays of strings and returns the frequency count of each element from the second array as it appears in the first array. Problem Given two arrays, we want to count how many times each string from the second array appears in the first array. The result should be an array of counts corresponding to each element in the second array. Example Following is the code − const arr1 = ['abc', 'abc', 'xyz', 'cde', 'uvw']; const arr2 = ['abc', 'cde', 'uap']; const findFrequency = ...
Read MoreMaximum absolute difference of the length of strings from two arrays in JavaScript
Problem We are required to write a JavaScript function that takes in two arrays, a1 and a2 of strings. Each string is composed with letters from a to z. Let x be any string in the first array and y be any string in the second array. Our function should find the value of − max(abs(length(x) − length(y))) Approach To find the maximum absolute difference, we need to: Find the longest string from both arrays Find the shortest string from both arrays ...
Read MoreRepeating each character number of times their one based index in a string using JavaScript
We need to write a JavaScript function that takes a string of lowercase English letters and transforms it according to specific rules: each character should be repeated based on its 1-based index position, with the first letter capitalized, and character groups separated by dashes. Problem Statement Given a string of lowercase English alphabets, construct a new string where: Each character is repeated according to its 1-based index (1st char repeated 1 time, 2nd char repeated 2 times, etc.) The first letter of each repeated group is capitalized Different character groups are separated by dashes ('-') ...
Read More