Found 6684 Articles for Javascript

Greatest number in a dynamically typed array in JavaScript

AmitDiwan
Updated on 24-Oct-2020 12:08:49

89 Views

We are required to write a JavaScript function that takes in an array that contains some numbers, some strings and some false values. Our function should return the biggest Number from the array.For example: If the input array is −const arr = [23, 'hello', undefined, null, 21, 65, NaN, 1, undefined, 'hii'];Then the output should be 65.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [23, 'hello', undefined, null, 21, 65, NaN, 1, undefined, 'hii']; const pickBiggest = arr => {    let max = -Infinity;    for(let i = 0; i ... Read More

Finding middlemost element(s) in JavaScript

AmitDiwan
Updated on 24-Oct-2020 12:07:14

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 ]

Chunking arrays in JavaScript

AmitDiwan
Updated on 24-Oct-2020 12:05:57

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

Remove leading zeros in array in JavaScript

AmitDiwan
Updated on 24-Oct-2020 12:04:32

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 ]

Push specific elements to last in JavaScript

AmitDiwan
Updated on 24-Oct-2020 12:02:58

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

Sort array of objects by string property value in JavaScript

Nikhilesh Aleti
Updated on 22-Sep-2022 12:24:14

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

Finding the balance of brackets in JavaScript

AmitDiwan
Updated on 24-Oct-2020 11:58:53

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

Is element repeated more than n times in JavaScript

AmitDiwan
Updated on 24-Oct-2020 11:56:59

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

Writing table of number in array in JavaScript

AmitDiwan
Updated on 24-Oct-2020 11:55:22

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

Counting smaller and greater in JavaScript

AmitDiwan
Updated on 24-Oct-2020 11:54:09

255 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

Advertisements