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 95 of 534
Breaking camelCase syntax in JavaScript
We need to write a JavaScript function that takes a camelCase string and converts it into a readable format by adding spaces before uppercase letters. Our function should construct and return a new string that splits the input string using a space between words. Problem Example For example, if the input to the function is: Input const str = 'thisIsACamelCasedString'; Expected Output 'this Is A Camel Cased String' Solution Using String Iteration The approach iterates through each character and adds a space before uppercase letters (except the first character): ...
Read MoreSorting numbers based on their digit sums in JavaScript
We are required to write a JavaScript function that takes in an array of positive integers and sorts them based on their digit sums in descending order. Numbers with higher digit sums appear first. Problem Statement Given an array of positive integers, sort the array so that numbers with the highest digit sum come first, followed by numbers with lesser digit sums. For example, if the input array is: const arr = [5, 34, 1, 13, 76, 8, 78, 101, 57, 565]; The output should be: [565, 78, 76, 57, 8, ...
Read MoreConverting alphabets to Greek letters in JavaScript
Converting alphabets to Greek letters in JavaScript requires creating a mapping between English and Greek characters, then replacing each character accordingly. Character Mapping First, let's establish the mapping between English and Greek letters: A=α (Alpha) B=β (Beta) D=δ (Delta) E=ε (Epsilon) I=ι (Iota) K=κ (Kappa) N=η (Eta) O=θ (Theta) P=ρ (Rho) R=π (Pi) T=τ (Tau) U=μ (Mu) V=υ (Upsilon) W=ω ...
Read MoreComputing Ackerman number for inputs in JavaScript
The Ackermann Function is a classic example of a recursive function that grows extremely quickly. It's notable for being a total computable function that is not primitive recursive, making it an important concept in theoretical computer science. Problem Statement We need to write a JavaScript function that takes two non-negative integers, m and n, and returns the Ackermann number A(m, n) defined by the following mathematical definition: A(m, n) = n+1 if m=0 A(m, n) = A(m-1, 1) if m>0 and n=0 A(m, n) = A(m-1, A(m, n-1)) if m>0 and n>0 Implementation ...
Read MoreSplitting number into n parts close to each other in JavaScript
We are required to write a JavaScript function that takes in a number, num, as the first argument and another number, parts, as the second argument. Our function should split the number num into exactly parts numbers while keeping these conditions in mind: The numbers should be as close as possible The numbers should be even (if possible) The ordering of numbers is not important. Problem Analysis To split a number into n parts as evenly as possible, we need to: Calculate ...
Read MoreAlternatingly combining array elements in JavaScript
We are required to write a JavaScript function that takes in any number of arrays of literals as input. Our function should prepare a new array that contains elements picked alternatingly from all the input arrays. For example, if the input to the function is − Problem Input const arr1 = [1, 2, 3, 4]; const arr2 = [11, 12, 13, 14]; const arr3 = ['a', 'b', 'c']; Expected Output const output = [1, 11, 'a', 2, 12, 'b', 3, 13, 'c', 4, 14]; The function should take ...
Read MorePlacing integers at correct index in JavaScript
We need to write a JavaScript function that takes a string containing only square brackets '[' and ']' and determines the minimum number of brackets to add to make it balanced. Problem Statement Given a string consisting of only '[' and ']' characters, find the minimum number of brackets that need to be added to make the string valid (properly balanced). A valid bracket string means every opening bracket '[' has a corresponding closing bracket ']' that comes after it. Example Input and Output Input: const str = '[]]'; Output: 1 ...
Read MoreFinding score of brackets in JavaScript
We are required to write a JavaScript function that takes in a balanced square bracket string as an argument and computes its score based on specific rules. Scoring Rules The bracket scoring follows these rules: [] has score 1 AB has score A + B, where A and B are balanced bracket strings [A] has score 2 * A, where A is a balanced bracket string Example Input and Output For the input string '[][]': Input: '[][]' Output: 2 This works because [] scores 1, and [][] is two ...
Read MoreCount and return the number of characters of str1 that makes appearances in str2 using JavaScript
We need to write a JavaScript function that takes two strings as parameters and counts how many characters from the first string appear in the second string, including duplicate occurrences. Problem Statement Given two strings str1 and str2, count how many characters from str1 also appear in str2. If a character appears multiple times in str2, count each occurrence separately. Example: str1 = 'Kk' contains characters 'K' and 'k' str2 = 'klKKkKsl' contains: k(1), l(1), K(2), K(3), k(4), K(5), s(6), l(7) Characters from str1 found in str2: k, K, K, k, K = 5 matches ...
Read MoreConvert mixed case string to lower case in JavaScript
In JavaScript, there are multiple ways to convert a mixed-case string to lowercase. The most common approach is using the built-in toLowerCase() method, but you can also implement a custom solution using character codes. Using the Built-in toLowerCase() Method The simplest and most efficient way is to use JavaScript's built-in toLowerCase() method: const str = 'ABcD123'; const output = str.toLowerCase(); console.log(output); abcd123 Custom Implementation Using Character Codes For educational purposes, here's how to implement a custom convertToLower() function that converts uppercase letters (ASCII 65-90) to lowercase by adding 32 to ...
Read More