Found 9150 Articles for Object Oriented Programming

Find the last element of a list in scala

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

824 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

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, 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

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, '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 }

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

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 over the array, calculating the factorial of each element of array and then finally we will return the new array of factorials.Therefore, let’s write the code for thisExampleconst arr = [4, 8, 2, 7, 6, 20, 11, 17, 12, 9]; const factorial = (num, fact = 1) => {   ... Read More

Find required sum pair with JavaScript

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 to return -1.By pair, we mean, two consecutive elements of the array and not any two arbitrary elements of the array. So, let’s write the code for this function −Exampleconst arr = [4, 8, 2, 7, 6, 42, 41, 77, 32, 9]; const findPair = (arr, num) => {   ... Read More

Map an integer from decimal base to hexadecimal with custom mapping JavaScript

AmitDiwan
Updated on 20-Aug-2020 06:40:29

384 Views

Usually when we convert a decimal to hexadecimal (base 16) we use the set 0123456789ABCDEF to map the number.We are required to write a function that does exactly the same but provides user the freedom to use any scale rather than the one mentioned above.For example −The hexadecimal notation of the decimal 363 is 16B But if the user decides to use, say, a scale ‘qwertyuiopasdfgh’ instead of ‘0123456789ABCDEF’, the number 363, then will be represented by wusThat’s what we are required to do.So, let’s do this by making a function toHex() that makes use of recursion to build a ... Read More

How to reduce an array while merging one of its field as well in JavaScript

AmitDiwan
Updated on 20-Aug-2020 06:39:58

498 Views

Consider, we have the following array of objects −const arr = [{    id: 121,    hobby: 'cycling' }, {    id: 125,    hobby: 'jogging' }, {    id: 129,    hobby: 'reading' }, {    id: 121,    hobby: 'writing' }, {    id: 121,    hobby: 'playing football' }, {    id: 125,    hobby: 'cooking' }, {    id: 129,    hobby: 'horse riding' }];Let’s say, we have to write a function that takes in such an array and merges it based on the common id property, and for the hobby property we assign it an ... Read More

Determining happy numbers using recursion JavaScript

AmitDiwan
Updated on 20-Aug-2020 06:37:52

773 Views

Happy NumberA happy number is a number which eventually reaches 1 when replaced by the sum of the square of each digit. Whereas if during this process any number gets repeated, the cycle will run infinitely and such numbers are called unhappy numbers.For example − 13 is a happy number because, 1^2 + 3^2 = 10 and, 1^2 + 0^2 = 1On the other hand, 36 is an unhappy number.We are required to write a function that uses recursion to determine whether or not a number is a happy number.So, let’s write this function out. The key to this function ... Read More

Advertisements