We are required to write a JavaScript function that takes in a number and returns the count of numbers that exactly divides the input number.For example −If the number is 12, then its factors are −1, 2, 3, 4, 6, 12Therefore, the output should be 6.ExampleFollowing is the code −const num = 12; const countFactors = num => { let count = 0; let flag = 2; while(flag
Suppose we have a data.txt file that lives in the same directory as our NodeJS file. Suppose the content of that file is −Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s.We are required to write a JavaScript function that loads this external ... Read More
We are required to write a JavaScript function that takes in string and returns an array with two string values, and they should be the smallest and largest words respectively from the string.For example −If the string is −const str = "Hardships often prepare ordinary people for an extraordinary destiny";Then the output should be −const output = ["an", "extraordinary"];So, let's write the code for this functionExampleFollowing is the code −const str = "Hardships often prepare ordinary people for an extraordinary destiny"; const largestSmallest = str => { const strArr = str.split(" "); let min = strArr[0]; let ... Read More
We are given the lengths of three sides of a triangle and we are required to write a function that returns the area of the triangle using the length of its sides.Heron's formulaWe can calculate the area of a triangle if we know the lengths of all three sides, using Heron's formula −Step 1 − Calculate "s" (half of the triangles perimeter) −s = (a+b+c) / 2Step 2 − Then calculate the Area using Herons formula −A = sqrt( s(s-a)(s-b)(s-c) )ExampleSo, Let’s write the code for this function −const sides = [12, 4, 9]; const areaOfTriangle = sides => { ... Read More
We are required to write a JavaScript function that takes in a number and returns a number that can be represented as a power of 2 which is nearest to the input number.For example −If the input number if 365, then the output should be 256, because 256 is the nearest such number to 365 which can be represented as 2^n for some whole number value of n.ExampleLet’s write the code for this function −const num = 365; const nearestPowerOfTwo = num => { // dealing only with non-negative numbers if(num < 0){ num *= ... Read More
We are required to write a JavaScript function that takes in three numbers (representing the coefficient of quadratic term, coefficient of linear term and the constant respectively in a quadratic quadratic).And we are required to find the roots, (if they are real roots) otherwise we have to return false. Let's write the code for this functionExampleFollowing is the code −const coefficients = [3, 12, 2]; const findRoots = co => { const [a, b, c] = co; const discriminant = (b * b) - 4 * a * c; if(discriminant < 0){ // the ... Read More
We are required to write a JavaScript function that takes in a string and returns a new string with only the words that appeared for more than once in the original string.For example: If the input string is −const str = "big black bug bit a big black dog on his big black nose";Then the output should be −const output = "big black";ExampleLet’s write the code for this function −const str = "big black bug bit a big black dog on his big black nose"; const findDuplicateWords = str => { const strArr = str.split(" "); const res ... Read More
We can use SCROLLABLE CURSOR to directly point the cursor to the mentioned relative position. The relative position is the position of the row in the result table from the current row. For example, consider the table below.ORDER_IDORDER_DATEA223672020-07-28A667562020-07-28A778902020-07-29A968322020-07-29If the cursor is currently pointing to the 2nd absolute row i.e, ORDER_ID A66756 then the relative +2 position will be ORDER_ID A96832 and relative -1 position will be ORDER_ID A22367.The syntax to use relative position in FETCH statement is−EXEC SQL FETCH RELATIVE +2 ORDER_CURR INTO :ORDER-ID, :ORDER-DATE END-SQLRead More
The SCROLLABLE CURSOR can be used to directly point the cursor position to the mentioned absolute position. The absolute position is the position of a particular row in the result table from the first row.We can fetch the absolute position by using ABSOLUTE parameter in the FETCH statement. For example, we have to declare a scrollable cursor as below.EXEC SQL DECLARE ORDER_CURR SCROLL CURSOR FOR SELECT ORDER_ID, ORDER_DATE FROM ORDERS WHERE ORDER_DATE = ‘2020-07-29’ END-SQLNow if we want to fetch the absolute 9th row then we will ... Read More
The INSENSITIVE SCROLLABLE CURSOR are sort of read only cursors in which the result table cannot change once the cursor is opened. The other applications also cannot update the INSENSITIVE SCROLLABLE CURSOR once it is opened. The SENSITIVE SCROLLABLE CURSOR, unlike INSENSITIVE are sensitive to changes made in the result table. The changes made by other applications will be reflected in the result table.We can declare SENSITIVE and INSENSITIVE SCROLLABLE CURSOR like below.EXEC SQL DECLARE ORDER_CURR SENSITIVE SCROLL CURSOR FOR SELECT ORDER_ID, ORDER_DATE FROM ORDERS WHERE ORDER_DATE ... Read More