Found 10710 Articles for Web Development

Sorting words by last character present in them in JavaScript

AmitDiwan
Updated on 19-Apr-2021 08:08:06

441 Views

ProblemWe are required to write a JavaScript function that takes in a string of words str. Our function needs to return an array of the words, sorted alphabetically by the final character in each.If two words have the same last letter, the returned array should show them in the order they appeared in the given string.ExampleFollowing is the code − Live Democonst str = 'this is some sample string'; const sortByLast = (str = '') => {    const arr = str.split(' ');    const sorter = (a, b) => {         return a[a.length - 1].charCodeAt(0) - b[b.length ... Read More

Difference between numbers and string numbers present in an array in JavaScript

AmitDiwan
Updated on 19-Apr-2021 08:06:43

285 Views

ProblemWe are required to write a JavaScript function that takes in a mixed array of number and string representations of integers.Our function should add up okthe string integers and subtract this from the total of the non-string integers.ExampleFollowing is the code − Live Democonst arr = [5, 2, '4', '7', '4', 2, 7, 9]; const integerDifference = (arr = []) => {    let res = 0;    for(let i = 0; i < arr.length; i++){       const el = arr[i];       if(typeof el === 'number'){          res += el;       }else if(typeof el === 'string' && +el){          res -= (+el);       };    };    return res; }; console.log(integerDifference(arr));OutputFollowing is the console output −10

Encoding decimal to factorial and back in JavaScript

AmitDiwan
Updated on 19-Apr-2021 08:04:44

231 Views

ProblemCoding decimal numbers with factorials is a way of writing out numbers in a base system that depends on factorials, rather than powers of numbers.In this system, the last digit is always 0 and is in base 0!. The digit before that is either 0 or 1 and is in base 1!. The digit before that is either 0, 1, or 2 and is in base 2!, etc. More generally, the nth-to-last digit is always 0, 1, 2, ..., n and is in base n!.We will need two functions. The first one will receive a decimal number and return a ... Read More

Finding the product of array elements with reduce() in JavaScript

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

747 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

Decomposing rational number as a sum of rationals in JavaScript

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

157 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

Encoding a number string into a string of 0s and 1s in JavaScript

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

248 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

242 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

Performing power operations on an array of numbers in JavaScript

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

353 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

108 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 their first digits in JavaScript

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

47 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

Advertisements