Counting Divisors of a Number Using JavaScript

AmitDiwan
Updated on 17-Apr-2021 12:36:25

560 Views

ProblemWe are required to write a JavaScript function that takes in a number and returns the count of its divisor.Inputconst num = 30;Outputconst output = 8;Because the divisors are −1, 2, 3, 5, 6, 10, 15, 30ExampleFollowing is the code − Live Democonst num = 30; const countDivisors = (num = 1) => {    if (num === 1) return num       let divArr = [[2, 0]]       let div = divArr[0][0]    while (num > 1) {       if (num % div === 0) {          for (let i = 0; divArr.length; i++) {             if (divArr[i][0] === div) {                divArr[i][1] += 1                break             } else {                if (i === divArr.length - 1) {                   divArr.push([div, 1])                   break                }             }          }          num /= div       } else {          div += 1       }    }    for (let i = 0; i < divArr.length; i++) {       num *= divArr[i][1] + 1    }    return num } console.log(countDivisors(num));Output8

Hours and Minutes from Number of Seconds Using JavaScript

AmitDiwan
Updated on 17-Apr-2021 12:35:56

421 Views

ProblemWe are required to write a JavaScript function that takes in the number of second and return the number of hours and number of minutes contained in those seconds.Inputconst seconds = 3601;Outputconst output = "1 hour(s) and 0 minute(s)";ExampleFollowing is the code − Live Democonst seconds = 3601; const toTime = (seconds = 60) => {    const hR = 3600;    const mR = 60;    let h = parseInt(seconds / hR);    let m = parseInt((seconds - (h * 3600)) / mR);    let res = '';    res += (`${h} hour(s) and ${m} minute(s)`)    return res; }; console.log(toTime(seconds));Output"1 hour(s) and 0 minute(s)"

Validating String with Reference to Array of Words Using JavaScript

AmitDiwan
Updated on 17-Apr-2021 12:35:01

283 Views

ProblemWe are required to write a JavaScript function that takes in a sequence of valid words and a string. Our function should test if the string is made up by one or more words from the array.Inputconst arr = ['love', 'coding', 'i']; const str = 'ilovecoding';Outputconst output = true;Because the string can be formed by the words in the array arr.ExampleFollowing is the code − Live Democonst arr = ['love', 'coding', 'i']; const str = 'ilovecoding'; const validString = (arr = [], str) => {    let arrStr = arr.join('');    arrStr = arrStr    .split('')    .sort()    .join('');   ... Read More

Convert Human Years into Cat Years and Dog Years in JavaScript

AmitDiwan
Updated on 17-Apr-2021 12:34:29

316 Views

ProblemWe are required to write a JavaScript function that takes in human age in years and returns respective dogYears and catYears.Inputconst humanYears = 15;Outputconst output = [ 15, 76, 89 ];ExampleFollowing is the code − Live Democonst humanYears = 15; const humanYearsCatYearsDogYears = (humanYears) => {    let catYears = 0;    let dogYears = 0;    for (let i = 1; i

Deep Count of Elements of an Array Using JavaScript

AmitDiwan
Updated on 17-Apr-2021 12:33:58

2K+ Views

ProblemWe are required to write a JavaScript function that takes in a nested array of element and return the deep count of elements present in the array.Inputconst arr = [1, 2, [3, 4, [5]]];Outputconst output = 7;Because the elements at level 1 are 2, elements at level 2 are 2 and elements at level 3 are 1, Hence the deep count is 7.ExampleFollowing is the code − Live Democonst arr = [1, 2, [3, 4, [5]]]; const deepCount = (arr = []) => {    return arr    .reduce((acc, val) => {       return acc + (Array.isArray(val) ? deepCount(val) ... Read More

Replacing Dots with Dashes in a String Using JavaScript

AmitDiwan
Updated on 17-Apr-2021 12:33:05

504 Views

ProblemWe are required to write a JavaScript function that takes in a string and replaces all appearances of dots(.) in it with dashes(-).inputconst str = 'this.is.an.example.string';Outputconst output = 'this-is-an-example-string';All appearances of dots(.) in string str are replaced with dash(-)ExampleFollowing is the code − Live Democonst str = 'this.is.an.example.string'; const replaceDots = (str = '') => {    let res = "";    const { length: len } = str;    for (let i = 0; i < len; i++) {       const el = str[i];       if(el === '.'){          res += '-';   ... Read More

Isosceles Triangles with Nearest Perimeter Using JavaScript

AmitDiwan
Updated on 17-Apr-2021 12:30:10

192 Views

Almost Isosceles TriangleAn Almost Isosceles Integer Triangle is a triangle that all its side lengths are integers and also, two sides are almost equal, being their absolute difference 1 unit of length.ProblemWe are required to write a JavaScript function that takes in a number which specifies the perimeter of a triangle.Our function should find the measurement of such an almost isosceles triangle whose perimeter is nearest to the input perimeter.For example, if the desired perimeter is 500, Then the almost isosceles triangle with the nearest perimeter will be − [105, 104, 181]ExampleFollowing is the code − Live Democonst perimeter = 500; ... Read More

Maximum Product of Any Two Adjacent Elements in JavaScript

AmitDiwan
Updated on 17-Apr-2021 12:29:43

653 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers.Our function should find the maximum product obtained from multiplying 2 adjacent numbers in the array.ExampleFollowing is the code − Live Democonst arr = [9, 5, 10, 2, 24, -1, -48]; function adjacentElementsProduct(array) {    let maxProduct = array[0] * array[1];    for (let i = 1; i < array.length; i++) {       product = array[i] * array[i + 1];       if (product > maxProduct)          maxProduct = product;    }    return maxProduct; }; console.log(adjacentElementsProduct(arr));Output50

Find Sum of Remaining Numbers to Reach Target Average Using JavaScript

AmitDiwan
Updated on 17-Apr-2021 12:29:21

163 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers and a single number.Our function should find that very number which should be pushed to the array so that its average equals the number specified by the second argument.ExampleFollowing is the code − Live Democonst arr = [4, 20, 25, 17, 9, 11, 15]; const target = 25; function findNumber(arr, target) {    let sum = arr.reduce((a, b) => a + b, 0);    let avg = sum / arr.length;    let next = Math.ceil((target * (arr.length + 1)) - sum);    if (next

Finding Nth Element of an Increasing Sequence Using JavaScript

AmitDiwan
Updated on 17-Apr-2021 12:28:04

240 Views

ProblemConsider an increasing sequence which is defined as follows −The number seq(0) = 1 is the first one in seq.For each x in seq, then y = 2 * x + 1 and z = 3 * x + 1 must be in seq too.There are no other numbers in seq.Therefore, the first few terms of this sequence will be −[1, 3, 4, 7, 9, 10, 13, 15, 19, 21, 22, 27, ...]We are required to write a function that takes in a number n and returns the nth term of this sequence.ExampleFollowing is the code − Live Democonst num = ... Read More

Advertisements