
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

877 Views
Suppose, we have a comma-separated string like this −const str = "a, b, c, d , e";We are required to write a JavaScript function that takes in one such and sheds it off all the whitespaces it contains.Then our function should split the string to form an array of literals and return that array.ExampleThe code for this will be −const str = "a, b, c, d , e"; const shedAndSplit = (str = '') => { const removeSpaces = () => { let res = ''; for(let i = 0; i < str.length; ... Read More

241 Views
We are required to write a JavaScript function that takes in an array of literal values. The array might contain some repeating values inside it.Our function should remove all the repeating values keeping the first instance of repeating value in the array.ExampleThe code for this will be −const arr = [1, 5, 7, 4, 1, 4, 4, 6, 4, 5, 8, 8]; const deleteDuplicate = (arr = []) => { for(let i = 0; i < arr.length; ){ const el = arr[i]; if(i !== arr.lastIndexOf(el)){ arr.splice(i, 1); } else{ i++; }; }; }; deleteDuplicate(arr); console.log(arr);OutputAnd the output in the console will be −[ 7, 1, 6, 4, 5, 8 ]

5K+ Views
Suppose we have two arrays of objects, the first of which contains some objects with user ids and user names.The array contains objects with user ids and user addresses.The array is −const arr1 = [ {"id":"123", "name":"name 1"}, {"id":"456", "name":"name 2"} ]; const arr2 = [ {"id":"123", "address":"address 1"}, {"id":"456", "address":"address 2"} ];We are required to write a JavaScript function that takes in two such arrays and merges these two arrays to form a third array.The third array should contain the user id, name, and address object of corresponding users.ExampleThe code for this will be −const ... Read More

560 Views
We are required to write a JavaScript function that takes in an array of literal values. Our function should pick all those values from the array that appears exactly twice in the array and return a new array of those elements.ExampleThe code for this will be −const arr = [0, 1, 2, 2, 3, 3, 5]; const findAppearances = (arr, num) => { let count = 0; for(let i = 0; i < arr.length; i++){ const el = arr[i]; if(num !== el){ continue; }; ... Read More

527 Views
Suppose, we have two different array of objects that contains information about the questions answered by some people −const arr1=[ { PersonalID: '11', qusetionNumber: '1', value: 'Something' }, { PersonalID: '12', qusetionNumber: '2', value: 'whatever' }, { PersonalID: '13', qusetionNumber: '3', value: 'anything' }, { PersonalID: '14', qusetionNumber: '4', value: 'null' } ]; const arr2=[ { qusetionNumber: '2', chID: '111', cValue: 'red' }, { qusetionNumber: '2', chID: '112', cValue: 'green'}, { qusetionNumber: '2', chID: '113', cValue: 'blue' }, {qusetionNumber: '3', choiceID: '114', cValue: 'yellow'}, {qusetionNumber: '4', choiceID: '115', cValue: 'red'} ];We ... Read More

405 Views
We are required to write a JavaScript function that takes in a multidimensional array of arrays of literal values. Our function should return the intersecting array of all the subarrays present in the input array.ExampleThe code for this will be −const arr = [ ["garden","canons","philips","universal"], ["universal","ola","uber","bangalore"] ]; const findMultiIntersection = (arr = []) => { const res = []; arr.forEach(el => { const thisObj = this; el.forEach(element => { if(!thisObj[element]){ thisObj[element] = true; } else{ res.push(element) }; }); }, {}); return res; }; console.log(findMultiIntersection(arr));OutputAnd the output in the console will be −[ 'universal' ]

471 Views
Suppose we have two arrays of literals like these −const arr1= ['a', 'b', 'c']; const arr2= ['d', 'e', 'f'];We are required to write a JavaScript function that takes in two such arrays and build all possible combinations from the arrays.So, for these two arrays, the output should look like −const output = [ad, ae, af, bd, be, bf, cd, ce, cf];ExampleThe code for this will be −const arr1= ['a', 'b', 'c']; const arr2= ['d', 'e', 'f']; const combineArrays = (...arr) => { const res = []; const combinePart = (part, index) => { arr[index].forEach(el => ... Read More

715 Views
Suppose, we have an array that contains dates in MM-YYYY format like this −const arr = ["1-2016", "7-2015", "7-2016", "3-2016", "8-2016", "2-2016", "6-2016", "8-2015", "5-2016", "4-2016", "9-2015", "10-2015", "11-2015", "12-2015"];We are required to write a JavaScript function that takes in one such array and sorts it such that the dates in the array are arranged in oldest to newest order.ExampleThe code for this will be −const arr = ["1-2016", "7-2015", "7-2016", "3-2016", "8-2016", "2-2016", "6-2016", "8-2015", "5-2016", "4-2016", "9-2015", "10-2015", "11-2015", "12-2015"]; const padToString = (num) => { return String("0" + num).slice(-2); }; const sortByDate = (first, second) => ... Read More

271 Views
We are required to write a JavaScript function that takes in an array of literals and finds the most frequent number in the array and how many times it is repeated.ExampleThe code for this will be −const arr = ['13', '4', '1', '1', '4', '2', '3', '4', '4', '1', '2', '4', '9', '3']; const findFrequency = (arr = []) => { const count = {}; const max = arr.reduce((acc, val, ind) => { count[val] = (count[val] || 0) + 1; if (!ind || count[val] > count[acc[0]]) { return ... Read More

106 Views
We are required to write a JavaScript function that determines how many different ways we can remove a group of values from a sequence, leaving the original sequence in order (stable), and making sure to remove only one instance value each from the original sequence.For example − If the sequence array is −const arr = [1, 2, 1, 3, 1, 4, 4];And the array to be removed is −const arr2 = [1, 4, 4];Then there are three possible ways of doing this without disrupting the order of elements −1 --> [2, 1, 3, 1] 2 --> [1, 2, 3, 1] ... Read More