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 267 of 801
Counting steps to make number palindrome in JavaScript
We are required to write a JavaScript function that takes in a number, num, as the first and the only argument. Our function should return the number of special steps needed to obtain a palindrome. The special step is: "reverse the digits, and add to the original number". If the resulting number is not a palindrome, repeat the procedure with the sum until the resulting number is a palindrome. Problem Example For example, if the input to the function is: const num = 87; The expected output is 4 steps because: ...
Read MoreRemoving parentheses from mathematical expressions in JavaScript
When working with mathematical expressions in JavaScript, you may need to remove parentheses while preserving the correct operations and operands. This involves carefully tracking signs and handling nested expressions. Problem We need to write a JavaScript function that takes a string of mathematical expressions and removes parentheses while keeping operations and operands in their correct positions with proper signs. For example, if the input is: const str = 'u-(v-w-(x+y))-z'; The expected output should be: u-v+w+x+y-z How It Works The algorithm uses a stack to track sign changes when ...
Read MoreLimiting elements occurrences to n times in JavaScript
Problem We are required to write a JavaScript function that takes in an array of integers, arr, that may contain duplicates as the first argument, and a number, num, as the second and final argument. The task of our function is to iterate through the array and check whether there exists some number that appears for more than n times in the array. If there exists any such element, we should delete its extra occurrences to limit its occurrence to at most num. For example, if the input to the function is: Input ...
Read MoreEncoding 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 More