- 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
776 Views
We are required to write a JavaScript function that takes in an array of Numbers as the first argument and a number, say n as the second argument. The number n will always be less than or equal to the length of the array.Our function should return an array of the sum of all elements of all the possible subarrays of length n from the original array.For example, If the input is −const arr = [2, 6, 4]; const n = 2;Then the output should be −const output = [8, 10, 6];ExampleThe code for this will be −const arr = ... Read More
204 Views
We are required to write a JavaScript function that takes in any number of arrays of numbers. Then the function should return an object that returns a frequency map indicating how many times each element has appeared checking in all the array.For example, If the arrays are −const a = [23, 45, 21], b = [45, 23], c = [21, 32], d = [23], e= [32], f=[50, 54];Then the output should be −const output = { "21": 2, "23": 3, "32": 2, "45": 2, "52": 1, "54": 1, "23, 45": 2, "23, ... Read More
724 Views
Suppose we have an array of objects like this −const arr = [ { duration: 10, any: 'fields' }, { duration: 20, any: 'other fields' }, { duration: 15, any: 'some other fields' } ];We are required to write a JavaScript function that takes in one such array and returns the summed result of the duration properties of all the objects.For the above array, the output should be 45.ExampleThe code for this will be −const arr = [ ... Read More
1K+ Views
Suppose, we have an array of names like this −const arr = ["Simon", "Mike", "Jake", "Lara", "Susi", "Blake", "James"];We are required to write a JavaScript function that takes in one such array. The function should return an array of objects with two properties −letter -> the letter on which the names are groupednames -> an array of names that falls in that groupExampleThe code for this will be −const arr = ["Simon", "Mike", "Jake", "Lara", "Susi", "Blake", "James"]; const groupNames = arr => { const map = arr.reduce((acc, val) => { let char = val.charAt(0).toUpperCase(); ... Read More
301 Views
We are required to write a JavaScript function that takes in an array that contains some false values. The function should remove all the null values from the array (if there are any) in place.For example: If the input array is −const arr = [12, 5, undefined, null, 0, false, null, 67, undefined, false, null];Then the output should be −const output = [12, 5, undefined, 0, false, 67, undefined, false];ExampleThe code for this will be −const arr = [12, 5, undefined, null, 0, false, null, 67, undefined, false, null]; const removeNullValues = arr => { for(let i = 0; ... Read More
274 Views
We are required to write a JavaScript function that takes in an array of literals and a number, say n.The function should return a new array, chunked into n subarrays, given that n will always be less than or equal to the length of the array.For example: If the input array is −const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; const n = 3;Then the output should be −const output = [[1, 2, 3], [4, 5, 6], [7, 8, 9, 10]];ExampleThe code for this will be −const arr = [1, 2, 3, 4, 5, 6, ... Read More
1K+ Views
We are required to write a JavaScript function that takes in a string. The function should return a new string that has all the words of the original string reversed.For example, If the string is −const str = 'this is a sample string';Then the output should be −const output = 'siht si a elpmas gnirts';ExampleThe code for this will be −const str = 'this is a sample string'; const reverseWords = str => { let reversed = ''; reversed = str.split(" ") .map(word => { return word .split("") ... Read More
944 Views
We are required to write a JavaScript function that takes in an array of Numbers. The function should check whether the numbers in the array are in increasing order, or in decreasing order or in no specific order.If the array contains only one element then we should return a message saying not enough elements.And if the array has all the elements equal, we should return a message saying all elements are equal.ExampleThe code for this will be −const arr1 = [7, 2, 1, 3, 2, 1]; const arr2 = [1, 1, 2, 3, 7, 7]; const determineOrder = arr => ... Read More
143 Views
We are required to write a JavaScript function that takes in a number and recursively adds the digits of the number until the result is not a single digit number.For example, If the number is −54563Then the output should be 5, because,= 5 + 4 + 5 + 6 + 3 = 23 = 2 + 3 = 5ExampleThe code for this will be −const num = 54563; const addRecursively = num => { if(num < 10){ return num; }; let sum = 0; while(num !== 0) { sum += (num%10); num = parseInt(num/10); }; return addRecursively(sum); }; console.log(addRecursively(num));OutputThe output in the console −3
822 Views
Suppose we have an array of arrays like this −const arr = [ [ ['juice', 'apple'], ['maker', 'motts'], ['price', 12] ], [ ['juice', 'orange'], ['maker', 'sunkist'], ['price', 11] ] ];We are required to write a JavaScript function that takes in one such array and returns a new array of objects built based on the input array.So, for the above array, the output should look like this −const output = [ {juice: 'apple', maker: 'motts', price: 12}, {juice: 'orange', maker: 'sunkist', price: 11} ];ExampleThe code for this will be ... Read More