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 161 of 534
Searching for target string in a strangely sorted array in JavaScript
We are required to write a JavaScript function that takes in a word target and an array of sorted(by length(increasing), number of uppercase letters(decreasing), natural order) unique words which always contains the target. The task of our function is to find the index(0 based) of the target in the array of words, which would always be in the list. Understanding the Sorting Pattern The array is sorted by three criteria in order: Length: Shorter strings come first Uppercase count: Among same-length strings, more uppercase letters come first ...
Read MoreFinding reflection of a point relative to another point in JavaScript
Point reflection, also known as point symmetry, is a geometric transformation where a point P is reflected across a midpoint Q to create a new point P' that is equidistant from Q but in the opposite direction. P(6, -4) Q(11, 5) ...
Read MoreEncoding a number string into a string of 0s and 1s in JavaScript
We need to write a JavaScript function that encodes a decimal number string into binary based on specific rules. For each digit, we determine the number of bits required, prepend (k-1) zeros followed by 1, then append the digit's binary representation. Encoding Rules For each digit d in the number string: Let k be the number of bits needed to represent d in binary Write (k-1) zeros followed by the digit 1 Write digit d as a binary string Concatenate ...
Read MoreRetrieving n smallest numbers from an array in their original order in JavaScript
We need to write a JavaScript function that takes an array of numbers and a number n, then returns the n smallest numbers while preserving their original order in the array. Problem Given an array of numbers and a value n, we want to find the n smallest numbers but maintain their relative positions from the original array. The result should not be sorted numerically but should appear in the same order as they existed in the input array. Example Here's how we can solve this problem: const arr = [6, 3, 4, 1, ...
Read MoreSum array of rational numbers and returning the result in simplest form in JavaScript
We are required to write a JavaScript function that takes in an array of exactly two subarrays with two numbers each. Both the subarrays represent a rational number in fractional form. Our function should add the rational numbers and return a new array of two numbers representing the simplest form of the added rational number. Problem When adding two fractions like 1/2 + 1/3, we need to: Find a common denominator Add the numerators Simplify the result using the Greatest Common Factor (GCD) Solution The mathematical formula for adding fractions is: a/b + ...
Read MoreDecrypting source message from a code based on some algorithm in JavaScript
We need to write a JavaScript function that decrypts a message by reversing an encryption algorithm. The encryption process involves reversing the string and replacing letters with their ASCII codes in quotes. Understanding the Encryption Algorithm The original encryption algorithm works as follows: Reverse the message string Replace every letter with its ASCII code in quotes (A becomes '65', h becomes '104') Keep digits and spaces unchanged The Decryption Function To decrypt, we need to reverse this process by applying the same algorithm to the encrypted message: const str = '12 hello ...
Read MoreFinding the sum of floors covered by an elevator in JavaScript
Problem We need to write a JavaScript function that calculates the total number of floors covered by an elevator. The function takes an array representing the floor numbers where the elevator stopped, and returns the sum of distances traveled between consecutive stops. Understanding the Logic The elevator covers floors by moving between consecutive stops. If it goes from floor 7 to floor 1, it covers 6 floors (7-1). If it then goes from floor 1 to floor 7, it covers another 6 floors (7-1). The total distance is the sum of absolute differences between consecutive floors. ...
Read MoreAdding and searching for words in custom Data Structure in JavaScript
We are required to design a data structure in JavaScript that supports two key operations: adding words and searching with pattern matching using regular expressions. Problem The data structure must support the following operations: addWord - adds a word to the data structure using arrays or any other storage mechanism search - searches for a literal word or a regular expression pattern containing lowercase letters "a-z" or "." where "." can represent any single letter Example Requirements addWord("sir") addWord("car") addWord("mad") search("hell") === false search(".ad") === true search("s..") === true ...
Read MoreLongest possible string built from two strings in JavaScript
We need to write a JavaScript function that takes two strings containing only letters from a to z and returns the longest possible sorted string with distinct letters from both strings combined. Problem Statement Given two strings s1 and s2, create a function that: Combines both strings Removes duplicate characters Returns a sorted string containing each unique letter only once Solution Using Array Methods Here's an implementation that combines the strings, removes duplicates, and sorts the result: const str1 = "xyaabbbccccdefww"; const str2 = "xxxxyyyyabklmopq"; const longestPossible = (str1 = '', ...
Read MoreFinding life path number based on a date of birth in JavaScript
A person's Life Path Number is calculated by adding each individual digit in that person's date of birth, then reducing it to a single digit number through repeated digit summation. Problem We need to write a JavaScript function that takes a date in "yyyy-mm-dd" format and returns the life path number for that date of birth. How It Works The calculation involves three steps: Sum all digits in the year, month, and day separately Reduce each sum to a single digit by adding digits repeatedly Add the three single digits and reduce to final ...
Read More