We are required to write a JavaScript function that takes in a string and replaces the adjacent words of that string.For example: If the input string is −const str = "This is a sample string only";Then the output should be −"is This sample a only string"Let’s write the code for this function −ExampleFollowing is the code −const str = "This is a sample string only"; const replaceWords = str => { return str.split(" ").reduce((acc, val, ind, arr) => { if(ind % 2 === 1){ return acc; } ... Read More
We are required to write a JavaScript function that takes in two numbers, say m and n and returns two numbers whose sum is n and product is m. If there exist no such numbers, then our function should return falseLet’s write the code for this function −Exampleconst perfectNumbers = (sum, prod) => { for(let i = 0; i < (sum / 2); i++){ if(i * (sum-i) !== prod){ continue; }; return [i, (sum-i)]; }; return false; }; // 12 12 are not two distinct numbers console.log(perfectNumbers(24, 144)); console.log(perfectNumbers(14, 45)); console.log(perfectNumbers(21, 98));OutputFollowing is the output in the console −false [ 5, 9 ] [ 7, 14 ]
The foreign key is used to establish a referential constraint between the child table(in which column is defined as foreign key) and parent table (in which foreign key of the child table becomes primary key). For example if we have an ORDER table in which foreign key is defined as TRANSACTION_ID. This foreign key will refer to the TRANSACTION_ID column of TRANSACTIONS table. In this TRANSACTIONS table, TRANSACTION_ID will be the primary key. The parent table here is TRANSACTIONS table while the child table here is ORDERS table.The CASCADE rule of the foreign key states that when any entry is ... Read More
We are required to write a JavaScript function that takes in array of strings and delete every one of the two string that start with the same letter.For example, If the actual array is −const arr = ['Apple', 'Jack' , 'Army', 'Car', 'Jason'];Then, we have to keep only one string in the array, so one of the two strings starting with A should get deleted. In the same way, the logic follows for the letter J in the above array.Let’s write the code for this function −Exampleconst arr = ['Apple', 'Jack' , 'Army', 'Car', 'Jason']; const delelteSameLetterWord = arr => ... Read More
The DB2 table contains a number of columns whose value will remain unique in the entire table. Among these multiple columns only one column is selected as the primary key and the remaining keys are known as candidate keys.We can declare any candidate key as an alternate key. Which means that the value of this key cannot take duplicate value, however unlike primary key the primary index is not built on the alternate key.We can define alternate key while defining any table using a UNIQUE keyword. For example, if we want to make TRANSACTION_ID as an alternate key then−CREATE TABLE ... Read More
We are required to write a JavaScript function that takes in a negative integer and returns the sum of its digitsFor example −-234 --> -2 + 3 + 4 = 5 -54 --> -5 + 4 = -1Let’s write the code for this function −ExampleFollowing is the code −const num = -4345; const sumNum = num => { return String(num).split("").reduce((acc, val, ind) => { if(ind === 0){ return acc; } if(ind === 1){ acc -= +val; return acc; }; acc += +val; return acc; }, 0); }; console.log(sumNum(num));OutputFollowing is the output in the console −8
We are required to write a JavaScript function that takes in an array of numbers with duplicate entries and sums all the duplicate entries to one indexFor example −If the input array is −const input = [1, 3, 1, 3, 5, 7, 5, 4];Then the output should be −const output = [2, 6, 7, 10, 4];ExampleLet’s write the code −const input = [1, 3, 1, 3, 5, 7, 5, 3, 4]; const sumDuplicate = arr => { const map = arr.reduce((acc, val) => { if(acc.has(val)){ acc.set(val, acc.get(val) + 1); }else{ return acc; }, new Map()); } return Array.from(map, el => el[0] * el[1]); }; console.log(sumDuplicate(input));OutputFollowing is the output in the console −[ 2, 9, 10, 7, 4 ]
We are required to write a JavaScript function that takes in a number and returns the difference of the sums of the digits at even place and the digits odd placeExampleLet’s write the code −const num = 345336; const evenOddDifference = (num, res = 0, ind = 0) => { if(num){ if(ind % 2 === 0){ res += num % 10; }else{ res -= num % 10; }; }; return Math.abs(res); }; console.log(evenOddDifference(num));OutputFollowing is the output in the console −2
We have two arrays of objects like these −const blocks = [ { id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }, ] const containers = [ { block: { id: 1 } }, { block: { id: 2 } }, { block: { id: 3 } }, ]We are required to write a function that checks each object of blocks array with the block key of each object of containers array and see if there exists any id in blocks array that is not present in ... Read More
We have an array of numbers like this −const arr = [-1, -2, -1, 0, -1, -2, -1, -2, -1, 0, 1, 0];Let’s say, we are required to write a JavaScript function that counts the consecutive groups of negative numbers in the array.Like here we have consecutive negatives from index 0 to 2 which forms one group and then from 4 to 8 forms the second groupSo, for this array, the function should return 2.ExampleLet’s write the code −const arr = [-1, -2, -1, 0, -1, -2, -1, -2, -1, 0, 1, 0]; const countNegativeGroup = arr => { ... Read More