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 169 of 534
Encoding string based on character frequency in JavaScript
We are required to write a JavaScript function that takes in a string as an argument and creates a new string based on character frequency. Each character in the new string is '(' if that character appears only once in the original string, or ')' if that character appears more than once. The comparison should be case-insensitive. Problem Statement Given a string, encode each character as: '(' - if the character appears only once ')' - if the character appears more than once Ignore case when counting character frequency For example, if the input is ...
Read MoreArray index to balance sums in JavaScript
Finding a balance point in an array means locating an index where the sum of elements on the left equals the sum of elements on the right. This is a common array manipulation problem in JavaScript. Problem Statement We need to write a JavaScript function that takes an array of integers and returns the index where the left sum equals the right sum. If no such index exists, return -1. For example, with the array [1, 2, 3, 4, 3, 2, 1], index 3 is the balance point because: Left side (indices 0-2): 1 + ...
Read MoreCounting possible APs within an array in JavaScript
Arithmetic Progression (AP) is a sequence of numbers where the difference between any two consecutive numbers is constant (called the common difference). For instance, 1, 2, 3, 4, 5, 6, … is an AP with a common difference of 1 (2 - 1 = 1). Problem Statement We need to write a JavaScript function that takes a sorted array of integers and returns the count of arithmetic progressions of size 3 that can be formed from the array elements. In each progression, the differences between consecutive elements must be the same. The input array is guaranteed ...
Read MoreReturning only odd number from array in JavaScript
JavaScript arrays often contain mixed data types, and finding specific patterns like the one odd number among evens (or vice versa) is a common programming challenge. This article demonstrates how to identify and return the single different element from an array. Problem Statement We need to write a JavaScript function that takes an array of integers as input. The array contains either all even numbers with one odd number, or all odd numbers with one even number. Our function should return this single different element. For example, if the input array is: const arr = ...
Read MoreReturning just greater array in JavaScript
We need to write a JavaScript function that takes an array of positive integers, joins them to form a single number, adds 1 to that number, and returns the result as an array of digits. Problem Statement Given an array of positive integers, we need to: Join the digits to form a single number Add 1 to that number Return the result as an array of individual digits For example, if the input array is [6, 7, 3, 9], it represents the number 6739. Adding 1 gives us 6740, which should be returned as [6, ...
Read MoreFinding length of repeating decimal part in JavaScript
When dividing 1 by certain numbers, the decimal result may have a repeating pattern. This function finds the length of that repeating decimal part, but only for numbers that are coprime with 10 (share no common factors except 1). Problem Statement We need to write a JavaScript function that: Checks if a number is coprime with 10 (shares no common factors except 1) If not coprime, returns -1 If coprime, returns the length of the repeating decimal part when 1 is divided by that number ...
Read MoreSorting one string by the order of second in JavaScript
We are required to write a JavaScript function that takes in two strings, str1 and str2 as the first and the second argument. Our function should sort str1 according to the order of characters as they appear in str2. Characters that appear first in str2 are placed first, followed by ones that come later, and finally followed by letters absent in str2. Problem Example For example, if the input to the function is: Input const str1 = 'coding'; const str2 = 'gncabdi'; Expected Output gncdio Output Explanation Looking at str2 ...
Read MoreASCII to hex and hex to ASCII converter class in JavaScript
In this tutorial, we'll create a JavaScript class that converts between ASCII and hexadecimal formats. This is useful for encoding text data, debugging, or working with binary protocols. Problem Statement We need to write a JavaScript class with two member functions: toHex: Takes an ASCII string and returns its hexadecimal equivalent toASCII: Takes a hexadecimal string and returns its ASCII equivalent For example, the string 'this is a string' should convert to hex '74686973206973206120737472696e67' and back to the original ASCII. How It Works ASCII to hex ...
Read MoreAdding binary without converting in JavaScript
Adding binary numbers without converting to decimal requires simulating manual binary addition with carry handling. This approach processes bits from right to left, just like traditional addition. Problem Statement We need to write a JavaScript function that takes two binary strings and returns their sum as a binary string, without converting to decimal numbers. Input: const str1 = '1101'; const str2 = '10111'; Expected Output: '100100' Algorithm Overview The solution works by: Reversing both strings to process from least significant bit Adding corresponding bits with carry propagation Building the ...
Read MoreCounting adjacent pairs of words in JavaScript
We are required to write a JavaScript function that takes in a string str that represents a sentence as the only argument. Our function should count and return the adjacent pair of identical words present in the string str. Our function should check the words ignoring their case, which means 'it' and 'It' should be counted as identical. Problem Statement For example, if the input to the function is: const str = 'This this is a a sample string'; The expected output should be: 2 Output Explanation: Because the ...
Read More