Found 6686 Articles for Javascript

Equality of corresponding elements in JavaScript

AmitDiwan
Updated on 14-Oct-2020 08:19:50

134 Views

We are required to write a JavaScript function that takes in two arrays of literals. The function should check the corresponding elements of the array. The function should return true if all the corresponding elements of the array are equal otherwise it should return false.ExampleThe code for this will be −const arr1 = [6, 7, 8, 9, 10, 11, 12, 14]; const arr2 = [6, 7, 8, 9, 10, 11, 12, 14]; const areEqual = (first, second) => {    if(first.length !== second.length){       return false;    };    for(let i = 0; i < first.length; i++){       if(first[i] === second[i]){          continue;       }       return false;    };    return true; }; console.log(areEqual(arr1, arr2));OutputThe output in the console −True

Splitting a string into parts in JavaScript

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

252 Views

We are required to write a JavaScript function that takes in a string and a number n (such that n exactly divides the length of string) and we need to return an array of string of length n containing n equal parts of the string.ExampleThe code for this will be −const str = 'we will be splitting this string into parts'; const num = 6; const divideEqual = (str, num) => {    const len = str.length / num;    const creds = str.split("").reduce((acc, val) => {       let { res, currInd } = acc;       ... Read More

Find the second most frequent element in array JavaScript

AmitDiwan
Updated on 14-Oct-2020 08:16:41

444 Views

We are required to write a JavaScript function that takes in a string and returns the character from the string that appears for the second most number of times.ExampleThe code for this will be −const arr = [5, 2, 6, 7, 54, 3, 2, 2, 5, 6, 7, 5, 3, 5, 3, 4]; const secondMostFrequent = 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);    const frequencyArray = Array.from(map);    return frequencyArray.sort((a, b) => {       return b[1] - a[1];    })[1][0]; }; console.log(secondMostFrequent(arr));OutputThe output in the console −2

Product of numbers present in a nested array in JavaScript

AmitDiwan
Updated on 14-Oct-2020 08:14:52

180 Views

We are required to write a JavaScript function that takes in an array of nested arrays of Numbers and some falsy values (including 0) and some strings as well and the function should return the product of number values present in the nested array. If the array contains some 0s, we should ignore them as well.ExampleThe code for this will be −const arr = [    1, 2, null, [       2, 5, null, undefined, false, 5, [          1, 3, false, 0, 2       ], 4, false    ], 4, 6, 0 ... Read More

Number of times a string appears in another JavaScript

AmitDiwan
Updated on 14-Oct-2020 08:07:16

159 Views

We are required to write a JavaScript function that takes in two strings and returns the count of the number of times the str1 appears in the str2.ExampleThe code for this will be −const main = 'This is the is main is string'; const sub = 'is'; const countAppearances = (main, sub) => {    const regex = new RegExp(sub, "g");    let count = 0;    main.replace(regex, (a, b) => {       count++;    });    return count; }; console.log(countAppearances(main, sub));OutputThe output in the console −4

Finding deviations in two Number arrays in JavaScript

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

67 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

83 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

39 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

374 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

98 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 ]

Advertisements