Found 10710 Articles for Web Development

Creating all possible unique permutations of a string in JavaScript

AmitDiwan
Updated on 17-Apr-2021 11:39:18

1K+ Views

ProblemWe are required to write a JavaScript function that takes in a string str. Our function should create all permutations of the input string and remove duplicates, if present. This means, we have to shuffle all letters from the input in all possible orders.ExampleFollowing is the code − Live Democonst str = 'aabb'; const permute = (str = '') => {    if (!!str.length && str.length < 2 ){       return str    }    const arr = [];    for (let i = 0; i < str.length; i++){       let char = str[i]       ... Read More

Returning number with increasing digits. in JavaScript

AmitDiwan
Updated on 17-Apr-2021 11:37:15

147 Views

ProblemWe are required to write a JavaScript function that takes in a number n. Our function should return it with its digits in descending order. Essentially, we should rearrange the digits to create the highest possible number.ExampleFollowing is the code − Live Democonst num = 5423267; const arrangeInDescending = (num = 1) => {    const str = String(num);    const arr = str.split('');    arr.sort((a, b) => {       return +b - +a;    });    const newStr = arr.join('');    const res = Number(newStr);    return res; }; console.log(arrangeInDescending(num));OutputFollowing is the console output −7654322

Currified function that multiples array elements in JavaScript

AmitDiwan
Updated on 17-Apr-2021 11:35:29

148 Views

ProblemWe are required to write a JavaScript function that takes in an array and returns another function which in turn takes in a number which returns a new array which is the product of corresponding elements of the input array to the first function and the number provided to the second function.ExampleFollowing is the code − Live Democonst arr = [2, 5, 2, 7, 8, 4]; const num = 4; const produceWith = (arr = []) => (num) => {    const res = arr.map(el => {       return el * num;    });    return res; }; console.log(produceWith(arr)(num));OutputFollowing is the console output −[ 8, 20, 8, 28, 32, 16 ]

Returning the expanded form of a number in JavaScript

AmitDiwan
Updated on 17-Apr-2021 11:33:22

278 Views

ProblemWe are required to write a JavaScript function that takes in a number and returns a string of the expanded form of the number, indicating the place value of each number.ExampleFollowing is the code − Live Democonst num = 56577; const expandedForm = (num = 0) => {    const str = String(num);    let res = '';    let multiplier = Math.pow(10, str.length - 1);    for(let i = 0; i < str.length; i++){       const el = +str[i];       const next = +str[i + 1];       if(el){          res += (el * multiplier);       };       if(next){          res += ' + ';       };       multiplier /= 10;    };    return res; }; console.log(expandedForm(num));OutputFollowing is the console output −50000 + 6000 + 500 + 70 + 7

Building an array of specific size with consecutive element sum being perfect square in JavaScript

AmitDiwan
Updated on 17-Apr-2021 11:29:55

77 Views

We are required to write a JavaScript function that takes in a number n. Our function should return an array of integers 1..n arranged in a way, such that the sum of each 2 consecutive numbers is a square.ExampleThe code for this will be − Live Democonst n = 15; const buildSquaresArray = (n = 1, res = []) => {    const helper = (res, set, n) => {       if(set.size === n){          return true;       };       for(let i = 1; i

Accumulating some value over using a callback function and initial value in JavaScript

AmitDiwan
Updated on 17-Apr-2021 11:25:53

157 Views

ProblemWe are required to write a JavaScript function that takes in an array a callback function and an initial value.The function should accumulate a value over the iteration of array and finally return the value just like the Array.prototype.reduce() does.ExampleFollowing is the code − Live Democonst arr = [1, 2, 3, 4, 5]; const sum = (a, b) => a + b; Array.prototype.customReduce = function(callback, initial){    if(!initial){       initial = this[0];    };    let res = initial;    for(let i = initial === this[0] ? 1 : 0; i < this.length; i++){       res = callback(res, this[i]);    };    return res; }; console.log(arr.customReduce(sum, 0));OutputFollowing is the console output −15

Finding nth day of the year in leap and non-leap years in JavaScript

AmitDiwan
Updated on 17-Apr-2021 11:24:23

220 Views

ProblemWe are required to write a JavaScript function that takes in a number as the first argument and boolean as the second argument.The boolean specifies a leap year (if it’s true). Based on this information our function should return the date that would fall on the nth day of the year.ExampleFollowing is the code − Live Democonst day = 60; const isLeap = true; const findDate = (day = 1, isLeap = false) => {    if(day > 366){       return undefined;    };    const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', ... Read More

Swapping string case using a binary number in JavaScript

AmitDiwan
Updated on 17-Apr-2021 11:21:37

132 Views

ProblemWe are required to write a JavaScript function that takes in a string str and a number n. Our function should change the given string str using n.Each bit in n will specify whether or not to swap the case for each alphabetic character in s −If the bit is 1, swap the case; if its 0, leave it as is. When we are finished with the last bit of n, start again with the first bit.And finally, we should return the new string thus formed.ExampleFollowing is the code − Live Democonst str = 'hey there'; const num = 21; const ... Read More

Building a lexicographically increasing sequence of first n natural numbers in JavaScript

AmitDiwan
Updated on 17-Apr-2021 11:19:36

65 Views

ProblemWe are required to write a JavaScript function that takes in a number n and return an array containing first n natural number.The only condition is that the numbers should be sorted lexicographically which means all the numbers starting with 1 should come before any starting with 2 or 3 or 4 and so on.ExampleFollowing is the code − Live Democonst num = 24; const buildLexicographically = (num = 1) => {    const res = [];    const curr = num >= 9 ? 9 : num;    for (let i = 1; i

Reversing words present in a string in JavaScript

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

322 Views

ProblemWe are required to write a JavaScript function that takes in a string that represents a sentence.Our function should reverse the order of words present in the string and return the new string.It means that the last word should become the first, second last should be the second and so on.ExampleFollowing is the code − Live Democonst str = 'this is some random string text'; const reverseWords = (str = '') => {    const strArr = str.split(' ');    strArr.reverse();    const reversedStr = strArr.join(' ');    return reversedStr; }; console.log(reverseWords(str));OutputFollowing is the console output −text string random some is ... Read More

Advertisements