- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 6686 Articles for Javascript
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
122 Views
Suppose, we array of arrays like this −const arr = [ [-73.9280684530257, 40.8099975343718], [-73.9282820374729, 40.8100875554645], [-73.9280124002104, 40.8103130893677], [-73.927875543761, 40.8102554080229], [-73.9280684530257, 40.8099975343718] ];Here each subarray represents a point on a 2-D plane, and each point is a vertex of n sided polygon where n is the number of subarrays in the input arrays.We are required to write a JavaScript function that takes in one such array and returns a new array of n subarrays, each representing the midpoint of corresponding sides of the polygon.ExampleThe code for this will be −const arr = [ [-73.9280684530257, 40.8099975343718], [-73.9282820374729, 40.8100875554645], [-73.9280124002104, 40.8103130893677], [-73.927875543761, 40.8102554080229], [-73.9280684530257, 40.8099975343718] ]; const findCenters = arr => { const centerArray = []; for(i = 0; i
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
302 Views
Suppose we have two arrays, the first array contains the scheduled date for some events and the second array contains the names of those events, like this −const dates = [ { id:"1", date:"2017-11-07" }, { id:"1", date:"2017-11-08" }, { id:"2", date:"2017-11-07" }, { id:"2", date:"2017-11-08" } ]; const names = [ { id:"1", name:"Pervies, Peter" }, { ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
351 Views
We are required to write a JavaScript function that takes in an array of literals and returns the count of elements that appears for the most number of times in the array.ExampleThe code for this will be −let arr = [2, 8, 4, 8, 6, 4, 7, 8]; const countOccurence = arr => { const max = arr.reduce((acc, val) => { return Math.max(acc, val); }, -Infinity); const count = arr.filter(el => { return el === max; }); const { length } = count; return length; }; console.log(countOccurence(arr));OutputThe output in the console −3
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
191 Views
Suppose we have an array of arrays like this −const arr = [ [ "Serta", "Black Friday" ], [ "Serta", "Black Friday" ], [ "Simmons", "Black Friday" ], [ "Simmons", "Black Friday" ], [ "Simmons", "Black Friday" ], [ "Simmons", "Black Friday" ] ];We are required to write a JavaScript function that ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
260 Views
Suppose we have two arrays of literals like these −const arr1 = [1, 3, 2, 4, 5, 6]; const arr2 = [1, 2, 5];We are required to write a JavaScript function that takes in two such arrays. Then our function should return a new array that contains all the elements of arr1 but sorted according to arr2.Like the elements that appear in both the array should appear first according to their order in the second array followed by the elements only present in first array retaining their order.ExampleThe code for this will be −const arr1 = [1, 3, 2, 4, ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
219 Views
Suppose, we have an array of objects like this −const arr = [ {"title": "Assistant"}, {"month": "July"}, {"event": "Holiday"}, {"title": "Assistant"} ];We are required to write a JavaScript function that takes in one such array. Our function should then return a new array that contains all the object from the original array but the duplicate ones.ExampleThe code for this will be −const arr = [ {"title": "Assistant"}, {"month": "July"}, {"event": "Holiday"}, {"title": "Assistant"} ]; const removeDuplicate = arr => { const map = {}; for(let i = 0; i < ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
169 Views
Suppose we have an array of literals like this −const arr = ['cat', 'dog', 'elephant', 'lion', 'tiger', 'mouse'];We are required to write a JavaScript function that takes in one such array as the first argument and then any number of strings as second and third and many more arguments.Then our function should remove all the strings from the array taken as first argument in place if that string is provided as argument to the function.ExampleThe code for this will be −const arr = ['cat', 'dog', 'elephant', 'lion', 'tiger', 'mouse']; const removeFromArray = (arr, ...removeArr) => { removeArr.forEach(item => { ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
937 Views
We are required to write a JavaScript function that takes in two. Our function should then return a new array that contains characters alternatively from both the strings.For example: If the two strings are −const str1 = 'abc'; const str2 = 'def';OutputThen the output should be −const output = 'adbecf';ExampleThe code for this will be −const str1 = 'abc'; const str2 = 'def'; const mergeAlternatively = (str1, str2) => { const a = str1.split("").filter(el => !!el); const b = str2.split(""); let mergedString = ''; for(var i = 0; i < a.length || i < b.length; i++){ ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
1K+ Views
We are required to write a JavaScript function that takes in a two-dimensional array of literals.Our function should return a new array that contains all the entries from the original array but the repeating ones.ExampleThe code for this will be −const arr = [ [1,2,3,4,5], [3,4,6,7,8,2], [7,2,4,9,11,15], [10,12,3,7,11] ]; const removeDuplicates = arr => { let map = {}; let res = []; res = arr.map(el => { return el.filter(val => { if(map[val]){ return false; }; map[val] = 1; return true; }); }); return res; }; console.log(removeDuplicates(arr));OutputThe output in the console −[ [ 1, 2, 3, 4, 5 ], [ 6, 7, 8 ], [ 9, 11, 15 ], [ 10, 12 ] ]