Found 9309 Articles for Object Oriented Programming

Sum from array values with similar key in JavaScript

AmitDiwan
Updated on 21-Aug-2020 13:34:47

427 Views

Let’s say, here is an array that contains some data about the stocks sold and purchased by some company over a period of time.const transactions = [    ['AAPL', 'buy', 100],    ['WMT', 'sell', 75],    ['MCD', 'buy', 125],    ['GOOG', 'sell', 10],    ['AAPL', 'buy', 100],    ['AAPL', 'sell', 100],    ['AAPL', 'sell', 20],    ['DIS', 'buy', 15],    ['MCD', 'buy', 10],    ['WMT', 'buy', 50],    ['MCD', 'sell', 90] ];We want to write a function that takes in this data and returns an object of arrays with key as stock name (like ‘AAPL’, ‘MCD’) and value as array ... Read More

Fill missing numeric values in a JavaScript array

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

380 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 series of which these two numbers are a part. To understand this problem more clearly, we can think of these null values as blank space where we need to fill numbers so that the whole array forms an arithmetic progression.About Arithmetic ProgressionA series / array of numbers is said to ... Read More

Returning an array with the minimum and maximum elements JavaScript

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

103 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 index 0 and maximum at 1.We will use the Array.prototype.reduce() method to build a minimum maximum array like this −Exampleconst arr = [12, 54, 6, 23, 87, 4, 545, 7, 65, 18, 87, 8, 76]; const minMax = (arr) => {    return arr.reduce((acc, val) => {       if(val < acc[0]){          acc[0] = val;       }       if(val > acc[1]){          acc[1] = val;       }       return acc;    }, [Infinity, -Infinity]); }; console.log(minMax(arr));OutputThe output in the console will be −[ 4, 545 ]

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

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

294 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 = this < 0){    if(temp){       return reverse(Math.floor(temp/10), (reversed*10)+temp%10,isNegative);    };    return !isNegative ? reversed : reversed*-1; }; Number.prototype.reverse = reverse; const n = -12763; const num = 43435; console.log(num.reverse()); console.log(n.reverse());OutputThe output in the console will be −53434 -36721

Digital root sort algorithm JavaScript

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

172 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 the digit root of b. If two numbers have the same digit root, the smaller one (in the regular sense) should come first. For example, 4 and 13 have the same digit root, however 4 < 13 thus 4 comes before 13 in any digitRoot sorting where both are present.For ... Read More

Find the last element of a list in scala

Arnab Chakraborty
Updated on 20-Aug-2020 07:37:29

574 Views

Suppose we have a list in Scala, this list is defined under scala.collection.immutable package. As we know, a list is a collection of same type elements which contains immutable (cannot be changed) data. We generally apply last function to show last element of a list.Using last keywordThe following Scala code is showing how to print the last element stored in a list of Scala.Example import scala.collection.immutable._ object HelloWorld {    def main(args: Array[String]) {       val temp_list: List[String] = List("Hello", "World", "SCALA", "is", "awesome")       println("Elements of temp_list: " + temp_list.last)    } }Output$scala HelloWorld Elements of ... Read More

Sort array by year and month JavaScript

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' }, {    year: 2017,    month: 'August' }, {    year: 2010,    month: 'February' }, {    year: 2020,    month: 'October' }, {    year: 2017,    month: 'June' }]We have to sort this array according to years in ascending order (increasing order). Moreover, if there exist two objects ... Read More

Find longest string in array (excluding spaces) JavaScript

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

141 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, we have to return the index of the first string that does so.We will iterate over the array, split each element by whitespace, join again and calculate the length, after this we will store this in an object. And when we encounter length greater than currently stored in the object, ... Read More

How to separate alphabets and numbers from an array using JavaScript

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

906 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, 'as', 'a', 'cx', 43, 's', 51, 7]; const sorter = (a, b) => {    const first = typeof a === 'number';    const second = typeof b === 'number';    if(first && second){       return a - b;    }else if(first && !second){       return ... Read More

Find n highest values in an object JavaScript

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 write a function that takes in such object and a number n (n {    const requiredObj = {};    if(num > Object.keys(obj).length){       return false;    };    Object.keys(obj).sort((a, b) => obj[b] - obj[a]).forEach((key, ind) =>    {       if(ind < num){          requiredObj[key] = obj[key];       }    });    return requiredObj; }; console.log(pickHighest(qualities, 3));OutputThe output in the console will be −{ tenacity: 97, pace: 96, passing: 95 } { tenacity: 97 } { tenacity: 97, pace: 96, passing: 95, attack: 92, heading: 91 }

Advertisements