Found 10483 Articles for Web Development

Sum a negative number (negative and positive digits) - JavaScript

AmitDiwan
Updated on 14-Sep-2020 13:51:46

2K+ Views

We are required to write a JavaScript function that takes in a negative integer and returns the sum of its digitsFor example −-234 --> -2 + 3 + 4 = 5 -54  --> -5 + 4 = -1Let’s write the code for this function −ExampleFollowing is the code −const num = -4345; 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));OutputFollowing is the output in the console −8

Sum all duplicate value in array - JavaScript

AmitDiwan
Updated on 14-Sep-2020 13:48:49

448 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 indexFor example −If the input array is −const input = [1, 3, 1, 3, 5, 7, 5, 4];Then the output should be −const output = [2, 6, 7, 10, 4];ExampleLet’s write the code −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{          return acc;       }, new Map()); }    return Array.from(map, el => el[0] * el[1]); }; console.log(sumDuplicate(input));OutputFollowing is the output in the console −[ 2, 9, 10, 7, 4 ]

Find even odd index digit difference - JavaScript

AmitDiwan
Updated on 14-Sep-2020 13:45:44

170 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 placeExampleLet’s write the code −const num = 345336; const evenOddDifference = (num, res = 0, ind = 0) => {    if(num){       if(ind % 2 === 0){          res += num % 10;       }else{          res -= num % 10;       };    };    return Math.abs(res); }; console.log(evenOddDifference(num));OutputFollowing is the output in the console −2

Compare array of objects - JavaScript

AmitDiwan
Updated on 14-Sep-2020 13:43:49

2K+ Views

We have two arrays of objects like these −const blocks = [    { id: 1 },    { id: 2 },    { id: 3 },    { id: 4 }, ] const containers = [    { block: { id: 1 } },    { block: { id: 2 } },    { block: { id: 3 } }, ]We are required to write a function that checks each object of blocks array with the block key of each object of containers array and see if there exists any id in blocks array that is not present in ... Read More

Count groups of negatives numbers in JavaScript

AmitDiwan
Updated on 14-Sep-2020 13:40:56

412 Views

We have an array of numbers like this −const arr = [-1, -2, -1, 0, -1, -2, -1, -2, -1, 0, 1, 0];Let’s say, we are required to write a JavaScript function that counts the consecutive groups of negative numbers in the array.Like here we have consecutive negatives from index 0 to 2 which forms one group and then from 4 to 8 forms the second groupSo, for this array, the function should return 2.ExampleLet’s write the code −const arr = [-1, -2, -1, 0, -1, -2, -1, -2, -1, 0, 1, 0]; const countNegativeGroup = arr => {   ... Read More

Factorial recursion in JavaScript

AmitDiwan
Updated on 14-Sep-2020 13:38:46

303 Views

We are required to write a JavaScript function that computes the Factorial of a number n by making use of recursive approach.Here, we are finding the factorial recursion and creating a custom function recursiceFactorial() −const num = 9; const recursiceFactorial = (num, res = 1) => {    if(num){       return recursiceFactorial(num-1, res * num);    };    return res; };Now, we will call the function and pass the value to find recursion −console.log(recursiceFactorial(num)); console.log(recursiceFactorial(6)); console.log(recursiceFactorial(10));ExampleLet’s write the code for this function −const num = 9; const recursiceFactorial = (num, res = 1) => {    if(num){   ... Read More

JavaScript - Exclude some values in average calculation

AmitDiwan
Updated on 14-Sep-2020 13:36:48

290 Views

Let’s say, we have an array of objects like this −data = [    {"Age":26, "Level":8},    {"Age":37, "Level":9},    {"Age":32, "Level":5},    {"Age":31, "Level":11},    {"Age":null, "Level":15},    {"Age":null, "Level":17},    {"Age":null, "Level":45} ];We are required to write a JavaScript function that calculates the average level for all the objects that have a truthy value for age propertyLet’s write the code for this function −ExampleFollowing is the code −data = [    {"Age":26, "Level":8},    {"Age":37, "Level":9},    {"Age":32, "Level":5},    {"Age":31, "Level":11},    {"Age":null, "Level":15},    {"Age":null, "Level":17},    {"Age":null, "Level":45} ]; const findAverage = arr => { ... Read More

Checking for co-prime numbers - JavaScript

AmitDiwan
Updated on 14-Sep-2020 13:33:39

438 Views

Two numbers are said to be co-primes if there exists no common prime factor amongst them (1 is not a prime number)For example −4 and 5 are co-primes 9 and 14 are co-primes 18 and 35 are co-primes 21 and 57 are not co-prime because they have 3 as the common prime factorWe are required to write a function that takes in two numbers and returns true if they are co-primes otherwise returns falseExampleLet’s write the code for this function −const areCoprimes = (num1, num2) => {    const smaller = num1 > num2 ? num1 : num2;    for(let ... Read More

Finding how many time a specific letter is appearing in the sentence using for loop, break, and continue - JavaScript

AmitDiwan
Updated on 14-Sep-2020 13:00:47

109 Views

We are required to write a JavaScript function that finds how many times a specific letter is appearing in the sentenceExampleLet’s write the code for this function −const string = 'This is just an example string for the program'; const countAppearances = (str, char) => {    let count = 0;    for(let i = 0; i < str.length; i++){    if(str[i] !== char){       // using continue to move to next iteration          continue;       };       // if we reached here it means that str[i] and char are same ... Read More

Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers - JavaScript

AmitDiwan
Updated on 14-Sep-2020 12:58:39

135 Views

We are required to write a JavaScript function that takes in three numbers A, B and N, and finds the total number of N-digit numbers whose sum of digits at even positions and odd positions are divisible by A and B respectively.ExampleLet’s write the code for this function −const indexSum = (num, sumOdd = 0, sumEven = 0, index = 0) => {    if(num){        if(index % 2 === 0){            sumEven += num % 10;        }else{            sumOdd += num % 10;       ... Read More

Advertisements