Javascript Articles

Page 374 of 534

Subarrays product sum in JavaScript

AmitDiwan
AmitDiwan
Updated on 09-Oct-2020 175 Views

We are required to write a JavaScript function that takes in an array of numbers of length N such that N is a positive even integer and divides the array into two sub arrays (say, left and right) containing N/2 elements each.The function should do the product of the subarrays and then add both the results thus obtained.For example, If the input array is −const arr = [1, 2, 3, 4, 5, 6]Then the output should be −(1*2*3) + (4*5*6) 6+120 126The code for this will be −const arr = [1, 2, 3, 4, 5, 6] const subArrayProduct = arr ...

Read More

Sum of all the non-repeating elements of an array JavaScript

AmitDiwan
AmitDiwan
Updated on 09-Oct-2020 485 Views

Suppose, we have an array of numbers like this −const arr = [14, 54, 23, 14, 24, 33, 44, 54, 77, 87, 77, 14];We are required to write a JavaScript function that takes in one such array and counts the sum of all the elements of the array that appear only once in the array −For example:The output for the array mentioned above will be −356The code for this will be −const arr = [14, 54, 23, 14, 24, 33, 44, 54, 77, 87, 77, 14]; const nonRepeatingSum = arr => {    let res = 0;    for(let i = 0; i < arr.length; i++){       if(i !== arr.lastIndexOf(arr[i])){          continue;       };       res += arr[i];    };    return res; }; console.log(nonRepeatingSum(arr));Following is the output on console −30

Read More

Finding smallest number using recursion in JavaScript

AmitDiwan
AmitDiwan
Updated on 09-Oct-2020 223 Views

We are required to write a JavaScript function that takes in an array of Numbers and returns the smallest number from it using recursion.Let’s say the following are our arrays −const arr1 = [-2, -3, -4, -5, -6, -7, -8]; const arr2 = [-2, 5, 3, 0];The code for this will be −const arr1 = [-2, -3, -4, -5, -6, -7, -8]; const arr2 = [-2, 5, 3, 0]; const min = arr => {    const helper = (a, ...res) => {       if (!res.length){          return a;       };     ...

Read More

JavaScript Get English count number

AmitDiwan
AmitDiwan
Updated on 09-Oct-2020 240 Views

We are required to write a JavaScript function that takes in a number and returns an English count number for it.For example 3 returns 3rdThe code for this will be −const num = 3; const englishCount = num => {    if (num % 10 === 1 && num % 100 !== 11){       return num + "st";    };    if (num % 10 === 2 && num % 100 !== 12) {       return num + "nd";    };    if (num % 10 === 3 && num % 100 !== 13) {       return num + "rd";    };    return num + "th"; }; console.log(englishCount(num)); console.log(englishCount(111)); console.log(englishCount(65)); console.log(englishCount(767));Following is the output on console −3rd 111th 65th 767th

Read More

How to find capitalized words and add a character before that in a given sentence using JavaScript?

AmitDiwan
AmitDiwan
Updated on 09-Oct-2020 214 Views

Suppose, we have a string that contains some capitalized English alphabets like this −const str = "Connecting to server Connection has been successful We found result";We are required to write a JavaScript function that takes in one such string and inserts a comma ', ' before the space before every capital letter in the string.The code for this will be −const str = "Connecting to server Connection has been successful We found result"; const capitaliseNew = str => {    let newStr = '';    const regex = new RegExp(/.[A-Z]/g);    newStr = str.replace(regex, ', $&');    return newStr; }; ...

Read More

Remove smallest number in Array JavaScript

AmitDiwan
AmitDiwan
Updated on 09-Oct-2020 692 Views

We are required to write a JavaScript function that takes in an array of numbers. The number should find the smallest element in the array and remove it in place.The code for this will be −const arr = [2, 1, 3, 2, 4, 5, 1]; const removeSmallest = arr => {    const smallestCreds = arr.reduce((acc, val, index) => {       let { num, ind } = acc;       if(val >= num){          return acc;       };       ind = index;       num = val;       return { ind, num };    }, {       num: Infinity,       ind: -1    });    const { ind } = smallestCreds;    if(ind === -1){       return;    };    arr.splice(ind, 1); }; removeSmallest(arr); console.log(arr);Following is the output on console −[ 2, 3, 2, 4, 5, 1 ]

Read More

Convert number to a reversed array of digits in JavaScript

AmitDiwan
AmitDiwan
Updated on 09-Oct-2020 209 Views

Given a non-negative integer, we are required to write a function that returns an array containing a list of independent digits in reverse order.For example:348597 => The correct solution should be [7,9,5,8,4,3]The code for this will be −const num = 348597; const reverseArrify = num => {    const numArr = String(num).split('');    const reversed = [];    for(let i = numArr.length - 1; i >= 0; i--){       reversed[i] = +numArr.shift();    };    return reversed; }; console.log(reverseArrify(num));Following is the output on console −[ 7, 9, 5, 8, 4, 3 ]

Read More

Distance between 2 duplicate numbers in an array JavaScript

AmitDiwan
AmitDiwan
Updated on 09-Oct-2020 197 Views

We are required to write a JavaScript function that takes in an array of numbers that contains at least one duplicate pair of numbers.Our function should return the distance between all the duplicate pairs of numbers that exist in the array.The code for this will be −const arr = [2, 3, 4, 2, 5, 4, 1, 3]; const findDistance = arr => {    var map = {}, res = {};    arr.forEach((el, ind) => {       map[el] = map[el] || [];       map[el].push(ind);    });    Object.keys(map).forEach(el => {       if (map[el].length > ...

Read More

Hyphen string to camelCase string in JavaScript

AmitDiwan
AmitDiwan
Updated on 09-Oct-2020 227 Views

Suppose, we have a string that contains words separated by hyphens like this −const str = 'this-is-an-example';We are required to write a JavaScript function that takes in one such string and converts it into a camelCase string.For the above string, the output should be −const output = 'thisIsAnExample';The code for this will be −const str = 'this-is-an-example'; const changeToCamel = str => {    let newStr = '';    newStr = str    .split('-')    .map((el, ind) => {       return ind && el.length ? el[0].toUpperCase() + el.substring(1)       : el;    })    .join('');    return newStr; }; console.log(changeToCamel(str));Following is the output on console −thisIsAnExample

Read More

Finding closest pair sum of numbers to a given number in JavaScript

AmitDiwan
AmitDiwan
Updated on 09-Oct-2020 308 Views

We are required to write a JavaScript function that takes in an array of Numbers as the first argument and a Number as the second argument.The function should return an array of two numbers from the original array whose sum is closest to the number provided as the second argument.The code for this will be −const arr = [1, 2, 3, 4, 5, 6, 7]; const num = 14; const closestPair = (arr, sum) => {    let first = 0, second = 0;    for(let i in arr) {       for(let j in arr) {     ...

Read More
Showing 3731–3740 of 5,338 articles
« Prev 1 372 373 374 375 376 534 Next »
Advertisements