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 497 of 801
Alternate addition multiplication in an array - 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, 4, 1, 2, 3, 4, 3]; Then the output should be calculated like this: 1*2 + 4*1 + 2*3 + 4*3 2 + 4 + 6 + 12 And the final output should be: 24 How It Works The algorithm pairs consecutive elements starting from index 0. Each pair is multiplied ...
Read MoreSwitch case calculator in JavaScript
Let's say, we are required to write a JavaScript function that takes in a string like these to create a calculator − "4 add 6" "6 divide 7" "23 modulo 8" Basically, the idea is that the string will contain two numbers on either sides and a string representing the operation in the middle. The string in the middle can take one of these five values − "add", "divide", "multiply", "modulo", "subtract" Our job is to return the correct result based on the string Syntax switch (expression) { ...
Read MoreAdd two array keeping duplicates only once - JavaScript
When working with two arrays, you often need to merge them while keeping only unique values. This operation combines arrays and removes duplicates in a single step. const arr1 = [2, 4, 5, 3, 7, 8, 9]; const arr2 = [1, 4, 5, 2, 3, 7, 6]; We need to write a JavaScript function that takes two arrays and returns a new array with all duplicates removed (each element appears only once). Using Set with Spread Operator (Recommended) The most efficient approach uses ES6 Set to automatically remove duplicates: const arr1 = ...
Read MoreCalculate difference between circumference and area of a circle - JavaScript
In this tutorial, we'll learn how to calculate the difference between the area and circumference of a circle using JavaScript. This is a common mathematical problem that demonstrates basic geometry calculations and JavaScript functions. Circle Formulas Before we start coding, let's review the formulas: Area of a circle: π × r² Circumference of a circle: 2 × π × r Where r is the radius of the circle and π (pi) is approximately 3.14159. JavaScript Implementation We'll create a function that takes the radius as input and returns the absolute difference between area ...
Read MoreProgram to pick out duplicate only once - JavaScript
We have an array of literals that contains some duplicate values appearing for many times like this: const arr = [1, 4, 3, 3, 1, 3, 2, 4, 2, 1, 4, 4]; We are required to write a JavaScript function that takes in this array and pick out all the duplicate entries from the original array and only once. So, for the above array, the output should be: [1, 4, 3, 2] Method 1: Using indexOf and lastIndexOf This approach checks if an element's first and last occurrence positions are ...
Read MoreTranspose of a two-dimensional array - JavaScript
The transpose of a matrix (2-D array) is simply a flipped version of the original matrix. We can transpose a matrix by switching its rows with its columns. For example, if we have a 3×3 matrix where the first row is [1, 1, 1], after transposing, the first column becomes [1, 1, 1]. Understanding Matrix Transpose Let's visualize how transposition works with a simple example: Original Matrix: [1, 1, 1] [2, 2, 2] [3, 3, 3] Transposed Matrix: [1, 2, 3] [1, 2, 3] [1, 2, 3] Method 1: In-Place Transpose ...
Read MoreImplement divide & conquer logic in JavaScript to implement QuickSort
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 It Works The algorithm follows these steps: Choose a pivot element (typically the middle element) Partition the array so elements smaller than pivot go to the ...
Read MoreFind all prime factors of a number - JavaScript
We are required to write a JavaScript function that takes in a number and returns an array of all the prime numbers that exactly divide the input number. For example, if the input number is 18, the prime factors are 2 and 3 because 18 = 2 × 3². Then the output should be − [2, 3] Understanding Prime Factorization Prime factorization breaks down a number into its prime components. A prime factor is a prime number that divides the original number exactly. Method 1: Using Helper Function to Check Primes ...
Read MoreLargest and smallest word in a string - JavaScript
We need to write a JavaScript function that takes a string and returns an array containing the smallest and largest words from the string based on their length. For example, if we have the string: const str = "Hardships often prepare ordinary people for an extraordinary destiny"; The output should be: const output = ["an", "extraordinary"]; The word "an" has 2 characters (smallest) and "extraordinary" has 13 characters (largest). Example Here's the complete implementation: const str = "Hardships often prepare ordinary people for an extraordinary destiny"; ...
Read MoreLoad a text file and find number of characters in the file - JavaScript
Suppose we have a data.txt file that lives in the same directory as our NodeJS file. Suppose the content of that file is − Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s. We are required to write a ...
Read More