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 234 of 801
Modify an array based on another array JavaScript
When working with arrays in JavaScript, you might need to modify one array based on patterns or elements found in another array. This article demonstrates how to join consecutive elements from a reference array when they appear as combined phrases in a second array. Problem Statement Suppose we have a reference array of individual words: const reference = ["your", "majesty", "they", "are", "ready"]; And we want to modify it based on another array that contains some words joined together: const another = ["your", "they are"]; The goal is to create ...
Read MoreCalculating the sum of digits of factorial JavaScript
We are required to write a JavaScript function that takes in a number. The function should first calculate the factorial of that number and then it should return the sum of the digits of the calculated factorial. For example, for the number 6, the factorial will be 720, so the sum of digits (7 + 2 + 0) should be 9. Understanding the Problem This problem involves two steps: Calculate the factorial of a given number Sum all digits in the factorial result Step 1: Calculate ...
Read MoreInsert a number into a sorted array of numbers JavaScript
We are required to write a JavaScript function that takes in a sorted array of numbers as the first argument and a single number as the second argument. The function should push the number specified as the second argument into the array without distorting the sorting of the elements. We are required to do this without creating another array. Approach: Binary Search with In-Place Insertion The solution uses binary search to find the correct insertion position, then shifts elements using a swapping technique to maintain order without extra space. Example const arr = ...
Read MoreCheck if a string is entirely made of the same substring JavaScript
In JavaScript, you can check if a string is entirely made of repeated substrings using various approaches. This is useful for pattern validation and string analysis. The problem requires that the string consists of a repeated character sequence with at least one repetition. For example, "aa" contains two "a" substrings, "abcabcabc" contains three "abc" substrings, but "ababa" fails because it has an extra character. Examples of Valid and Invalid Patterns "aa" should return true because it entirely contains two strings "a" "aaa" should return true because it entirely ...
Read MoreCompare Strings in JavaScript and return percentage of likeliness
We are required to write a JavaScript function that can compare two strings and return the percentage likeliness of how much they are alike. The percentage will be nothing but a measure of many characters the two strings have in common. If they are completely similar the output should be 100, and if they contain no common character at all, the output should be 0. Understanding the Algorithm This implementation uses the Levenshtein distance algorithm to calculate string similarity. The Levenshtein distance measures the minimum number of single-character edits (insertions, deletions, or substitutions) needed to change one ...
Read MoreSorting array of Number by increasing frequency JavaScript
We are required to write a JavaScript function that takes in an array of numbers that might contain some repeating numbers. The function should sort the array such that the elements that are repeated for the least number of times appears first followed by the elements with increasing frequency. For example − If the input array is − const arr = [1, 1, 2, 2, 2, 3]; Then the sorted array should be − const output = [3, 1, 1, 2, 2, 2]; How It Works The solution ...
Read MoreJavaScript - find distance between items on array
In JavaScript, finding the distance between items in an array involves calculating the difference between each element and all succeeding elements. This creates a matrix of distances that can be useful for various algorithms and data analysis. Suppose we have a sorted (increasing order) array of Numbers like this: const arr = [2, 5, 7, 8, 9]; We need to write a JavaScript function that takes in one such array. The function should construct a new subarray for each element of the input array containing the differences between that element and all succeeding elements. ...
Read MoreFinding Number of Days Between Two Dates JavaScript
Finding the number of days between two dates is a common requirement in JavaScript applications. While you can implement complex date calculations manually, JavaScript's built-in Date object provides a much simpler and more reliable approach. The Simple Approach Using Date Objects The most straightforward method is to convert date strings to Date objects and calculate the difference in milliseconds, then convert to days: const daysBetweenDates = (date1, date2) => { const firstDate = new Date(date1); const secondDate = new Date(date2); ...
Read MoreUnique intersection of arrays in JavaScript
We are required to write a JavaScript function that takes in two arrays of numbers, let's say arr1 and arr2. The function should find the intersection between the elements of the array. i.e., the elements that appear in both the arrays. The only condition is that if we encountered one element before as intersected, we should not consider it again even if appears again in both the arrays. For example − If the input arrays are − const arr1 = [1, 5, 7, 3, 1]; const arr2 = [1, 7, 3, 1, 6]; ...
Read MoreDecimal to binary conversion using recursion in JavaScript
We are required to write a JavaScript function that takes in a number as the first and the only argument. The function should use recursion to construct a string representing the binary notation of that number. For example − f(4) = '100' f(1000) = '1111101000' f(8) = '1000' How Binary Conversion Works Binary conversion involves repeatedly dividing a number by 2 and collecting the remainders. The remainders, read in reverse order, form the binary representation. Decimal 4 to Binary 4 ÷ 2 ...
Read More