Found 10710 Articles for Web Development

Separating data type from array into groups in JavaScript

AmitDiwan
Updated on 17-Apr-2021 11:42:09

446 Views

ProblemWe are required to write a JavaScript function that takes in an array of mixed data types. Our function should return an object that contains data type names as key and their value as array of elements of that specific data type present in the array.ExampleFollowing is the code − Live Democonst arr = [1, 'a', [], '4', 5, 34, true, undefined, null]; const groupDataTypes = (arr = []) => {    const res = {};    for(let i = 0; i < arr.length; i++){       const el = arr[i];       const type = typeof el;   ... Read More

Numbers obtained during checking divisibility by 7 using JavaScript

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

515 Views

ProblemWe can check for a number to be divisible by 7 if it is of the form 10a + b and a - 2b is divisible by 7.We continue to do this until a number known to be divisible by 7 is obtained; we can stop when this number has at most 2 digits because we are supposed to know if a number of at most 2 digits is divisible by 7 or not.We are required to write a JavaScript function that takes in a number and return the number of such steps required to reduce the number to a ... Read More

Counting divisors of a number using JavaScript

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

434 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

329 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)"

Returning reverse array of integers using JavaScript

Aayush Mohan Sinha
Updated on 04-Aug-2023 10:08:41

106 Views

Exploring the intricate realm of JavaScript, one encounters various programming challenges that demand innovative solutions. Among these, the ability to return a reverse array of integers emerges as a fundamental yet significant topic. Understanding how to manipulate arrays in JavaScript is essential for developers seeking to enhance their coding prowess. In this enlightening article, we shall embark on a journey to unravel the intricacies of returning a reverse array of integers using JavaScript. By delving into the depths of this subject matter and employing lesser-known methods, we shall empower developers with the knowledge and skills to conquer array manipulation challenges ... Read More

Validating string with reference to array of words using JavaScript

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

232 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

Mathematics summation function in JavaScript

AmitDiwan
Updated on 17-Apr-2021 11:40:30

1K+ Views

ProblemWe are required to write a JavaScript function that takes in a number n. Our function should return the sum of all the natural numbers from 1 to n including both 1 and nExampleFollowing is the code − Live Democonst num = 34; const summation = (num = 1) => {    let res = 0;    for(let i = 1; i

Converting humanYears into catYears and dogYears in JavaScript

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

240 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

1K+ 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

364 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

Advertisements