Finding Persistence of Number in JavaScript

AmitDiwan
Updated on 16-Sep-2020 10:25:50

364 Views

We are required to write a JavaScript function that takes in an positive integer and returns its additive persistenceThe additive persistence of an integer, say n, is the number of times we have to replace the number with the sum of its digits until the number becomes a single digit integer.For example −If the number is −1679583Then, 1 + 6 + 7 + 9 + 5 + 8 + 3 = 39   // 1 Pass 3 + 9 = 12                   // 2 Pass 1 + 2 = 3     ... Read More

Can Array Form Consecutive Sequence in JavaScript

AmitDiwan
Updated on 16-Sep-2020 10:24:28

214 Views

We are required to write a JavaScript function that takes in an array of numbers and checks if the elements of the array can be rearranged to form a sequence of numbers or not.For example −If the array is −const arr = [3, 1, 4, 2, 5];Then the output should be −trueExampleFollowing is the code −const arr = [3, 1, 4, 2, 5]; const canBeConsecutive = (arr = []) => {    if(!arr.length){       return false;    };    const copy = arr.slice();    copy.sort((a, b) => a - b);    for(let i = copy[0], j = 0; ... Read More

Checking Smooth Sentences in JavaScript

AmitDiwan
Updated on 16-Sep-2020 10:23:17

278 Views

We are required to write a JavaScript function that checks whether a sentence is smooth or not. A sentence is smooth when the first letter of each word in the sentence is same as the last letter of its preceding word.ExampleFollowing is the code −const str = 'this stringt tries sto obe esmooth'; const str2 = 'this string is not smooth'; const isSmooth = str => {    const strArr = str.split(' ');    for(let i = 0; i < strArr.length; i++){       if(!strArr[i+1] || strArr[i][strArr[i].length -1] === strArr[i+1]       [0]){          continue; ... Read More

Strictly Increasing or Decreasing Array in JavaScript

AmitDiwan
Updated on 16-Sep-2020 10:21:57

428 Views

In Mathematics, a strictly increasing function is that function in which the value to be plotted always increase. Similarly, a strictly decreasing function is that function in which the value to be plotted always decrease.We are required to write a JavaScript function that takes in an array of numbers and returns true if it’s either strictly increasing or strictly decreasing, otherwise returns false.ExampleFollowing is the code −const arr = [12, 45, 6, 4, 23, 23, 21, 1]; const arr2 = [12, 45, 67, 89, 123, 144, 2656, 5657]; const sameSlope = (a, b, c) => (b - a < 0 && c - b < 0) || (b - a > 0 && c - b > 0); const increasingOrDecreasing = (arr = []) => {    if(arr.length

Determining Rightness of a Triangle in JavaScript

AmitDiwan
Updated on 16-Sep-2020 10:20:47

1K+ Views

We are required to write a JavaScript function that takes in three numbers say a, b and c representing the length of three sides of a triangle. The function should return true if those three sides represent a right-angle triangle, false otherwise.Right angle triangleA triangle is a right-angle triangle if one of the three angles in the triangle is 90 degree. And one angle in the triangle is 90 degrees when the square of the longest side is equal to the sum of squares of the other two sides.For example − 3, 4, 5, as3*3 + 4*4 = 5*5 = ... Read More

Find the Smallest N-Digit Number or Greater in JavaScript

AmitDiwan
Updated on 16-Sep-2020 10:18:58

399 Views

We are required to write a JavaScript function that takes in a number as the first argument, say n, and an array of numbers as the second argument. The function should return the smallest n digit number which is a multiple of all the elements specified in the array. If there exist no such n digit element then we should return the smallest such element.For example: If the array is −const arr = [12, 4, 5, 10, 9]For both n = 2 and n = 3, the output should be 180ExampleFollowing is the code −const arr = [12, 4, 5, ... Read More

Find the Sum of Fractions in JavaScript

AmitDiwan
Updated on 16-Sep-2020 10:16:53

691 Views

We have an array of arrays like this −const arr = [[12, 56], [3, 45], [23, 2], [2, 6], [2, 8]];Note that while the array can have any number of elements, each subarray should strictly contain two numbers.The two numbers in each subarray represents a fraction. For example, the fraction represented by the first subarray is 12/56, by the second is 3/45 and so on.We are required to write a JavaScript function that takes in one such array and calculates the sum of fractions represented by all the subarrays. Calculate the sum in fraction form (i.e., without converting them to ... Read More

Digit Distance of Two Numbers in JavaScript

AmitDiwan
Updated on 16-Sep-2020 10:15:44

347 Views

We are required to write a JavaScript function that takes in two numbers a and b returns their digit distance.Digit distanceThe digit distance of two numbers is the absolute sum of the difference between their corresponding digits.For example: If the numbers are −345 678Then the digit distance will be −|3-6| + |4-7| + |5-8| = 3 + 3 + 3 = 9ExampleFollowing is the code −const num1 = 345; const num2 = 678; const digitDistance = (a, b) => {    const aStr = String(a);    const bStr = String(b);    let diff = 0;    for(let i = 0; ... Read More

Sum of Prime Numbers Between a Range in JavaScript

AmitDiwan
Updated on 16-Sep-2020 10:14:44

525 Views

We are required to write a JavaScript function that takes in two numbers, say a and b and returns the sum of all the prime numbers that fall between a and b. We should include a and b if they are prime as well.ExampleFollowing is the code −const num1 = 45; const num2 = 345; const isPrime = n => {    if (n===1){       return false;    }else if(n === 2){       return true;    }else{       for(let x = 2; x < n; x++){          if(n % x === 0){             return false;          }       }       return true;    }; }; const primeBetween = (a, b) => {    const res = [];    while(a

Finding Unique String in an Array in JavaScript

AmitDiwan
Updated on 16-Sep-2020 10:12:05

421 Views

Suppose, we have the following array of strings that might contain duplicate characters −const arr = ['54gdgdfe3', '434ffd', '43frdf', '43fdhnh', 'wgcxhjny', 'fsdf34'];We are required to write a JavaScript function that takes in one such array and returns the very first element from the array that contains 0 duplicate characters.If there does not exist any such string, we should return false.ExampleFollowing is the code −const arr = ['54gdgdfe3', '434ffd', '43frdf', '43fdhnh', 'wgcxhjny', 'fsdf34']; const isUnique = str => {    return str.split('').every(el => str.indexOf(el) ===    str.lastIndexOf(el)); }; const findUniqueString = arr => {    for(let i = 0; i < ... Read More

Advertisements