Javascript Articles

Page 477 of 534

Convert object to a Map - JavaScript

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

In JavaScript, you can convert an object to a Map using several approaches. Maps offer better performance for frequent additions and deletions compared to objects. Sample Object Let's start with this example object: const obj = { name: "Vikas", age: 45, occupation: "Frontend Developer", address: "Tilak Nagar, New Delhi", experience: 23, salary: "98000" }; Method 1: Using Object.entries() (Recommended) The most concise approach uses Object.entries() which returns key-value pairs ...

Read More

Alternate addition multiplication in an array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 282 Views

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 More

Switch case calculator in JavaScript

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

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 More

Add two array keeping duplicates only once - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 141 Views

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 More

Calculate difference between circumference and area of a circle - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 229 Views

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 More

Program to pick out duplicate only once - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 703 Views

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 More

Transpose of a two-dimensional array - JavaScript

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

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 More

Implement divide & conquer logic in JavaScript to implement QuickSort

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 659 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 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 More

Find all prime factors of a number - JavaScript

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

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 More

Largest and smallest word in a string - JavaScript

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

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 More
Showing 4761–4770 of 5,340 articles
« Prev 1 475 476 477 478 479 534 Next »
Advertisements