Found 6683 Articles for Javascript

Difference between product and sum of digits of a number in JavaScript

AmitDiwan
Updated on 20-Jan-2021 06:59:11

229 Views

We are required to write a JavaScript function that takes in an positive integer as the only argument.The function should first count the sum of the digits of the number and then their product. Finally, the function should return the absolute difference of the product and the sum.For example −If the input number is −const num = 12345;Then the output should be −const output = 105;ExampleFollowing is the code −const num = 12345; const product = (num, res = 1) => {    if(num){       return product(Math.floor(num / 10), res * (num % 10));    }    return ... Read More

Pair of similar elements at different indices in JavaScript

AmitDiwan
Updated on 20-Jan-2021 06:57:53

63 Views

We are required to write a JavaScript function that takes in an array of integers as the first and the only argument.The function is required to count the number of all such element pairs from the array that are equal in magnitude but are present at different indices.For example −If the input array is −const arr = [7, 9, 5, 7, 7, 5];Then the output should be −const output = 4;because the desired pairs are [7, 7], [7, 7], [7, 7], [5, 5]ExampleFollowing is the code −const arr = [7, 9, 5, 7, 7, 5]; const equalPairCount = (arr = ... Read More

Armstrong number within a range in JavaScript

AmitDiwan
Updated on 20-Jan-2021 06:56:08

316 Views

Armstrong Numbers: A positive integer is called an Armstrong number (of order n) if −abcd... = a^n + b^n + c^n + d^n + ...We are required to write a JavaScript function that takes in an array of exactly two numbers specifying a range.The function should return an array of all the Armstrong numbers that falls in that range (including the start and end numbers if they are Armstrong).We will first separately write a function to detect Armstrong numbers and then iterate through the range to fill the array with desired numbers.ExampleFollowing is the code −const range = [11, 1111]; ... Read More

Splitting array of numbers into two arrays with same average in JavaScript

AmitDiwan
Updated on 20-Jan-2021 06:54:29

159 Views

We are required to write a JavaScript function that takes in an array of integers as the first and the only argument.The function needs to determine whether there exists a combination of elements of the input array that when they are divided into two groups (may/may not have equal elements), the average of both the groups is just the same. If there exists any such condition the function should return true, false otherwise.For example −If the input array is −const arr = [6, 3, 2, 8, 1, 5, 7, 4];Then the output should be −const output = true;because the combination ... Read More

Similar string groups in JavaScript

AmitDiwan
Updated on 20-Jan-2021 06:52:40

299 Views

Two strings str1 and str2 are similar if we can swap two letters (in different positions) of str1, so that it equals str2. Also, two strings str1 and str2 are similar if they are equal.For example, "tars" and "rats" are similar (swapping at positions 0 and 2), and "rats" and "arts" are similar, but "star" is not similar to "tars", "rats", or "arts".Together, these form two connected groups by similarity: {"tars", "rats", "arts"} and {"star"}. Notice that "tars" and "arts" are in the same group even though they are not similar.Formally, each group is such that a word is in ... Read More

Finding missing element in an array of numbers in JavaScript

AmitDiwan
Updated on 20-Jan-2021 06:47:46

893 Views

We are required to write a JavaScript function that takes in an array of numbers of length, say n. The array contains all the integers from 0 to n (including both 0 and n), but just one integer is missing, it can be any number and the array is not sorted. The task of our function is to find the missing number and return it in linear time and constant space.Since the array contains all the numbers from 0 to n but one, we can simply calculate the sum of all the elements of the array in linear time.And then ... Read More

Prime digits sum of a number in JavaScript

AmitDiwan
Updated on 20-Jan-2021 06:46:36

237 Views

We are required to write a JavaScript function that takes in a number as the first and the only argument. The function should then sum all the digits of the number that are prime and return the sum as a number.For example −If the input number is −const num = 67867852;Then the output should be −const output = 21;because 7 + 7 + 5 + 2 = 21 −ExampleFollowing is the code −const num = 67867852; const sumPrimeDigits = (num) => {    const primes = '2357';    let sum = 0;    while(num){       const digit = ... Read More

How to create permutation of array with the given number of elements in JavaScript

AmitDiwan
Updated on 20-Jan-2021 06:45:15

153 Views

We are required to write a JavaScript function that takes in an array of literals as the first argument and a number as the second argument.The function should construct an array of all such arrays which have the length equal to the number specified by the second argument and contains all possible permutations of the elements of the input array.For example −If the input array and the number are −const arr = ['k', 5]; const num = 3;Then the output should be −const output = [    [ 'k', 'k', 'k' ],    [ 'k', 'k', 5 ],    [ ... Read More

Adding numbers represented by string without complete conversion in JavaScript

AmitDiwan
Updated on 20-Jan-2021 06:43:24

663 Views

We are required to write a JavaScript function that takes in two strings, str1 and str2 that represents two numbers.Without converting the whole strings to respective numbers, our function should calculate the sum of those two string numbers and return the result as a string.For example −If the two strings are −const str1 = '234'; const str2 = '129';Then the output should be 363.−ExampleFollowing is the code −const str1 = '234'; const str2 = '129'; const addStringNumbers = (str1, str2) => {    let ind1 = str1.length - 1,    ind2 = str2.length - 1,    res = "",   ... Read More

Finding the first non-repeating character of a string in JavaScript

AmitDiwan
Updated on 20-Jan-2021 06:41:53

376 Views

We are required to write a JavaScript function that takes in a string as the first and the only argument.The function should find and return the index of first character it encounters in the string which appears only once in the string.If the string does not contain any unique character, the function should return -1.For example −If the input string is −const str = 'hellohe';Then the output should be −const output = 4;ExampleFollowing is the code −const str = 'hellohe'; const firstUnique = (str = '') => {    let obj = {};    for(let i = 0; i < ... Read More

Advertisements