Alternate addition multiplication in an array - JavaScript

AmitDiwan
Updated on 15-Sep-2020 09:04:14

276 Views

We are required to write a JavaScript function that takes in an array of numbers and returns the alternative multiplicative sum of the elementsFor example −If the array is −const arr = [1, 2, 4, 1, 2, 3, 4, 3];then the output should be calculated like this −1*2+4*1+2*3+4*3 2+4+6+12And the output should be −24ExampleLet's write the code for this −const arr = [1, 2, 4, 1, 2, 3, 4, 3]; const alternateOperation = arr => {    const productArr = arr.reduce((acc, val, ind) => {       if(ind % 2 === 1){          return acc;       };       acc.push(val * (arr[ind + 1] || 1));       return acc;    }, []);    return productArr.reduce((acc, val) => acc + val); }; console.log(alternateOperation(arr));OutputThe output in the console: −24

Convert object to a Map - JavaScript

AmitDiwan
Updated on 15-Sep-2020 09:02:18

1K+ Views

Suppose we have an object like this −const obj = {    name: "Vikas",    age: 45,    occupation: "Frontend Developer",    address: "Tilak Nagar, New Delhi",    experience: 23,  };We are required to write a JavaScript function that takes in such an object with key value pairs and converts it into a Map.ExampleLet's write the code for this −const obj = {    name: "Vikas",    age: 45,    occupation: "Frontend Developer",    address: "Tilak Nagar, New Delhi",    experience: 23,    salary: "98000" }; const objectToMap = obj => {    const keys = Object.keys(obj);    const map ... Read More

Convert nested array to string - JavaScript

AmitDiwan
Updated on 15-Sep-2020 08:58:44

559 Views

We are required to write a JavaScript function that takes in a nested array of literals and converts it to a string by concatenating all the values present in it to the stringconst arr = [    'hello', [       'world', 'how', [          'are', 'you', [             'without', 'me'          ]       ]    ] ];ExampleLet’s say the following is our nested array −const arr = [    'hello', [       'world', 'how', [          'are', 'you', [             'without', 'me'          ]       ]    ] ]; const arrayToString = (arr) => {    let str = '';    for(let i = 0; i < arr.length; i++){       if(Array.isArray(arr[i])){          str += arrayToString(arr[i]);       }else{          str += arr[i];       };    };    return str; }; console.log(arrayToString(arr));OutputFollowing is the output in the console −helloworldhowareyouwithoutme

Comparing ascii scores of strings - JavaScript

AmitDiwan
Updated on 15-Sep-2020 08:55:05

1K+ Views

ASCII CodeASCII is a 7-bit character code where every single bit represents a unique character.Every English alphabet has a unique decimal ascii code.We are required to write a function that takes in two strings and calculates their ascii scores (i.e., the sum of ascii decimal of each character of string) and returns the difference.ExampleLet's write the code for this −const str1 = 'This is the first string.'; const str2 = 'This here is the second string.'; const calculateScore = (str = '') => {    return str.split("").reduce((acc, val) => {       return acc + val.charCodeAt(0);    }, 0); ... Read More

Comparing forEach() and reduce() for summing an array of numbers in JavaScript.

AmitDiwan
Updated on 15-Sep-2020 08:52:42

478 Views

We are required to compare the time taken respectively by the ES6 functions forEach() and reduce() for summing a huge array of numbers.As we can't have a huge array of numbers here, we will simulate the magnitude of array by performing the summing operation for large number of times (iterations)ExampleLet's write the code for this −const arr = [1, 4, 4, 54, 56, 54, 2, 23, 6, 54, 65, 65]; const reduceSum = arr => arr.reduce((acc, val) => acc + val); const forEachSum = arr => {    let sum = 0;    arr.forEach(el => sum += el);    return ... Read More

Prime numbers in a range - JavaScript

AmitDiwan
Updated on 15-Sep-2020 08:50:23

1K+ Views

We are required to write a JavaScript function that takes in two numbers, say, a and b and returns the total number of prime numbers between a and b (including a and b, if they are prime).For example −If a = 2, and b = 21, the prime numbers between them are 2, 3, 5, 7, 11, 13, 17, 19And their count is 8. Our function should return 8.Let’s write the code for this function −ExampleFollowing is the code −const isPrime = num => {    let count = 2;    while(count < (num / 2)+1){       if(num ... Read More

What are the steps involved to use a CURSOR in any COBOL-DB2 program?

Mandalika
Updated on 14-Sep-2020 15:22:57

9K+ Views

The CURSOR is used when we have to fetch multiple rows from a table. There are 4 steps involved in order to use a cursor in a COBOL-DB2 program.DECLARE cursor− In this step we will define the layout of the cursor. We will give the query we want to use. For example−EXEC SQL DECLARE ORDER_CUR CURSOR FOR SELECT ORDER_ID FROM ORDERS WHERE ORDER_DATE = ‘2020-07-28’ END-EXECOPEN cursor− Next we will open our cursor. This statement readies the cursor for data retrieval. For example−EXEC SQL OPEN ORDER_CUR END-EXECFETCH cursor− In this statement, we start fetching the data from DB2 and the ... Read More

Explain the scenario in which CURSOR should be used over a standalone SELECT statement?

Mandalika
Updated on 14-Sep-2020 15:13:40

123 Views

The standalone SELECT statement is generally used when we use a primary or an alternate key in the WHERE clause. Therefore, In this case we are certain that the standalone select statement will return only one row since the primary key cannot have a duplicate value (multiple rows).If we want to query the database using a non-unique key which could return multiple rows from a DB2 table, we have to use a cursor to handle the multiple rows returned. We can access the cursor in a loop to read each row data one by one.For example, if we want to ... Read More

What we can conclude if the final value in the NULL indicator is -2?

Mandalika
Updated on 14-Sep-2020 15:07:28

1K+ Views

A NULL indicator is a 2 bytes field which serves the multiple purpose. This indicator takes the value as -1 when any DB2 column has NULL value and it takes the value as 0 when DB2 column has a non NULL value.Although the main purpose of the NULL indicator is to check whether a column has a NULL value or not, this indicator can take a value of -2 as well. A -2 value in this indicator states that a NULL value was assigned to the host variable as a result of evaluating an expression with an arithmetic error, or ... Read More

What is the purpose of “NOT NULL WITH DEFAULT” clause used in DB2 table column?

Mandalika
Updated on 14-Sep-2020 15:02:16

1K+ Views

When we define the DB2 table, we can declare any column as “NOT NULL” which means that in any case this column cannot store NULL value.Now if we try to store a NULL value in this column in our COBOL-DB2 program using -1 value in the NULL indicator then our query will fail. In this case the SQLCODE field of SQLCA will give the error code as -407. As per the IBM documentation -407 error code states that−“AN UPDATE, INSERT, OR SET VALUE IS NULL, BUT THE OBJECT COLUMN CANNOT CONTAIN NULL VALUES”.

Advertisements