Finding the Product of Array Elements with Reduce in JavaScript

AmitDiwan
Updated on 19-Apr-2021 08:01:24

942 Views

ProblemWe are required to write a JavaScript function that takes in an array arr. Our function should find and return the product of all the elements of the array.ExampleFollowing is the code − Live Democonst arr = [3, 1, 4, 1, 2, -2, -1]; const produceElements = (arr = []) => {    const res = arr.reduce((acc, val) => {       acc = acc * val;       return acc;    }, 1);    return res; }; console.log(produceElements(arr));OutputFollowing is the console output −48

Decompose Rational Number as a Sum of Rationals in JavaScript

AmitDiwan
Updated on 19-Apr-2021 07:59:58

232 Views

ProblemWe are required to write a JavaScript function that takes in an array of exactly two numbers.The first element specifies the numerator of any rational number and the second element specifies the denominator of the same.Our function should return an array of any number of sub arrays of two elements each such that when the rational number specified by the subarray are added they sum up to the input rational number and the numerator of all the subarrays should be 1.We also need to make sure that the number of subarrays is as small as possible.ExampleFollowing is the code − Live ... Read More

Encode Number String into 0s and 1s in JavaScript

AmitDiwan
Updated on 19-Apr-2021 07:56:57

337 Views

ProblemWe are required to write a JavaScript function that takes in a string that represents a decimal number.Our function should convert/encode this decimal into binary based on the following rules.For each digit d of nLet k be the number of bits of dWe write k-1 times the digit 0 followed by the digit 1We write digit d as a binary string, with the rightmost bit being the least significantLastly, we concatenate the result of b) and c) to get the coding of dAt last, we concatenate all the results got for the digits of n.Thus, code 2 as 0110 and ... Read More

Finding Reflection of a Point Relative to Another Point in JavaScript

AmitDiwan
Updated on 19-Apr-2021 07:41:36

382 Views

Point Of Symmetry"Point reflection" or "point symmetry" is a basic concept in geometry where a given point, P, at a given position relative to a midpoint, Q has a corresponding point, P1, which is the same distance from Q but in the opposite direction.ProblemWe are required to write a JavaScript function that takes in two objects P and Q specifying two points in a 2-D plane.Our function should output the symmetric point of point P about Q.ExampleFollowing is the code − Live Democonst p = {    x: 6, y: -4 }; const q = {    x: 11, y: 5 ... Read More

Perform Power Operations on an Array of Numbers in JavaScript

AmitDiwan
Updated on 19-Apr-2021 07:39:46

418 Views

ProblemWe are required to write a JavaScript function that takes in an array of integers, arr, of even length.Suppose a number num where −num = (arr[0] * arr[0] + arr[1] * arr[1]) * (arr[2] * arr[2] + arr[3] * arr[3]) * … * (arr[n-2] * arr[n-2] + arr[n-1] * arr[n-1])Where n is the length of the array.Our function should find and return an array of two numbers [A, B] such that −A2 + B2 = numFor instance, if the array is −[1, 2, 3, 4]Then num = ( 1 + 4 ) * (9 + 16) = 125Then output should ... Read More

Searching for Target String in a Strangely Sorted Array in JavaScript

AmitDiwan
Updated on 19-Apr-2021 07:36:53

158 Views

ProblemWe are required to write a JavaScript function that takes in a word target and an array of sorted(by length(increasing), number of uppercase letters(decreasing), natural order) unique words which always contains the target.The task of our function is to find the index(0 based) of the target in the array of words, which would always be in the list.ExampleFollowing is the code − Live Democonst arr = ['cP', 'rE', 'sZ', 'am', 'bt', 'ev', 'hq', 'rx', 'yi', 'akC', 'nrcVpx', 'iKMVqsj']; const target = 'akC'; const findTarget = (arr = [], target = '') => {    const index = arr.indexOf(target);    return index; ... Read More

Number Difference After Interchanging First Digits in JavaScript

AmitDiwan
Updated on 19-Apr-2021 07:35:17

88 Views

ProblemWe are required to write a JavaScript function that takes in an array of exactly two numbers. Our function should return the absolute difference between the numbers after interchanging their first digits.For instance, for the array [105, 413], The difference will be: |405 - 113| = 292ExampleFollowing is the code − Live Democonst arr = [105, 413]; const interchangedDigitDiff = (arr = []) => {    arr = arr.map(String);    const [first, second] = arr;    const fChar = first[0];    const sChar = second[0];    const newFirst = sChar + first.substring(1, first.length);    const newSecond = fChar + second.substring(1, second.length); ... Read More

Interchanging First Letters of Words in a String in JavaScript

AmitDiwan
Updated on 19-Apr-2021 07:31:43

327 Views

ProblemWe are required to write a JavaScript function that takes in a string that contains exactly two words.Our function should construct and return a new string in which the first letter of the words are interchanged with each other.ExampleFollowing is the code − Live Democonst str = 'hello world'; const interchangeChars = (str = '') => {    const [first, second] = str.split(' ');    const fChar = first[0];    const sChar = second[0];    const newFirst = sChar + first.substring(1, first.length);    const newSecond = fChar + second.substring(1, second.length);    const newStr = newFirst + ' ' + newSecond;    return newStr; }; console.log(interchangeChars(str));OutputFollowing is the console output −wello horld

Find Immediate Bigger Number with Same Digits in JavaScript

AmitDiwan
Updated on 19-Apr-2021 07:30:18

320 Views

ProblemWe are required to write a JavaScript function that takes in a number n. Our function should rearrange the digits of the numbers such that we form the smallest number using the same digits but just bigger than the input number.For instance, if the input number is 112. Then the output should be 121.ExampleFollowing is the code − Live Democonst num = 112; const findNextBigger = (num = 1) => {    const sortedDigits = (num = 1) => {       return String(num)       .split('')       .sort((a, b) => b - a);    };    let max = sortedDigits(num).join('');    max = Number(max);    for(let i = num + 1; i

Remove Second Number of the Pair That Adds Up to a Target in JavaScript

AmitDiwan
Updated on 19-Apr-2021 07:27:15

136 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers and a target sum.Our function should remove the second number of all such consecutive number pairs from the array that add up to the target number.ExampleFollowing is the code − Live Democonst arr = [1, 2, 3, 4, 5]; const target = 3; const removeSecond = (arr = [], target = 1) => {    const res = [arr[0]];    for(i = 1; i < arr.length; i++){       if(arr[i] + res[res.length-1] !== target){          res.push(arr[i]);       };    };    return res; }; console.log(removeSecond(arr, target));OutputFollowing is the console output −[ 1, 3, 4, 5 ]

Advertisements