Merge JavaScript Objects with Same Key Value and Count Them

AmitDiwan
Updated on 21-Nov-2020 06:13:05

4K+ Views

Suppose, we have an array of objects like this −const arr = [{    "value": 10,    "id": "111",    "name": "BlackCat", }, {    "value": 10,    "id": "111",    "name":    "BlackCat", }, {    "value": 15,    "id": "777",    "name": "WhiteCat", }];We are required to write a JavaScript function that takes in one such array.The function should then merge all those objects together that have the common value for "id" property.Therefore, for the above array, the output should look like −const output = [{    "value": 10,    "id": "111",    "name": "BlackCat",    "count": 2, ... Read More

Check if Second String is a Rotated Version of First String in JavaScript

AmitDiwan
Updated on 21-Nov-2020 06:11:27

160 Views

We are required to write a JavaScript function that takes in two strings, say str1 and str2. We are required to determine whether or not the second string is a rotated version of the first string.For example− If the input strings are −const str1 = 'abcde'; const str2 = 'cdeab';Then the output should be true because str2 is indeed made by shifting 'ab' to the end of string in str1.Exampleconst str1 = 'abcde'; const str2 = 'cdeab'; const isRotated = (str1, str2) => {    if(str1.length !== str2.length){       return false    };    if( (str1.length || str2.length) ... Read More

Make Axes Widths in a Plot Wider than Default in Base R

Nizamuddin Siddiqui
Updated on 21-Nov-2020 06:11:23

128 Views

The axes widths are generally very thin in plots but we can make them wider. This will be useful if we want to highlight the axes labels for reasons such as getting attention of the viewer on axes labels etc. To increase the width of the axes in a base R plot, we can use axis function and set the lwd argument.Example> x hist(x) > axis(side=1,lwd=4)Output:Example> axis(side=2,lwd=4)Output:

Difference Between NA Omit and Complete Cases in R

Nizamuddin Siddiqui
Updated on 21-Nov-2020 06:10:11

876 Views

The na.omit function removes all the missing values in a data frame and complete.cases also does the same thing if applied to the whole data frame. The main difference between the two is that complete.cases can be applied to some columns or rows. Check out the below example to understand the difference.ExampleConsider the below data frame:Live Demo> set.seed(2584) > x y df dfOutput x y 1 NA 25 2 5 5 3 8 NA 4 6 5 5 4 NA 6 4 5 7 6 NA 8 4 NA 9 4 5 10 8 5 11 8 5 ... Read More

Implement Binary Search in JavaScript

AmitDiwan
Updated on 21-Nov-2020 06:09:41

652 Views

We are required to write a JavaScript function that takes in a sorted array of numbers as the first argument and a search number as the second argument.If the search number exists in the array, we need to return its index in the array, otherwise we need to return -1.We have to do this making use of the binary search algorithm. The binary search algorithm is basically a divide and conquer algorithm which recursive divides the array into halves until it converses to a singleton element.The sorting of array is necessary of binary search algorithm in this case, as it ... Read More

Reversing Words Within a String in JavaScript

AmitDiwan
Updated on 21-Nov-2020 06:08:24

403 Views

We are required to write a JavaScript function that takes in a string that might contain spaces. Our function should first split the string based on the spaces and then reverse and join and return the new string.For example − If the input string is −const str = 'this is a word';Then the output should be −const output = 'siht si a drow';Exampleconst str = 'this is a word'; const reverseWords = (str = '') => {    const strArr = str.split(' ');    for(let i = 0; i < strArr.length; i++){       let el = strArr[i];   ... Read More

Extract Names of List Elements in R

Nizamuddin Siddiqui
Updated on 21-Nov-2020 06:07:43

3K+ Views

The names of list elements can be extracted by using the names function. For example, if we have a list defined as List that contains three elements with names element1, element2, and element3 then then these names can be extracted from the List by using the below command:names(List)Example1Live Demo> List1 List1Output$x1 [1] -0.04518909 -0.22779868 0.24339595 -0.86189295 -0.73387277 -0.75313131 [7] 0.39694608 2.30565359 0.55670193 0.21973762 0.62968128 -0.90936921 [13] 1.33946741 -0.16315751 0.31357793 0.40365980 -0.23639612 -2.48749453 [19] 0.52152768 -1.57059863 0.51728464 0.98177111 0.65475629 0.23715538 [25] -0.71796609 -0.42731839 0.32335282 -0.90013122 -0.84549927 -0.88358214 [31] -0.32066379 -0.98945433 0.42469849 -1.63095343 0.32584448 0.10947333 [37] 0.23486625 0.28166351 1.18432843 0.94828212 0.09452671 0.56618262 ... Read More

Is Uppercase Used Correctly in JavaScript?

AmitDiwan
Updated on 21-Nov-2020 06:06:58

67 Views

For the purpose of this very problem, we define the correct use of uppercase letter by the following rules −All letters in a word are capitals, like "INDIA".All letters in a word are not capitals, like "example".Only the first letter in a word is capital, like "Ramesh".We are required to write a JavaScript function that takes in a string determines whether or not the string complies with any of these three rules.If it does then we return true, false otherwise.Exampleconst detectCapitalUse = (word = '') => {    let allCap = true;    for (let i = 0; i < ... Read More

Create Heatmap for Lower Triangular Matrix in R

Nizamuddin Siddiqui
Updated on 21-Nov-2020 06:05:37

912 Views

A heatmap is a diagrammatic representation of data where the values are represented with colours. Mostly, it is used to display data that has slight variation. We can draw it for a full matrix, an upper triangular matrix as well as a lower triangular matrix. This can be done with the help of image function.Example1Live Demo> M1 M1Output   [, 1] [, 2] [, 3] [, 4] [, 5] [, 6] [1, ] 6    9    4    7    5    4 [2, ] 6    6    4    3    7    5 [3, ] 2   ... Read More

Sum Only Numbers in an Array using JavaScript

AmitDiwan
Updated on 21-Nov-2020 06:04:59

393 Views

We are required to write a JavaScript function that takes in an array. The array might contain any type of value, Number literals, string literals, objects, undefined.Our function should pick all the Number type values and return their sumExampleconst arr = [1, 2, 'a', 4]; const countNumbers = (arr = []) => {    let sum = 0;    for(let i = 0; i < arr.length; i++){       const el = arr[i]; if(+el){          sum += +el;       };    };    return sum; } console.log(countNumbers(arr));OutputAnd the output in the console will be −7

Advertisements