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 257 of 801
Longest string consisting of n consecutive strings in JavaScript
Problem We are required to write a JavaScript function that takes in an array of strings. Our function should create combinations by combining all possible n consecutive strings in the array and return the longest such string that comes first. Example Following is the code − const arr = ["zone", "abigail", "theta", "form", "libe", "zas", "theta", "abigail"]; const num = 2; function longestConsec(strarr, k) { if (strarr.length == 0 || k > strarr.length || k longStr.length) { ...
Read MoreCounting rings in letters using JavaScript
We need to write a JavaScript function that counts the number of rings (closed loops) present in letters of a string. Different letters contain different numbers of rings based on their visual structure. Understanding Letter Rings Letters with rings are those that contain closed loops in their visual representation: One ring: A, D, O, P, Q, R, a, b, d, e, g, o, p, q Two rings: B (has two closed loops) No rings: All other letters Example Implementation const str = 'some random text string'; function countRings(str) { ...
Read MoreRemoving letters to make adjacent pairs different using JavaScript
We are required to write a JavaScript function that takes in a string containing only 'A', 'B' and 'C'. Our function should find the minimum number of characters needed to be removed from the string so that no two adjacent characters are the same. Problem Statement Given a string with characters 'A', 'B', and 'C', we need to remove the minimum number of characters to ensure all adjacent pairs are different. For example, in "AAB", we need to remove one 'A' to get "AB". Algorithm Approach The strategy is to iterate through the string and whenever ...
Read MoreMoving vowels and consonants using JavaScript
We are required to write a JavaScript function that takes in a string of English alphabets. Our function should construct a new string where every consonant is pushed forward 9 places through the alphabet, and every vowel is pushed backward 5 places. If they pass 'z', start again at 'a', and if they go before 'a', wrap around to 'z'. Problem Breakdown The transformation rules are: Vowels (a, e, i, o, u): Move backward 5 positions Consonants: Move forward 9 positions Non-alphabetic characters: Keep unchanged Wrapping: Use modulo arithmetic to handle alphabet boundaries Example ...
Read MoreTransforming array of numbers to array of alphabets using JavaScript
Problem We need to write a JavaScript function that takes an array of numbers and transforms it into a string with four parts separated by hyphens. Each part creates a 4-character "word" from the first two and last two elements of the array under different conditions. The four parts are: Characters from the original array (first, second, second-last, last) Same as above, after sorting the array in ascending order Same as above, after sorting the array in descending order Same as above, after converting to ASCII characters and sorting alphabetically Example Implementation Here's ...
Read MoreSum of perimeter of all the squares in a rectangle using JavaScript
Problem: Suppose there are 5 squares embedded inside a rectangle like this − 1 1 2 3 5 Side lengths: 1, 1, 2, 3, 5 Perimeters: 4, 4, 8, 12, 20 The squares follow a Fibonacci-like pattern ...
Read MoreObtaining maximum number via rotating digits in JavaScript
We are required to write a JavaScript function that takes in a positive integer n and returns the maximum number we can get by performing only left rotations on the digits of the number. Problem Left rotation means moving the first digit to the end. For example, rotating 12345 once gives 23451, rotating again gives 34512, and so on. We need to find which rotation produces the largest number. Example Let's implement the solution to find the maximum number through left rotations: const num = 56789; const findMaximum = (num = 1) => ...
Read MoreExpanding binomial expression using JavaScript
We need to write a JavaScript function that expands binomial expressions of the form (ax+b)^n, where a and b are integers, x is a variable, and n is a natural number. The function returns the expanded polynomial as a string. Problem Statement Given an expression like (8a+6)^4, we need to expand it using the binomial theorem. The result should be in the form ax^b+cx^d+ex^f... with coefficients and powers in decreasing order. Understanding the Binomial Theorem The binomial theorem states that (x+y)^n = Σ(C(n, k) * x^(n-k) * y^k) where C(n, k) is the binomial coefficient. ...
Read MoreCounting specific digits used in squares of numbers using JavaScript
We need to write a JavaScript function that takes an integer n (n >= 0) and a digit d (0
Read MoreFinding length, width and height of the sheet required to cover some area using JavaScript
Problem Statement We need to write a JavaScript function that calculates the number of wallpaper sheets required to cover a room's walls. Given the room's length, width, and height, we must determine how many sheets are needed when each sheet has dimensions of 0.52 units width and 10 units length. The function should return 15% more sheets than the calculated requirement to account for waste and cutting. Understanding the Calculation The total wall area of a rectangular room is calculated using the formula: 2 × (length + width) × height. This covers all four walls, excluding ...
Read More