We are required to write a JavaScript function that takes in a string that represents a binary code. The function should return the alphabetical representation of the string.For example −If the binary input string is −const str = '1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100';Then the output should be −const output = 'Hello World';ExampleThe code for this will be −const str = '1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100'; const binaryToString = (binary = '') => { let strArr = binary.split(' '); const str = strArr.map(part => { return String.fromCharCode(parseInt(part, 2)); }).join(''); return str; }; console.log(binaryToString(str));OutputAnd the output in the console will be −Hello World
We are required to write a JavaScript function that takes in a string as the only input. The function should construct and return the binary representation of the input string.For example −If the input string is −const str = 'Hello World';Then the output should be −const output = '1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100';ExampleThe code for this will be −const str = 'Hello World'; const textToBinary = (str = '') => { let res = ''; res = str.split('').map(char => { return char.charCodeAt(0).toString(2); }).join(' '); return res; }; console.log(textToBinary('Hello World'));OutputAnd the output in the console will be −1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100
We are required to write a JavaScript function that takes in an array of Numbers as the first argument and a number, say n, as the second argument.Our function should then pick the n greatest numbers from the array and return a new array consisting of those numbers.ExampleThe code for this will be −const arr = [3, 4, 12, 1, 0, 5, 22, 20, 18, 30, 52]; const pickGreatest = (arr = [], num = 1) => { if(num > arr.length){ return []; }; const sorter = (a, b) => b - a; ... Read More
Subsetting of a data frame can be done in many ways and one such say is selecting the columns that are stored in a vector. Suppose we have a data frame df that has columns x, y, and z and the column names y and z are stored in a vector called V then we can subset df by excluding column names in V as select(df, -all_of(V)).ExampleConsider the below data frame:Live Demo> x1 x2 x3 x4 df1 df1Outputx1 x2 x3 x4 1 3 4 0 5 2 4 1 2 6 3 4 1 2 3 4 8 1 7 ... Read More
We are required to write a JavaScript function that takes in an array of literal values.The array might contain some repeating values.Our function should remove all the values from the array that are repeating. We are required to remove all instances of all such elements.ExampleThe code for this will be −const arr = [1, 2, 3, 2, 4]; const removeAllInstances = (arr = []) => { filtered = arr.filter(val => { const lastIndex = arr.lastIndexOf(val); const firstIndex = arr.indexOf(val); return lastIndex === firstIndex; }); return filtered; }; console.log(removeAllInstances(arr));OutputAnd the output in the console will be −[ 1, 3, 4 ]
Suppose, we have an array of objects that contains data about some students like this −const arr = [{ name: 'A', idNo: 1, marks: { math: 98, sci: 97, eng: 89 } }, { name: 'B', idNo: 2, marks: { math: 88, sci: 87, eng: 79 } }, { name: 'C', idNo: 3, marks: { math: 87, sci: 98, eng: 91 ... Read More
A named vector cannot be directly converted to a list because we would need to un-name the vector names and convert those names to names of the list elements. This can be done by using lapply function function. For example, suppose we have a named vector x then it can be converted to a list by using the command x x1 names(x1) x1OutputA B C D E F G H I J K L M N O P Q R S T U V W X Y Z 1 2 3 4 5 6 7 8 9 10 11 12 ... Read More
If two data frames in R have equal number of columns then we can find the correlation coefficient among the columns of these data frames which will be the correlation matrix. For example, if we have a data frame df1 that contains column x and y and another data frame df2 that contains column a and b then the correlation coefficient between df1 and df2 can be found by cor(df1, df2).Example1Consider the below data frame:Live Demo> x1 x2 df1 df1Output x1 x2 1 39.56630 38.25632 2 39.43689 44.14647 3 40.80479 37.43309 ... Read More
The abline function can give us a straight line from intercept 0 with slope 1 in an existing plot. We would need to pass the coefficients inside the function as abline(coef = c(0,1)). Therefore, we can use this function to add a line starting from bottom left and ending at top right. This is also called diagonal line because it joins the end points on one side with the opposite of the other side.Example> plot(1:10,type="n") > abline(coef=c(0,1))Output:
The mean of row values can be found by using rowwise function of dplyr package along with the mutate function to add the new column of means in the data frame. The rowwise function actually helps R to read the values in the data frame rowwise and then we can use mean function to find the means as shown in the below examples.Example1Consider the below data frame:Live Demo> x1 x2 df1 df1Output x1 x2 1 0 8 2 2 3 3 2 5 4 0 5 5 3 2 6 0 10 7 3 5 8 1 7 9 0 ... Read More