We are required to write a JavaScript function that takes in a number and checks whether it falls in Fibonacci series or not.We should return a boolean on this basis.ExampleThe code for this will be −const num = 89; const isFib = query => { if(query === 0 || query === 1){ return true; } let prev = 1; let count = 2; let temp = 0; while(count >= query){ if(prev + count === query){ return true; }; temp = prev; prev = count; count += temp; }; return false; }; console.log(isFib(num));OutputFollowing is the output on console −true
Suppose we have a sorted array of literals like this −const arr = [2, 2, 3, 3, 3, 5, 5, 6, 7, 8, 9];We are required to write a JavaScript function that takes in one such array and returns the first number that appears only once in the array.If there is no such number in the array, we should return false.For this array, the output should be 6.ExampleThe code for this will be −const arr = [2, 2, 3, 3, 3, 5, 5, 6, 7, 8, 9]; const firstNonDuplicate = arr => { let appeared = false; ... Read More
We are required to write a JavaScript function that takes in a Number, say n, and we are required to check whether there exist such three consecutive natural numbers (not decimal/floating point) whose sum equals to n.If there exist such numbers, our function should return them, otherwise it should return false.ExampleThe code for this will be −const sum = 54; const threeConsecutiveSum = sum => { if(sum < 6 || sum % 3 !== 0){ return false; } // three numbers will be of the form: // x + x + 1 + ... Read More
We are required to write a JavaScript function that takes in a lowercase string and sorts it in the reverse order i.e., b should come before a, c before b and so on.For example:If the input string is −const str = "hello";Then the output should be −const output = "ollhe";The code for this will be −const string = 'hello'; const sorter = (a, b) => { const legend = [-1, 0, 1]; return legend[+(a < b)]; } const reverseSort = str => { const strArr = str.split(""); return strArr .sort(sorter) .join(""); }; console.log(reverseSort(string));Following is the output on console −ollhe
We have an array of numbers like this −const arr = [1, 1, 5, 2, -4, 6, 10];We are required to write a function that returns a new array, of the same size but with each element being the sum of all elements until that point.Therefore, the output should look like −const output = [1, 2, 7, 9, 5, 11, 21];Therefore, let’s write the function partialSum(), The full code for this function will be −const arr = [1, 1, 5, 2, -4, 6, 10]; const partialSum = (arr) => { const output = []; arr.forEach((num, index) => { ... Read More
We are required to create a function that takes in a number and finds the sum of its digits recursively until the sum is a one-digit number.For examplefindSum(12345) = 1+2+3+4+5 = 15 = 1+5 = 6Therefore, the output should be 6.Let’s write the code for this function findSum()// using recursion const findSum = (num) => { if(num < 10){ return num; } const lastDigit = num % 10; const remainingNum = Math.floor(num / 10); return findSum(lastDigit + findSum(remainingNum)); } console.log(findSum(2568));We check if the number is less than 10, it’s already minified and ... Read More
You might have heard that matrix can contain only numerical values but it is also possible to create a matrix with string values, and of course calculations using these types of matrices would not be possible. To create a matrix using string values, we can first create a vector of strings then define its dimension with dim function and that will convert the vector into matrix of string values.Example Live DemoM1
Suppose we have one n × n matrix containing values from 0 to n. Here 0 represents an unfilled square, we have to check whether we can fill empty squares such that in each row and each column every number from 1 to n appears exactly once.So, if the input is like002201123then the output will be True, as we can set the matrix to312231123To solve this, we will follow these steps −Define a function find_empty_cell() . This will take matrix, nfor i in range 0 to n, dofor j in range 0 to n, doif matrix[i, j] is same as ... Read More
Finding the position of one of more values that are common in two vectors can be easily done with the help of match function. The match function will match the values in first and second vector then return the index or position of these common values in second vector.Example Live Demoset.seed(145) x1
We can plot numerical values in R with many scales and that includes log scale as well. Also, it is possible to plot the values with log scales on both the axes. In base R, the best way to do this is defining the axes values with decimal representation as shown in the below examples with well-defined log.Consider the below vector −Example Live Demoset.seed(555) x