Found 6686 Articles for Javascript

How to validate if an element in an array is repeated? - JavaScript

AmitDiwan
Updated on 01-Oct-2020 10:36:18

237 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.ExampleFollowing is the code −const arr = [4, 6, 7, 4, 2, 5, 7, 7, 4, 4, 3]; const validateElements = (arr, n) => {    const counts = arr.reduce((acc, el) => {       acc[el] = (acc[el] + ... Read More

Calculate Subtraction of diagonals-summations in a two-dimensional matrix using JavaScript

AmitDiwan
Updated on 01-Oct-2020 10:34:52

192 Views

Suppose, we have a square matrix represented by a 2-D array in JavaScript like this −const arr = [    [1, 3, 5],    [3, 5, 7],    [2, 4, 2] ];We are required to write a JavaScript function that takes in one such array.The function should return the difference between the sum of elements present at the diagonals of the matrix.Like for the above matrix, the calculations will be −|(1+5+2) - (5+5+2)| |8 - 12| 4ExampleFollowing is the code −const arr = [    [1, 3, 5],    [3, 5, 7],    [2, 4, 2] ]; const diagonalDiff = ... Read More

Constructing multiples array - JavaScript

AmitDiwan
Updated on 01-Oct-2020 10:33:36

114 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.For example −If the numbers are 4 and 6Then the output should be −const output = [4, 8, 12, 16, 20, 24]ExampleFollowing is the code −const num1 = 4; const num2 = 6; const multiples = (num1, num2) => {    const res = [];    for(let i = num1; i

Tidy numbers in JavaScript

AmitDiwan
Updated on 01-Oct-2020 10:32:33

187 Views

A tidy number is a number whose digits are in non-decreasing order.For example −489 is a tidy number 234557 is also a tidy number 34535 is not a tidy numberWe are required to write a JavaScript function that takes in a number and checks whether its a tidy number or not.ExampleFollowing is the code −const num = 234789; const isTidy = (num, last = 10) => {    if(num){       if(num % 10 > last){          return false;       };       return isTidy(Math.floor(num / 10), (num % 10));    };    return true; }; console.log(isTidy(num));OutputThis will produce the following output on console −true

Frequency of smaller and larger elements - JavaScript

AmitDiwan
Updated on 01-Oct-2020 10:31:37

93 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.ExampleFollowing is the code −const arr = [3, 5, 5, 2, 23, 4, 7, 8, 8, 9]; const smallerLargerNumbers = (arr, num) => {    return arr.reduce((acc, val) => {       let { greater, smaller } = acc;       if(val > num){          greater++;       };       if(val < num){          smaller++;       };       return { greater, smaller };    }, {       greater: 0,       smaller: 0    }); }; console.log(smallerLargerNumbers(arr, 3));OutputThis will produce the following output on console −{ greater: 8, smaller: 1 }

JavaScript function that should count all unique items in an array

AmitDiwan
Updated on 01-Oct-2020 10:30:33

162 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.Let’s say the following is our array −const arr = ["hi", "hello", "hi"];ExampleFollowing is the code −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));OutputThis will produce the following output on console −{ hi: 2, hello: 1 }

Find closest index of array in JavaScript

AmitDiwan
Updated on 01-Oct-2020 10:29:35

1K+ Views

Suppose, we have an array like this −const arr = [2, 42, 82, 122, 162, 202, 242, 282, 322, 362];We are required to write a JavaScript function that takes in one such array and a number, say n.The function should return the index of item from the array which is closest to the number n.ExampleFollowing is the code −const arr = [2, 42, 82, 122, 162, 202, 242, 282, 322, 362]; const closestIndex = (num, arr) => {    let curr = arr[0], diff = Math.abs(num - curr);    let index = 0;    for (let val = 0; val ... Read More

Repeat even number inside the same array - JavaScript

AmitDiwan
Updated on 01-Oct-2020 10:28:25

63 Views

We are required to write a JavaScript function that should repeat the even number inside the same array.Therefore, for example given the following array −const arr = [1, 2, 5, 6, 8];We should get the output −const output = [1, 2, 2, 5, 6, 6, 8, 8];ExampleFollowing is the code −const arr = [1, 2, 5, 6, 8]; const repeatEvenNumbers = arr => {    let end = arr.length -1;    for(let i = end; i > 0; i--){       if(arr[i] % 2 === 0){          arr.splice(i, 0, arr[i]);       };    };    return arr; }; console.log(repeatEvenNumbers(arr));OutputThis will produce the following output on console −[     1, 2, 2, 5,     6, 6, 8, 8 ]

Finding Gapful number in JavaScript

AmitDiwan
Updated on 01-Oct-2020 10:27:10

270 Views

A number is gapful if it is at least 3 digits long and is divisible by the number formed by stringing the first and last numbers together. The smallest number that fits this description is 100. First digit is 1, last digit is 0, forming 10, which is a factor of 100. Therefore, 100 is gapful.We are required to create a function that takes a number n and returns the closest gapful number (including itself). If there are 2 gapful numbers that are equidistant to n, return the lower one.Some examples −gapful(25) ➞ 100 gapful(100) ➞ 100 gapful(103) ... Read More

JavaScript: Check if array has an almost increasing sequence

AmitDiwan
Updated on 01-Oct-2020 10:25:39

331 Views

Given a sequence of integers as an array, determine whether it is possible to obtain a strictly increasing sequence by removing no more than one element from the array.The sequence a0, a1, ..., an is considered to be a strictly increasing if a0 < a1 < ... < an. Sequence containing only one element is also considered to be strictly increasing.ExampleFor sequence = [1, 3, 2, 1], the output should be −almostIncreasingSequence(sequence) = false.There is no one element in this array that can be removed in order to get a strictly increasing sequence.For sequence = [1, 3, 2], the output ... Read More

Advertisements