
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 6710 Articles for Javascript

777 Views
Let’s say. we have an array and an object like this −const arr = ['a', 'd', 'f']; const obj = { "a": 5, "b": 8, "c": 4, "d": 1, "e": 9, "f": 2, "g": 7 };We are required to write a function that takes in the object and the array and filter away all the object properties that are not an element of the array. So, the output should only contain 3 properties, namely: “a”, “d” and “e”.Let’s write the code for this function −Exampleconst arr = ['a', 'd', 'f']; const obj = ... Read More

3K+ Views
We are required to write a sorting function that sort an array based on the contents of another array.For example − We have to sort the original array such that the elements present in the below sortOrder array appear right at the start of original array and all other should keep their order −const originalArray = ['Apple', 'Cat', 'Fan', 'Goat', 'Van', 'Zebra']; const sortOrder = ['Zebra', 'Van'];Exampleconst originalArray = ['Apple', 'Cat', 'Fan', 'Goat', 'Van', 'Zebra']; const sortOrder = ['Zebra', 'Van']; const sorter = (a, b) => { if(sortOrder.includes(a)){ return -1; }; if(sortOrder.includes(b)){ ... Read More

698 Views
We are required to write a function that returns the index of the very first element that appears at least twice in the array. If no element appears more than once, we have to return -1. The condition is that we have to do this in constant space (i.e., without utilizing extra memory).Let’s devise the solution for this problem. We will use a for loop to iterate over the array and use the Array.prototype.lastIndexOf() method to check for duplicates.Exampleconst firstDuplicate = arr => { for(let i = 0; i < arr.length; i++){ if(arr.lastIndexOf(arr[i]) !== i){ ... Read More

2K+ Views
We are required to write a function that takes in a one-dimensional array as the first argument and a number n as the second argument and we have to make n subarrays inside of the parent array (**if possible) and divide elements into them accordingly.** if the array contains 9 elements and we asked to make 4 subarrays, then dividing 2 elements in each subarray creates 5 subarrays and 3 in each creates 3, so in such cases we have to fallback to nearest lowest level (3 in this case) because our requirement is to distribute equal number of elements ... Read More

582 Views
There are times when you need to merge multiple Boolean arrays into a single array in JavaScript. One common approach is to combine the corresponding elements from each array using the OR (||) operator. This operation returns true if at least one of the elements being compared is true; otherwise, it returns false. In this article, we'll learn how to merge multiple Boolean arrays into a single array using JavaScript, specifically by applying the OR operator on corresponding elements. We will utilize the Array.prototype.reduce() function to efficiently perform this task. Problem Definition We are given an array of arrays of ... Read More

1K+ Views
To push value in an empty index in an array, we must write an array function pushAtEmpty() that takes in an element and pushes it at the first empty index it finds in the array it is used in the context of. If there are no empty spaces, the element should be pushed to the end of the array. The arrays are data structures in JavaScript that are used to store values of the same type in a linear form. Arrays are basic data structures that every developer works on. While working with arrays, they commonly work on pushing elements ... Read More

2K+ Views
We are required to write a function, say isPowerOfTwo() that takes in a positive number and returns a boolean based on the fact whether or not the number is some power of 2.For example −console.log(isPowerOfTwo(3)); //false console.log(isPowerOfTwo(32)); //true console.log(isPowerOfTwo(2048)); //true console.log(isPowerOfTwo(256)); //true console.log(isPowerOfTwo(22)); //falseLet’s write the code for this function, it will be a very straightforward recursive function that keeps recurring until the number stays divisible by 2, if in this process the number gets reduced all the way down to 1, it is a power of 2 otherwise it isn’t. Here is the code −Exampleconst isPowerOfTwo = num => ... Read More

563 Views
Let’s say, we are required to write a function classifyArray() that takes in an array which contains mixed data types and returns a Map() with the elements grouped by their data types.For example −// if the input array is: const arr = ['class', 2, [7, 8, 9], {"name": "Michael"}, Symbol('foo'), true, false, 'name', 6]; // then the output Map should be: Map(5) { 'string' => [ 'class', 'name' ], 'number' => [ 2, 6 ], 'object' => [ [ 7, 8, 9 ], { name: 'Michael' } ], 'symbol' => [ Symbol(foo) ], 'boolean' => ... Read More

391 Views
We are required to write a function that does the following transformation −If the input object is −const input = { a: 0, b: {x: {y: 1, z: 2}}, c: 3 };Then the output of the function should be −const output = { a: 0, 'b.x.y': 1, 'b.x.z': 2, c: 3 }We basically have to write a function that flattens a nested object. Will do this via recursion and the code for doing this will be −Exampleconst obj = { a: 1, b: {x: {y: 1, z: 2}}, c: 3 ... Read More

210 Views
Let’s say, we are required to write a function, say parseEqualInterval() that takes in an array of Numbers of strictly two elements as the first argument and a number n as the second argument and it inserts n-1 equidistant entries between the actual two elements of the original array so that it gets divided into n equal intervals.For example −// if the input array is const arr = [12, 48]; // and the interval is 4 //then the output array should be: const output = [12, 21, 30, 39, 48];This way the array got divided into 4 equal intervals. So, ... Read More