- 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 6683 Articles for Javascript
63 Views
We are required to write a JavaScript function that takes in an array of Numbers. The function should return the middlemost element of the array.For example: If the array is −const arr = [1, 2, 3, 4, 5, 6, 7];Then the output should be 4.ExampleThe code for this will be −const arr = [1, 2, 3, 4, 5, 6, 7]; const middle = function(){ const half = this.length >> 1; const offset = 1 - this.length % 2; return this.slice(half - offset, half + 1); }; Array.prototype.middle = middle; console.log(arr.middle()); console.log([1, 2, 3, 4, 5, 6].middle());OutputThe output in the console will be −[ 4 ] [ 3, 4 ]
119 Views
We are required to write a JavaScript function that takes in an array of literals and returns a new array that have elements of the original array chunked into subarrays of length exactly 2. Now if the length of original array is not exactly divisible by 2, then the last subarray should have only one element.For example: If the input array is −const arr = [1, 2, 3, 4, 5, 6, 7];Then the output should be −const output = [[1, 2], [3, 4], [5, 6], [7]]Therefore, let’s write the code for this function −ExampleThe code for this will be −const ... Read More
310 Views
The requirements for this question are simple. We are required to write a JavaScript function that takes in an array of Numbers.If the array contains leading zero, the function should remove the leading zeros in place, otherwise the function should do nothing.For example: If the input array is −const arr = [0, 0, 0, 14, 0, 63, 0];Then the output should be −const output = [14, 0, 63, 0];Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [0, 0, 0, 14, 0, 63, 0]; const removeLeadingZero = arr => { while (arr.indexOf(0) === 0) { arr.shift(); }; }; removeLeadingZero(arr); console.log(arr);OutputThe output in the console will be −[ 14, 0, 63, 0 ]
84 Views
Suppose we have an array of objects like this −const arr = [ {flag: true, other: 1}, {flag: true, other: 2}, {flag: false, other: 3}, {flag: true, other: 4}, {flag: true, other: 5}, {flag: true, other: 6}, {flag: false, other: 7} ];We are required to write a JavaScript function that takes in one such array and sorts it based on the following conditions −If arr.flag === false, the matching element gets placed first in the array, but only after the previous matching elements.The elements that do not match, remain in the same order ... Read More
3K+ Views
The given task is to sort an array of objects by using string property value in JavaScript. Assume there is an array of objects and we need to sort those elements by using the string property. In this below scenario, we are sorting array of objects by "company" property. const array = [ {Company: 'Oneplus', Manufacturing: 'China'}, {Company: 'Samsung', Manufacturing: 'South korea'}, {Company: 'Nothing', Manufacturing: 'India'} ]; Output = // it sorted the array of objects by "company" property. [ {"Company":"Nothing", "Manufacturing":"India"}, {"Company":"Oneplus", "Manufacturing":"China"}, ... Read More
275 Views
Given a string that consists of only two types of characters: "(" and ")". We are required to write a function that takes in one such string and balances the parentheses by inserting either a "(" or a ")" as many times as necessary. The function should then return the minimum number of insertions made in the string to balance it.For example: If the string is −const str = '()))';Then the output should be 2, because by prepending '((', we can balance the string.ExampleThe code for this will be −const str = '()))'; const balanceParanthesis = str => { ... Read More
105 Views
We are required to write a JavaScript function that takes in two arguments −An Array, say arr, of literals that may contain some repeating elements.A number, say limit.The function should validate that no element of the array is repeated more than limit number of times. If any element is repeated more than the limit the function should return false, true otherwise.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [4, 6, 7, 4, 2, 5, 7, 7, 4, 4, 3]; const validateElements = (arr, n) => { const counts = arr.reduce((acc, ... Read More
110 Views
We are required to write a JavaScript function that takes in two numbers, say m and n, and it returns an array of first n multiples of m.Therefore, let’s write the code for this function −ExampleThe code for this will be −const num1 = 4; const num2 = 6; const multiples = (num1, num2) => { const res = []; for(let i = num1; i
254 Views
Suppose, we have an array of literals like this −const arr = [3, 5, 5, 2, 23, 4, 7, 8, 8, 9];We are required to write a JavaScript function that takes in this array and a number, say n, and returns an object representing the count of elements greater than and smaller than n.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [3, 5, 5, 2, 23, 4, 7, 8, 8, 9]; const smallerLargerNumbers = (arr, num) => { return arr.reduce((acc, val) => { let { greater, smaller ... Read More
2K+ Views
We are required to write a JavaScript function that counts all unique items in an array. The function should return an object representing the count of each unique element of the array.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = ["hi", "hello", "hi"]; const countUnique = arr => { const counts = {}; for (var i = 0; i < arr.length; i++) { counts[arr[i]] = 1 + (counts[arr[i]] || 0); }; return counts; }; console.log(countUnique(arr));OutputThe output in the console will be −{ hi: 2, hello: 1 }