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 159 of 534
Moving 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 MoreFinding path to the end of maze using JavaScript
Problem We are required to write a JavaScript function that takes in a matrix of N * N order. The walls in the matrix are marked by 'W' and empty positions are marked by '_'. We can move in any of the four directions at any point. Our function should return true if we can reach the end position [N - 1, N - 1] from the starting position [0, 0], false otherwise. Algorithm Approach We'll use a Breadth-First Search (BFS) algorithm to find if a path exists from the starting position to the end position. ...
Read MoreImplement a custom function similar to Array.prototype.includes() method using JavaScript
We need to create a custom JavaScript function that mimics the behavior of Array.prototype.includes(). This function should check if a value exists in an array and return a boolean result. Problem We are required to write a JavaScript function that lives on the prototype object of Array. It must take in a literal value, and return true if that value is present in the array it is being called upon, false otherwise. Basic Implementation Here's a simple implementation using a for loop to iterate through the array: const arr = [1, 2, 3, 4, ...
Read MoreFinding average age from array of Objects using JavaScript
Problem We need to write a JavaScript function that takes an array of objects containing people's data and calculates the average age from the age property. Example Following is the code: const people = [ { fName: 'Ashish', age: 23 }, { fName: 'Ajay', age: 21 }, { fName: 'Arvind', age: 26 }, { fName: 'Mahesh', age: 28 }, { fName: 'Jay', age: 19 }, ]; const findAverageAge = (arr = []) => { ...
Read More