We are required to write a JavaScript function that takes in a non-negative Integer and computes and returns its square root. We can floor off a floating-point number to an integer.For example: For the number 15, we need not to return the precise value, we can just return the nearest smaller integer value that will be 3, in case of 15We will make use of the binary search algorithm to converse to the square root of the given number.The code for this will be −Exampleconst squareRoot = (num = 1) => { let l = 0; let r = ... Read More
We are required to write a JavaScript function that takes a string as input and reverse only the vowels of a string.For example −If the input string is −const str = 'Hello';Then the output should be −const output = 'Holle';The code for this will be −const str = 'Hello'; const reverseVowels = (str = '') => { const vowels = new Set(['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']); let left = 0, right = str.length-1; let foundLeft = false, foundRight = false; str = str.split(""); while(left < right){ if(vowels.has(str[left])){ ... Read More
The chi square test for goodness of fit is a nonparametric test to test whether the observed values that falls into two or more categories follows a particular distribution of not. We can say that it compares the observed proportions with the expected chances. In R, we can perform this test by using chisq.test function. Check out the below examples to understand how it is done.Example1Live Demo> x1 x1Output[1] 9 4 1 9 6 6 1 6 0 0 5 8 8 3 7 8 0 3 3 9 6 0 3 8 2 0 8 5 9 1 3 ... Read More
Consider the following input and output arrays −const input = ["0:3", "1:3", "4:5", "5:6", "6:8"]; const output = [ [0, 1, 3], [4, 5, 6, 8] ];Considering each number as a node in a graph, and each pairing x:y as an edge between nodes x and y, we are required to find the sets of numbers that can be traveled to using the edges defined.That is, in graph theory terms, find the distinct connected components within such a graph. For instance, in the above arrays, there is no way to travel from 4 to 0 so they are ... Read More
The chartr function in base R helps us to replace old strings with new strings and hence it can be also used to replace a subs-string with the reverse of that substring. For example, if we have a vector say x that contains tutorialpsoint and we want to convert it to tutorialspoint then it can be done as chartr("tutorialpsoint ", " tutorialspoint ", x).Example1Live Demo> x1 x1Output[1] "IDNIA"Example> chartr("DN", "ND", x1)Output[1] "INDIA" Example2Live Demo> x2 x2Output[1] "IDNIA" "IDNIA" "IDNIA" "IDNONESIA" "IDNIA" "IDNONESIA" [7] "IDNONESIA" "IDNIA" "IDNONESIA" "IDNIA" "IDNIA" "IDNONESIA" [13] "IDNONESIA" "IDNONESIA" "IDNIA" "IDNONESIA" "IDNIA" "IDNIA" [19] "IDNONESIA" "IDNONESIA" "IDNIA" ... Read More
We are required to write a JavaScript function that takes in an array of arrays of Numbers as the first argument and an array of Numbers as the second argument. The function should pick a subarray from each array of the first array, (subarray that contains item common to both the second array and the corresponding array of first array.)For example −If the inputs are −Exampleconst arr1 = [ [1, 2, 5, 6], [5, 13, 7, 8], [9, 11, 13, 15], [13, 14, 15, 16], [1, 9, 11, 12] ]; const arr2 = [9, 11, 13, 15, 1, 2, 5, ... Read More
If we have many lists but we want to use the values in the lists as a vector then we first need to combine those lists and create a vector. This can be done by using unlist function along with the combine function c to create the vector. For example, if we have two lists defined as List1 and List2 and we want to create a vector V using these lists then it can be created as:V x1 x1Output$a [1] -0.6972237 -1.5013768 -0.2451809 -0.2365569 -1.6304919 -1.1704378 [7] 1.1617054 -0.2349498 -1.2582229 0.4112065 $b [1] 2 0 2 6 0 0 ... Read More
We are required to write a JavaScript function that takes in an array of Numbers that may contain some repeating elements. The function should return the length of the longest repeating number sequence from the array.For example −If the input array is −const arr = [2, 1, 1, 2, 3, 3, 2, 2, 2, 1];Then the output should be 3 because the number 2 is repeated 3 times consecutively in the array (and that's the highest number).Exampleconst arr = [2, 1, 1, 2, 3, 3, 2, 2, 2, 1]; const findLongestSequence = (arr = []) => { const res ... Read More
Suppose, we have two arrays like these −const input = ['S-1', 'S-2', 'S-3', 'S-4', 'S-5', 'S-6', 'S-7', 'S-8']; const sortingArray = ["S-1", "S-5", "S-2", "S-6", "S-3", "S-7", "S-4", "S-8"];We are required to write a JavaScript function that takes in two such arrays as first and second argument respectively.The function should sort the elements of the first array according to their position in the second array.The code for this will be −Exampleconst input = ['S-1', 'S-2', 'S-3', 'S-4', 'S-5', 'S-6', 'S-7', 'S-8']; const sortingArray = ["S-1", "S-5", "S-2", "S-6", "S-3", "S-7", "S-4", "S-8"]; const sortByReference = (arr1 = [], arr2 = ... Read More
The main title of a histogram in base R can be inserted by using title function and if we want to have it in the outer margin then outer argument must be set to TRUE. If the outer argument does not fulfil our requirement then we can use par function to adjust the outer margin area and create the histogram. Check out the below example to understand how it works.Example> x hist(x) > title('Normal Distribution',outer=TRUE)OutputExample> par(oma=c(0,0,2,0)) > hist(x) > title('Normal Distribution',outer=TRUE)Output