Web Development Articles

Page 192 of 801

Sorting an array of literals using quick sort in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 458 Views

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 More

Determining sum of array as even or odd in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 473 Views

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 More

Alternate casing a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

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 More

Can all array elements mesh together in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 192 Views

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 More

Algorithm for matrix multiplication in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 2K+ Views

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 More

Program to implement Bucket Sort in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 738 Views

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 More

Rearranging digits to form the greatest number using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 774 Views

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 More

Computing zeroes (solutions) of a mathematical equation in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 154 Views

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 More

Third smallest number in an array using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 639 Views

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 More

Finding place value of a number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 865 Views

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
Showing 1911–1920 of 8,010 articles
« Prev 1 190 191 192 193 194 801 Next »
Advertisements