AmitDiwan has Published 10744 Articles

Fill missing numeric values in a JavaScript array

AmitDiwan

AmitDiwan

Updated on 21-Aug-2020 13:28:23

512 Views

We are given an array of n entries, of which only 2 are Numbers, all other entries are null. Something like this −const arr = [null, null, -1, null, null, null, -3, null, null, null];We are supposed to write a function that takes in this array and complete the arithmetic ... Read More

Returning an array with the minimum and maximum elements JavaScript

AmitDiwan

AmitDiwan

Updated on 21-Aug-2020 13:22:00

144 Views

Provided an array of numbers, let’s say −const arr = [12, 54, 6, 23, 87, 4, 545, 7, 65, 18, 87, 8, 76];We are required to write a function that picks the minimum and maximum element from the array and returns an array of those two numbers with minimum at ... Read More

Reverse digits of an integer in JavaScript without using array or string methods

AmitDiwan

AmitDiwan

Updated on 21-Aug-2020 13:18:25

391 Views

We are required to write Number.prototype.reverse() function that returns the reversed number of the number it is used with.For example −234.reverse() = 432; 6564.reverse() = 4656;Let’s write the code for this function. We will use a recursive approach like this −Exampleconst reverse = function(temp = Math.abs(this), reversed = 0, isNegative ... Read More

Digital root sort algorithm JavaScript

AmitDiwan

AmitDiwan

Updated on 21-Aug-2020 13:15:03

257 Views

Digit root of some positive integer is defined as the sum of all of its digits. We are given an array of integers. We have to sort it in such a way that if a comes before b if the digit root of a is less than or equal to ... Read More

Sort array by year and month JavaScript

AmitDiwan

AmitDiwan

Updated on 20-Aug-2020 06:48:36

3K+ Views

We have an array like this −const arr = [{    year: 2020,    month: 'January' }, {    year: 2017,    month: 'March' }, {    year: 2010,    month: 'January' }, {    year: 2010,    month: 'December' }, {    year: 2020,    month: 'April' }, { ... Read More

Find longest string in array (excluding spaces) JavaScript

AmitDiwan

AmitDiwan

Updated on 20-Aug-2020 06:47:17

188 Views

We are required to write a function that accepts an array of string literals and returns the index of the longest string in the array. While calculating the length of strings we don’t have to consider the length occupied by whitespacesIf two or more strings have the same longest length, ... Read More

How to separate alphabets and numbers from an array using JavaScript

AmitDiwan

AmitDiwan

Updated on 20-Aug-2020 06:45:24

1K+ Views

We have an array that contains some number literals and some string literals mixed, and we are required to write a sorting function that separates the two and the two types should also be sorted within.The code for this sorting function will be −Exampleconst arr = [1, 5, 'fd', 6, ... Read More

Find n highest values in an object JavaScript

AmitDiwan

AmitDiwan

Updated on 20-Aug-2020 06:44:11

1K+ Views

Let’s say, we have an object that describes various qualities of a football player like this −const qualities = {    defence: 82,    attack: 92,    heading: 91,    pace: 96,    dribbling: 88,    tenacity: 97,    vision: 91,    passing: 95,    shooting: 90 };We wish to ... Read More

How to make a function that returns the factorial of each integer in an array JavaScript

AmitDiwan

AmitDiwan

Updated on 20-Aug-2020 06:42:31

700 Views

We are here required to write a JavaScript function that takes in an array of numbers and returns another array with the factorial of corresponding elements of the array. We will first write a recursive method that takes in a number and returns its factorial and then we will iterate ... Read More

Find required sum pair with JavaScript

AmitDiwan

AmitDiwan

Updated on 20-Aug-2020 06:41:20

437 Views

Let’s say, we are required to write a function that takes in an array and a number and returns the index of the first element of the first pair from the array that adds up to the provided number, if there exists no such pair in the array, we have ... Read More

Advertisements