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 191 of 801
Object to Map conversion in JavaScript
In JavaScript, you can convert an object to a Map using several approaches. Maps offer advantages over objects like guaranteed key insertion order and the ability to use any data type as keys. Suppose we have an object like this: const obj = { name: "Jai", age: 32, occupation: "Software Engineer", address: "Dhindosh, Maharashtra", salary: "146000" }; We need to convert this object into a Map while preserving all key-value pairs. Using Object.entries() (Recommended) The most concise approach uses Object.entries() with the Map ...
Read MoreEven index sum in JavaScript
We need to write a JavaScript function that calculates the sum of all integers at even indices (0, 2, 4, etc.) in an array, then multiplies this sum by the last element of the array. Problem Statement Given an array of integers, find the sum of elements at even indices and multiply by the last element. Input: [4, 1, 6, 8, 3, 9] Even indices: 0, 2, 4 → elements: 4, 6, 3 Sum: 4 + 6 + 3 = 13 Last element: 9 Result: 13 × 9 = 117 Solution const ...
Read MoreAddition 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 MoreSum of all positives present in an array in JavaScript
We are required to write a JavaScript function that takes in an array of numbers (positive and negative). Our function should calculate and return the sum of all the positive numbers present in the array. Method 1: Using reduce() with Helper Function This approach uses a helper function to check if a number is positive and reduce() to accumulate the sum: const arr = [5, -5, -3, -5, -7, -8, 1, 9]; const sumPositives = (arr = []) => { const isPositive = num => typeof num === 'number' && num ...
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 MoreBoolean Gates in JavaScript
We are required to write a JavaScript function that takes in an array of Boolean values and a logical operator. Our function should return a Boolean result based on sequentially applying the operator to the values in the array. Problem Given an array of Boolean values and a logical operator (AND, OR, XOR), we need to apply the operator sequentially to all values and return the final result. Solution Here's the implementation that handles three common Boolean operations: const array = [true, true, false]; const op = 'AND'; function logicalCalc(array, op) { ...
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 MoreReversing negative and positive numbers in JavaScript
Problem We are required to write a JavaScript function that takes in a number and returns its reversed number. One thing that we should keep in mind is that numbers should preserve their sign; i.e., a negative number should still be negative when reversed. Example Following is the code − const num = -224; function reverseNumber(n) { let x = Math.abs(n) let y = 0 while (x > 0) { y = y * 10 + (x ...
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 MoreReversing all alphabetic characters in JavaScript
We are required to write a JavaScript function that takes in a string and reverses only the alphabetic characters, omitting all non-alphabetic characters from the result. Problem Given a string containing both alphabetic and non-alphabetic characters, we need to create a function that filters out non-alphabetic characters and returns the alphabetic characters in reverse order. Example Following is the code − const str = 'exa13mple'; function reverseLetter(str) { const res = str.split('') .reverse() .filter(val ...
Read More