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 255 of 801
Finding number plate based on registration number in JavaScript
The car registering system of a city assigns unique identifiers to customers and corresponding number plates to their vehicles. We need to create a JavaScript function that converts a customer ID into its corresponding number plate format. Problem Statement The car registration system assigns two types of identifiers: Customer ID − A natural number between 0 and 17, 558, 423 (inclusive), assigned sequentially starting from 0 Number Plate − Contains a series ...
Read MoreSwapping string case using a binary number in JavaScript
We need to write a JavaScript function that takes a string and a number, then uses the binary representation of the number to determine which alphabetic characters should have their case swapped. Problem Each bit in the binary representation of the number specifies whether to swap the case for each alphabetic character: If the bit is 1, swap the case (lowercase → uppercase, uppercase → lowercase) If the bit is 0, leave the character as is When we reach the end of the binary representation, start again from the first bit Non-alphabetic characters remain unchanged ...
Read MoreBuilding an array of specific size with consecutive element sum being perfect square in JavaScript
We need to create a JavaScript function that arranges numbers 1 to n such that the sum of each pair of consecutive elements forms a perfect square. This is a classic backtracking problem that requires careful arrangement of numbers. Problem Understanding For an array of size n, we must arrange integers 1 through n where each adjacent pair sums to a perfect square. For example, if two consecutive numbers are 9 and 7, their sum (16) should be a perfect square (4²). Algorithm Approach We use a backtracking algorithm with these steps: Try each ...
Read MoreCreating all possible unique permutations of a string in JavaScript
We are required to write a JavaScript function that takes in a string str. Our function should create all permutations of the input string and remove duplicates, if present. This means, we have to shuffle all letters from the input in all possible orders. Problem When generating permutations of strings with repeated characters, we need to avoid duplicate permutations. For example, the string "aabb" should not generate identical permutations like "aabb" appearing multiple times. Algorithm Approach The solution uses recursion with duplicate checking: For each character position, try placing each unique character Skip duplicate ...
Read MoreReplacing dots with dashes in a string using JavaScript
We are required to write a JavaScript function that takes in a string and replaces all appearances of dots(.) in it with dashes(-). Input const str = 'this.is.an.example.string'; Expected Output const output = 'this-is-an-example-string'; All appearances of dots(.) in string str are replaced with dash(-) Method 1: Using replace() with Regular Expression The most efficient approach is using the built-in replace() method with a global regular expression: const str = 'this.is.an.example.string'; const replaceDots = (str = '') => { return str.replace(/\./g, '-'); }; ...
Read MoreConverting humanYears into catYears and dogYears in JavaScript
We need to write a JavaScript function that converts human age into equivalent cat and dog years. The conversion follows specific aging rules for the first two years, then a constant rate afterward. Problem Statement Create a function that takes human age in years and returns an array containing human years, cat years, and dog years. Input: const humanYears = 15; Expected Output: [15, 76, 89] Age Conversion Rules The aging calculation follows these rules: Year 1: Both cats and dogs age ...
Read MoreValidating string with reference to array of words using JavaScript
We need to write a JavaScript function that takes an array of valid words and a string, then checks if the string can be formed by concatenating one or more words from the array. Problem Statement Given an array of words and a target string, determine if the string can be constructed using words from the array. Words can be reused multiple times. Input: const arr = ['love', 'coding', 'i']; const str = 'ilovecoding'; Expected Output: true The string "ilovecoding" can be formed by concatenating "i" + "love" + "coding". ...
Read MoreCounting divisors of a number using JavaScript
We are required to write a JavaScript function that takes in a number and returns the count of its divisors. Problem Statement Input: const num = 30; Expected Output: const output = 8; Because the divisors of 30 are: 1, 2, 3, 5, 6, 10, 15, 30 Method 1: Simple Iteration Approach The straightforward approach is to iterate through all numbers from 1 to the given number and count those that divide evenly. const countDivisorsSimple = (num) => { ...
Read MoreNumbers obtained during checking divisibility by 7 using JavaScript
In mathematics, we can check if a number is divisible by 7 using a special algorithm. For any number of the form 10a + b (where a is all digits except the last, and b is the last digit), we calculate a - 2b. If this result is divisible by 7, then the original number is also divisible by 7. We repeat this process until we get a number with at most 2 digits, since it's easy to check divisibility by 7 for such small numbers. This article demonstrates how to implement this algorithm in JavaScript and count the ...
Read MoreReturning lengthy words from a string using JavaScript
We are required to write a JavaScript function that takes in a sentence of words and a number. The function should return an array of all words greater than the length specified by the number. Problem Statement Input: const str = 'this is an example of a basic sentence'; const num = 4; Expected Output: const output = [ 'example', 'basic', 'sentence' ]; Because these are the only three words with length greater than 4. Using for Loop The most straightforward approach is to split the string into ...
Read More