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 192 of 801
Sorting an array of literals using quick sort in JavaScript
We are required to write a JavaScript function that takes in an array of numbers and uses the quick sort algorithm to sort it. QuickSort Algorithm QuickSort is a divide-and-conquer algorithm that works by selecting a 'pivot' element from the array and partitioning other elements into two sub-arrays according to whether they are less than or greater than the pivot. The sub-arrays are then sorted recursively. How QuickSort Works The algorithm follows these steps: Choose a pivot element from the array (typically the middle element) Partition the ...
Read MoreDetermining sum of array as even or odd in JavaScript
Problem We are required to write a JavaScript function that takes in an array of integers arr. Our function should return the string 'odd' if the sum of all the elements of the array is odd or 'even' if it's even. Example Following is the code − const arr = [5, 1, 8, 4, 6, 9]; const assignSum = (arr = []) => { const sum = arr.reduce((acc, val) => { return acc + val; }, 0); ...
Read MoreAlternate casing a string in JavaScript
We are required to write a JavaScript function that takes in a string and constructs a new string with all the uppercase characters from the original string converted to lowercase and all the lowercase characters converted to uppercase from the original string. For example: If the string is − const str = 'The Case OF tHis StrinG Will Be FLiPped'; Expected Output Then the output should be − const output = 'tHE cASE of ThIS sTRINg wILL bE flIpPED'; Using Manual Character Code Manipulation This approach manually checks character ...
Read MoreCan all array elements mesh together in JavaScript?
Two words can mesh together if the ending substring of the first word is the starting substring of the second word. For instance, "robinhood" and "hoodie" can mesh together because "hood" appears at the end of "robinhood" and at the start of "hoodie". We need to write a JavaScript function that takes an array of strings and checks if all consecutive words mesh together. If they do, the function returns the meshed letters as a string, otherwise it returns an empty string. How It Works The solution uses a regular expression to find overlapping substrings between consecutive ...
Read MoreAlgorithm for matrix multiplication in JavaScript
Matrix multiplication is a fundamental operation in linear algebra where two matrices are multiplied to produce a third matrix. In JavaScript, we can implement this algorithm using nested loops to calculate the dot product of rows and columns. Matrix Multiplication Rules For matrix multiplication to be valid, the number of columns in the first matrix must equal the number of rows in the second matrix. If matrix A is m×n and matrix B is n×p, the result will be an m×p matrix. Matrix A (m×n) ...
Read MoreProgram to implement Bucket Sort in JavaScript
Bucket Sort is an efficient sorting algorithm that works by distributing elements into multiple buckets based on their values, then sorting each bucket individually. This approach is particularly effective when the input is uniformly distributed across a range. How Bucket Sort Works The algorithm follows these key steps: Find the minimum and maximum values in the array Create a specific number of buckets to hold ranges of values Distribute array elements into appropriate buckets Sort each bucket using a suitable sorting algorithm (like insertion sort) Concatenate all sorted buckets to get the final result ...
Read MoreRearranging digits to form the greatest number using JavaScript
Problem We are required to write a JavaScript function that takes in one positive three-digit integer and rearranges its digits to get the maximum possible number. Approach To create the maximum number, we need to arrange the digits in descending order. We can convert the number to a string, split it into individual digits, sort them in descending order, and join them back. Example Following is the code − const num = 149; const maxRedigit = function(num) { if(num < 100 || num > 999) ...
Read MoreComputing zeroes (solutions) of a mathematical equation in JavaScript
We are required to write a JavaScript function that takes in three numbers representing the coefficients of a quadratic equation (ax² + bx + c = 0). The function should find the real roots of the equation or return false if the roots are complex (non-real). A quadratic equation has real roots when its discriminant (b² - 4ac) is greater than or equal to zero. The roots are calculated using the quadratic formula: x = (-b ± √discriminant) / (2a). Syntax // Quadratic formula: x = (-b ± √(b² - 4ac)) / (2a) // Discriminant = ...
Read MoreThird smallest number in an array using JavaScript
Problem We are required to write a JavaScript function that takes in an array of numbers of length at least three. Our function should simply return the third smallest number from the array. Example Following is the code − const arr = [6, 7, 3, 8, 2, 9, 4, 5]; const thirdSmallest = () => { const copy = arr.slice(); for(let i = 0; i < 2; i++){ const minIndex = copy.indexOf(Math.min(...copy)); copy.splice(minIndex, ...
Read MoreFinding place value of a number in JavaScript
In JavaScript, finding the place value of digits means extracting each digit's positional worth. For example, in the number 1234, the place values are [1000, 200, 30, 4]. We need to write a function that takes a positive integer and returns an array with the place values of all digits. Problem Example If the input number is: const num = 1234; Then the output should be: [1000, 200, 30, 4] Using Recursive Approach This problem is well-suited for recursion since we need to process each digit and calculate ...
Read More