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 104 of 534
Algorithm 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 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 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 MoreReversing the even length words of a string in JavaScript
We are required to write a JavaScript function that takes in a string and reverses the words in the string that have an even number of characters in them. Let's say the following is our string: const str = 'This is an example string'; We want to reverse the even length words of the above string i.e. reverse the following words: This (4 characters) is (2 characters) an (2 characters) string (6 characters) The word "example" has 7 characters (odd length), so it remains unchanged. Syntax ...
Read MoreDifferences in two strings in JavaScript
We are required to write a JavaScript function that takes in two strings and find the number of corresponding dissimilarities in the strings. The corresponding elements will be dissimilar if they are not equal at the same position. Example Strings Let's say the following are our strings: const str1 = 'Hello world!!!'; const str2 = 'Hellp world111'; In this case, we need to compare each character at the same index and count how many positions have different characters. Method 1: Using a For Loop const str1 = 'Hello world!!!'; const str2 ...
Read MoreFinding the index of the first repeating character in a string in JavaScript
We are required to write a JavaScript function that takes in a string and returns the index of the first character that appears twice in the string. If there is no such character then we should return -1. Let's say the following is our string − const str = 'Hello world, how are you'; We need to find the index of the first repeating character. Understanding the Problem In the string "Hello world, how are you", we need to find which character repeats first. The character 'l' appears at index 2 and again ...
Read MoreColumn sum of elements of 2-D arrays in JavaScript
We have an array of arrays and are required to write a function that takes in this array and returns a new array that represents the sum of corresponding elements of original array. If the original array is: [ [43, 2, 21], [1, 2, 4, 54], [5, 84, 2], [11, 5, 3, 1] ] Then the output should be: [60, 93, 30, 55] This means we add all elements at index 0 (43+1+5+11=60), all elements at index 1 (2+2+84+5=93), ...
Read MoreFinding smallest number that satisfies some conditions in JavaScript
We are required to write a JavaScript function that takes in a number as the first argument, say n, and an array of numbers as the second argument. The function should return the smallest n-digit number which is a multiple of all the elements specified in the array. If there exist no such n-digit element then we should return the smallest such element. For example: If the array is − const arr = [12, 4, 5, 10, 9] For both n = 2 and n = 3, we need to find the smallest number ...
Read MoreFinding the inclination of arrays in JavaScript
We are required to write a JavaScript function that takes in an array of numbers and returns true if it's either strictly increasing or strictly decreasing, otherwise returns false. In Mathematics, a strictly increasing function is that function in which the value to be plotted always increases. Similarly, a strictly decreasing function is that function in which the value to be plotted always decreases. Understanding the Concept An array has a consistent inclination when: Strictly increasing: Each element is greater than the previous one Strictly decreasing: Each element ...
Read More