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
Object Oriented Programming Articles
Page 28 of 589
Addition multiplication ladder in an array in JavaScript
We are required to write a JavaScript function that takes in an array of numbers and returns the alternative multiplicative sum of the elements. For example: If the array is − const arr = [1, 2, 3, 4, 5, 6, 7]; Then the output should be calculated like this − 1*2+3*4+5*6+7 2+12+30+7 And the output should be − 51 Let's write the code for this function − How It Works The algorithm pairs elements at even indices with their next element, multiplies them, and adds ...
Read MoreMerge and remove duplicates in JavaScript Array
Merging arrays and removing duplicates is a common task in JavaScript. There are several approaches to accomplish this, from traditional loops to modern ES6 methods. Problem Statement Given two arrays of numbers, we need to combine them into a single array where each element appears only once. const arr1 = [2, 4, 5, 3, 7, 8, 9]; const arr2 = [1, 4, 5, 2, 3, 7, 6]; console.log("Array 1:", arr1); console.log("Array 2:", arr2); Array 1: [ 2, 4, 5, 3, 7, 8, 9 ] Array 2: [ 1, 4, 5, 2, 3, 7, ...
Read MoreFiltering out the non-unique value to appear only once in JavaScript
We have an array that contains some duplicate values appearing multiple times. We need to extract only the elements that appear more than once in the array, but show each duplicate element only once in the result. const arr = [1, 4, 3, 3, 1, 3, 2, 4, 2, 1, 4, 4]; console.log("Original array:", arr); Original array: [1, 4, 3, 3, 1, 3, 2, 4, 2, 1, 4, 4] We need to write a JavaScript function that filters out elements that appear multiple times and returns each duplicate element only once. For ...
Read MoreChecking for the similarity of two 2-D arrays in JavaScript
We are required to write a JavaScript function that takes in two 2-D arrays and returns a boolean based on the check whether the arrays are equal or not. The equality of these arrays, in our case, is determined by the equality of corresponding elements. Both the arrays should have same number of rows and columns. Also, arr1[i][j] === arr2[i][j] should yield true for all i between [0, number of rows] and j between [0, number of columns] Method 1: Basic Nested Loop Approach This method compares each element position by position using nested loops: ...
Read MoreSorting 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 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 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 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 More