Let’s say, we have an array that contains some numbers, positive, negative, decimals and integers. We have to write a function that takes in an array and returns an array of square of all the positive integers from the original array.Let’s write the code for this function −Exampleconst arr = [1, -4, 6.1, 0.1, 2.6, 5, -2, 1.9, 6, 8.75, -7, 5]; const squareSum = (arr) => { return arr.reduce((acc, val) => { //first condition checks for positivity and second for wholeness of the number if(val > 0 && val % 1 === 0){ acc += val*val; }; return acc; },0); } console.log(squareSum(arr));OutputThe output in the console will be −87
We have an array of Number / String literals that contain some duplicate values, we have to remove these values from the array without creating a new array or storing the duplicate values anywhere else.We will use the Array.prototype.splice() method to remove entries inplace, and we will take help of Array.prototype.indexOf() and Array.prototype.lastIndexOf() method to determine the duplicacy of any element.Exampleconst arr = [1, 4, 6, 1, 2, 5, 2, 1, 6, 8, 7, 5]; arr.forEach((el, ind, array) => { if(array.indexOf(el) !== array.lastIndexOf(el)){ array.splice(ind, 1); } }); console.log(arr);OutputThe output in the console will be −[ 4, 1, 5, 2, 6, 8, 7 ]
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
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
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 ]
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
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
Of course, writing axes titles help viewers to understand the plot in a better way because they add more information to the plot. In general, the axes titles have simple font but we can change partial or complete title to italics to get the viewers attraction. This is needed when we want to highlight the title by making it different. In ggplot2, we can do this by using expression.ExampleConsider the below data frame −set.seed(1) x
We use head function to take a look at some top values in an R data frame but it shows the top values for the whole data frame without considering the groups of factor column. Therefore, if we have a large number of values in a particular group then head function does not seem to be helpful alone, we must use something to extract the top values for each of the groups. This can be done through using by function with single square brackets and head function.Examplesdata(iris) str(iris) 'data.frame': 150 obs. of 5 variables: $ Sepal.Length: num 5.1 4.9 4.7 ... Read More
Often, we need to subset our data frame and sometimes this subsetting is based on strings. If we have a character column or a factor column then we might be having its values as a string and we can subset the whole data frame by deleting rows that contain a value or part of a value, for example, we can get rid of all rows that contain set or setosa word in Species column.ExampleConsider the below data frame −Character