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 486 of 534
Does this array contain any majority element - JavaScript
Given an array of numbers, any element of the array will be a majority element if that element appears more than array length's 1/2 times in the array. For example, if the length of array is 7, then if there's any element in the array that appears for at least 4 number of times, it will be considered a majority. It's quite apparent that any particular array can have at most one majority element. We are required to write a JavaScript function that takes in an array of numbers with repetitive values and returns true if there exists ...
Read MoreThrice sum of elements of array - JavaScript
We are required to write a JavaScript function that takes in an array of Numbers and returns a new array with elements as sum of three consecutive elements from the original array. For example, if the input array is: const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Then the output should be: const output = [3, 12, 21, 9]; The function groups elements in sets of three and calculates their sum: (0+1+2=3), (3+4+5=12), (6+7+8=21), and (9+0+0=9) for the remaining element. How It Works The ...
Read MoreFinding the rotation of an array in JavaScript
We are required to write a JavaScript function that takes in an array and a number n. Our function should rotate the array by n elements, i.e., take n elements from the front and put them to the end. The only condition here is that we have to do this without using any extra space in memory. For example, if the input array is: const arr = [12, 6, 43, 5, 7, 2, 5]; and number n is 3, then the output should be: const output = [5, 7, 2, 5, ...
Read MoreCalculating excluded average - JavaScript
In JavaScript, you may need to calculate the average of specific values in an array of objects based on certain conditions. This tutorial shows how to compute the average of values where a boolean flag indicates they should be included in the calculation. Problem Description Given an array of objects with val and canUse properties, we need to calculate the average of val values only for objects where canUse is true. const arr = [ {val: 56, canUse: true}, {val: 16, canUse: true}, {val: 45, canUse: true}, ...
Read MoreSplit keys and values into separate objects - JavaScript
In JavaScript, you often need to transform an object into an array of objects where each key-value pair becomes a separate object. This is useful for data processing, API responses, and working with libraries that expect array formats. Problem Statement Suppose we have an object like this: const dataset = { "diamonds": 77, "gold-bars": 28, "exciting-stuff": 52, "oil": 51, "sports-cars": 7, "bitcoins": 40 }; We need to write a JavaScript function that takes this object and returns an array of objects with ...
Read MoreSplitting string into groups – JavaScript
Given a string S, consisting of alphabets, numbers and special characters. We need to write a program to split the strings in three different strings S1, S2 and S3, such that − The string S1 will contain all the alphabets present in S, The string S2 will contain all the numbers present in S, and S3 will contain all special characters present in S. The strings S1, S2 and S3 should have characters in the same order as they appear in input. Syntax const ...
Read MoreChecking whether the sum of digits of a number forms a Palindrome Number or not in JavaScript
We are required to write a JavaScript function that takes in a number, sums its digits and checks whether that sum is a Palindrome number or not. The function should return true if the sum is Palindrome, false otherwise. For example, if the number is 697, then the sum of its digits will be 6 + 9 + 7 = 22, which indeed, is a Palindrome number. Therefore, our function should return true for 697. Understanding the Problem A palindrome number reads the same forwards and backwards. For example: 11, 22, 121, 1331 are palindromes. Our task ...
Read MoreFind the Symmetric difference between two arrays - JavaScript
In Mathematics, the symmetric difference of two sets, say A and B is represented by A △ B. It is defined as the set of all elements which belong either to A or to B but not to both. For example: const A = [1, 2, 3, 4, 5, 6, 7, 8]; const B = [1, 3, 5, 6, 7, 8, 9]; The symmetric difference of A and B will be: const diff = [2, 4, 9] Using For Loops with indexOf() This approach iterates through both arrays and checks ...
Read MoreHow to reverse a portion of an array in JavaScript?
We are required to write a JavaScript function that takes in an array, a start index and an end index. The function should reverse the portion of the array between the start index and end index. For example, if the array is: const arr = [2, 6, 5, 8, 3, 5, 2, 6, 7]; And the start index and end index are 3, 7 respectively, then the array should be reversed to: const output = [2, 6, 5, 2, 5, 3, 8, 6, 7]; Using Array Splice Method This approach ...
Read MoreSorting an associative array in ascending order - JavaScript
In JavaScript, you can sort an array of objects (associative array) in ascending order using the sort() method with a custom comparison function. Suppose we have an array of objects like this: const people = [ {"id":1, "name":"Andrew", "age":30, "gender":"m", "category":"G"}, {"id":2, "name":"Brandon", "age":25, "gender":"m", "category":"G"}, {"id":3, "name":"Christine", "age":20, "gender":"m", "category":"G"}, {"id":4, "name":"Elena", "age":29, "gender":"W", "category":"M"} ]; We need to sort this array by the age property in ascending order. The expected output should be: [ ...
Read More