Javascript Articles

Page 250 of 534

Creating a binary spiral array of specific size in JavaScript

AmitDiwan
AmitDiwan
Updated on 17-Apr-2021 221 Views

ProblemWe are required to write a JavaScript function that takes in a number n. Our function should construct and return an array of N * N order (2-D array), in which the 1s take all the spiralling positions starting from [0, 0] and all the 0s take non-spiralling positions.Therefore, for n = 5, the output will look like −[    [ 1, 1, 1, 1, 1 ],    [ 0, 0, 0, 0, 1 ],    [ 1, 1, 1, 0, 1 ],    [ 1, 0, 0, 0, 1 ],    [ 1, 1, 1, 1, 1 ] ]ExampleFollowing ...

Read More

Preparing numbers from jumbled number names in JavaScript

AmitDiwan
AmitDiwan
Updated on 17-Apr-2021 191 Views

ProblemSuppose the following number name string −const str = 'TOWNE';If we rearrange this string, we can find two number names in it 2 (TWO) and 1 (ONE).Therefore, we expect an output of 21We are required to write a JavaScript function that takes in one such string and returns the numbers present in the string.ExampleFollowing is the code − Live Democonst str = 'TOWNE'; const findNumber = (str = '') => {    function stringPermutations(str) {       const res = [];       if (str.length == 1) return [str];       if (str.length == 2) return [str, str[1]+str[0]]; ...

Read More

Sum of all positives present in an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 17-Apr-2021 2K+ Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers (positive and negative). Our function should calculate and return the sum of all the positive numbers present in the array.ExampleFollowing is the code − Live Democonst arr = [5, -5, -3, -5, -7, -8, 1, 9]; const sumPositives = (arr = []) => {    const isPositive = num => typeof num === 'number' && num > 0;    const res = arr.reduce((acc, val) => {       if(isPositive(val)){          acc += val;       };       return acc;    }, 0);    return res; }; console.log(sumPositives(arr));OutputFollowing is the console output −15

Read More

Even index sum in JavaScript

AmitDiwan
AmitDiwan
Updated on 17-Apr-2021 638 Views

ProblemWe are required to write a JavaScript function that takes in an array of integers. Our function should return the sum of all the integers that have an even index, multiplied by the integer at the last index.const arr = [4, 1, 6, 8, 3, 9];Expected output −const output = 117;ExampleFollowing is the code − Live Democonst arr = [4, 1, 6, 8, 3, 9]; const evenLast = (arr = []) => {    if (arr.length === 0) {       return 0    } else {       const sub = arr.filter((_, index) => index%2===0)       const sum = sub.reduce((a,b) => a+b)       const posEl = arr[arr.length -1]       const res = sum*posEl       return res    } } console.log(evenLast(arr));OutputFollowing is the console output −117

Read More

Separating data type from array into groups in JavaScript

AmitDiwan
AmitDiwan
Updated on 17-Apr-2021 627 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

Mathematics summation function in JavaScript

AmitDiwan
AmitDiwan
Updated on 17-Apr-2021 2K+ 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

Read More

Creating all possible unique permutations of a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 17-Apr-2021 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
AmitDiwan
Updated on 17-Apr-2021 200 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

Read More

Currified function that multiples array elements in JavaScript

AmitDiwan
AmitDiwan
Updated on 17-Apr-2021 249 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 ]

Read More

Returning the expanded form of a number in JavaScript

AmitDiwan
AmitDiwan
Updated on 17-Apr-2021 407 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

Read More
Showing 2491–2500 of 5,338 articles
« Prev 1 248 249 250 251 252 534 Next »
Advertisements