Reversing Words Within a String in JavaScript

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

424 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

93 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

961 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

413 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

Sort in Multi-Dimensional Arrays in JavaScript

AmitDiwan
Updated on 21-Nov-2020 06:03:57

775 Views

Suppose, we have the following array of arrays −const arr = [ ["A", "F", "A", "H", "F", "F"],  ["F", "A", "A", "F", "F", "H"] ];We are required to write a JavaScript function that takes in one such array.The function should sort all the subarrays of the given array internally according to these rules −If the elements are not either "A" or "F", they should maintain their positionIf the element is either of "A" or "F", they should be sorted alphabeticallyTherefore, the final output for the above array should look like −const output = [ ["A", "A", "A", "H", "A", "F"], ... Read More

Finding Perfect Numbers in JavaScript

AmitDiwan
Updated on 21-Nov-2020 06:02:18

2K+ Views

A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself. A divisor of an integer x is an integer that can divide x evenly.For example −28 is a perfect number, because 28 = 1 + 2 + 4 + 7 + 14We are required to write a JavaScript function that takes in a number, say n, and determines whether or not n is a perfect number.Exampleconst num = 28; const checkPerfectNumber = (num = 1) => {    if(num === 1) {       return false;    }; ... Read More

Difference Between & and | in R

Nizamuddin Siddiqui
Updated on 21-Nov-2020 06:01:56

460 Views

If we have a data frame defined as df that contains column x, y, and z then extraction of these columns from df can be done by using df$x, df$y, and df$z. On the other hand, if we have an S4 object defined as Data_S4 that contains column x, y, and z then the extraction of these columns can be done by using Data_S4@x, Data_S4@y, and Data_S4@z.Example of a data frame:ExampleLive Demo> x1 x2 df dfOutput x1 x2 1 4 2 2 7 0 3 10 2 4 3 1 5 7 1 6 2 2 7 3 4 8 ... Read More

Remove Border of Bars from Histogram in Base R

Nizamuddin Siddiqui
Updated on 21-Nov-2020 05:58:53

889 Views

By default, a histogram drawn in base R has black color borders around bars. We might want to remove these black borders to make the histogram visually smooth. This can be done by using lty argument with the hist function. For example, if we have a vector x and we want to create a histogram of x without border of bars then we can use the argument as hist(x,lty="blank").Example> x hist(x)Output:Creating histogram of x without border of bars:Example> hist(x,lty="blank")Output:

Shift Certain Elements to the End of Array in JavaScript

AmitDiwan
Updated on 21-Nov-2020 05:58:26

412 Views

We are required to write a JavaScript function that takes in an array of numbers as the first argument and a single number as the second argument.Our function should check for all the instances of second number in the array, if there exists any, the function should push all those instances to the end of the array.If the input array is −const arr = [1, 5, 6, 6, 5, 3, 3];And the second argument is 6Then the array should become −const output = [1, 5, 5, 3, 3, 6, 6];Exampleconst arr = [1, 5, 6, 6, 5, 3, 3]; const ... Read More

Advertisements