Found 6710 Articles for Javascript

Finding deviations in two Number arrays in JavaScript

AmitDiwan
Updated on 14-Oct-2020 08:05:15

117 Views

We are required to write a JavaScript function that takes in Number arrays and returns the element from arrays that are not common to both.For example, if the two arrays are −const arr1 = [2, 4, 2, 4, 6, 4, 3]; const arr2 = [4, 2, 5, 12, 4, 1, 3, 34];OutputThen the output should be −const output = [ 6, 5, 12, 1, 34 ]ExampleThe code for this will be −const arr1 = [2, 4, 2, 4, 6, 4, 3]; const arr2 = [4, 2, 5, 12, 4, 1, 3, 34]; const deviations = (first, second) => {   ... Read More

Special type of numbers (pronic) in JavaScript

AmitDiwan
Updated on 14-Oct-2020 08:03:13

144 Views

We are required to write a JavaScript function that takes in a number and returns true if it is a Pronic number otherwise returns false.A Pronic number is a number which is the product of two consecutive integers, that is, a number of the form −n(n + 1)ExampleThe code for this will be −const num = 132; const isPronic = num => {    let nearestSqrt = Math.floor(Math.sqrt(num)) - 1;    while(nearestSqrt * (nearestSqrt + 1)

Changing positivity/negativity of Numbers in a list in JavaScript

AmitDiwan
Updated on 14-Oct-2020 07:58:14

63 Views

We are required to write a JavaScript function that takes in an array of positive as well as negative Numbers and changes the positive numbers to corresponding negative numbers and the negative numbers to corresponding positive numbers in place.ExampleThe code for this will be −const arr = [12, 5, 3, -1, 54, -43, -2, 34, -1, 4, -4]; const changeSign = arr => {    arr.forEach((el, ind) => {       arr[ind] *= -1;    }); }; changeSign(arr); console.log(arr);OutputThe output in the console −[    -12, -5, -3, 1, -54,    43, 2, -34, 1, -4,    4 ]

Swapping adjacent words of a String in JavaScript

AmitDiwan
Updated on 14-Oct-2020 07:56:39

491 Views

We are required to write a JavaScript function that takes in a string and swaps the adjacent words of that string with one another until the end of that string.ExampleThe code for this will be −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;       }       acc += ((arr[ind+1] || "") + " " + val + " ");       return acc;    }, ""); }; console.log(replaceWords(str));OutputThe output in the console −is This sample a only string

Finding two golden numbers in JavaScript

AmitDiwan
Updated on 14-Oct-2020 07:54:43

148 Views

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 than our function should return false.ExampleThe code for this will be −const goldenNumbers = (sum, prod) => {    for(let i = 0; i < (sum / 2); i++){       if(i * (sum-i) !== prod){          continue;       };       return [i, (sum-i)];    };    return false; }; console.log(goldenNumbers(24, 144)); console.log(goldenNumbers(14, 45)); console.log(goldenNumbers(21, 98));OutputThe output in the console −false [ 5, 9 ] [ 7, 14 ]

Delete every one of the two strings that start with the same letter in JavaScript

AmitDiwan
Updated on 14-Oct-2020 07:53:32

137 Views

We are required to write a JavaScript function that takes in an array of strings and deletes every one of the two strings that start with the same letter.For example, If the actual array is −const arr = ['Apple', 'Jack' , 'Army', 'Car', 'Jason'];Then we have to delete and keep only one string in the array distinct letters, so one of the two strings starting with A should get deleted and the same should the one with J.ExampleThe code for this will be −const arr = ['Apple', 'Jack' , 'Army', 'Car', 'Jason']; const delelteSameLetterWord = arr => {    const ... Read More

Omitting false values while constructing string in JavaScript

AmitDiwan
Updated on 14-Oct-2020 07:51:58

91 Views

We have an array that contains some string values as well as some false values.We are required to write a JavaScript function that takes in this array and returns a string constructed by joining values of the array and omitting false values.ExampleThe code for this will be −const arr = ["Here", "is", null, "an", undefined, "example", 0, "", "of", "a", null, "sentence"]; const joinArray = arr => {    const sentence = arr.reduce((acc, val) => {       return acc + (val || "");    }, "");    return sentence; }; console.log(joinArray(arr));OutputThe output in the console −Hereisanexampleofasentence

Negative number digit sum in JavaScript

AmitDiwan
Updated on 14-Oct-2020 07:50:34

565 Views

We are required to write a JavaScript function that takes in a negative integer and returns the sum of its digits.For example: If the number is −-5456OutputThen the output should be −5+4+5+6 10ExampleThe code for this will be −const num = -5456; 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));OutputThe output in the console −10

Sum all duplicate values in array in JavaScript

AmitDiwan
Updated on 14-Oct-2020 07:48:31

2K+ Views

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 index.ExampleThe code for this will be −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{          acc.set(val, 1);       };       return acc;    }, new Map());    return Array.from(map, el => el[0] * el[1]); }; console.log(sumDuplicate(input));OutputThe output in the console −[ 2, 9, 10, 7, 4 ]

Difference of digits at alternate indices in JavaScript

AmitDiwan
Updated on 14-Oct-2020 07:45:31

102 Views

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 place.ExampleThe code for this will be −const num = 123456; const alternateDifference = (num, res = 0, ind = 0) => {    if(num){       if(ind % 2 === 0){          res += num % 10;       }else{          res -= num % 10;       };       return alternateDifference(Math.floor(num / 10), res, ++ind);    };    return Math.abs(res); }; console.log(alternateDifference(num));OutputThe output in the console −3

Advertisements