Found 10710 Articles for Web Development

Applying a custom function to each corresponding element of two arrays using JavaScript

AmitDiwan
Updated on 20-Apr-2021 09:20:40

121 Views

ProblemWe are required to write a JavaScript function that takes in a callback function (which takes in two arguments and returns a value) as the first argument and two arrays of essentially the same length as the second and third argument.Our function should construct and return a new array whose each corresponding element is the return value of the callback function if corresponding numbers of the input array are provided to it.ExampleFollowing is the code − Live Democonst arr1 = [1, 2, 3, 4]; const arr2 = [5, 6, 7, 8]; const add = (a, b) => a + b; const ... Read More

Finding the count of total upside down numbers in a range using JavaScript

AmitDiwan
Updated on 20-Apr-2021 09:18:06

139 Views

Upside Down NumbersThe numbers that remain the same when they’re rotated by 180 degrees are called upside down numbers.For instance, 9116, 69.ProblemWe are required to write a JavaScript function that takes in a range array of two numbers. Our function should return the count of all the upside down numbers that fall in the specified range.ExampleFollowing is the code − Live Democonst range = [5, 125]; const flipNum = (number) => {    const upsideDownDigits = [0, 1, -99, -99, -99, -99, 9, -99, 8, 6];    let reverseNumArr = String(number)       .split('')       .map(val => Number(val)) ... Read More

Finding the longest consecutive appearance of a character in another string using JavaScript

AmitDiwan
Updated on 20-Apr-2021 09:17:43

177 Views

ProblemWe are required to write a JavaScript function that takes in a string as the first argument and a single character as the second argument.Our function should count and return the longest consecutive appearance of the the character in the string.ExampleFollowing is the code − Live Democonst str = 'abcdaaadse'; const char = 'a'; const countChars = (str = '', char = '') => {    const arr = str.split('');    let c = 0, max = 0;    for (let i = 0; i max){             max = c;          };       }else{          if(c > max){             max = c;          };          c = 0;       };    };    return max; }; console.log(countChars(str, char));Output3

Count of pairs in an array that have consecutive numbers using JavaScript

AmitDiwan
Updated on 20-Apr-2021 09:17:20

349 Views

ProblemWe are required to write a JavaScript function that takes in an array of integers. Our function should return the count of such contagious pairs from the array that have consecutive numbers in them.ExampleFollowing is the code − Live Democonst arr = [1, 2, 5, 8, -4, -3, 7, 6, 5]; const countPairs = (arr = []) => {    let count = 0;    for (var i=0; i

All ways to divide array of strings into parts in JavaScript

AmitDiwan
Updated on 20-Apr-2021 09:17:01

188 Views

ProblemWe are required to write a JavaScript function that takes in an array of literals of at least two elements.Our function should return all the ways to divide the array into two non-empty parts.For instance −For the array ["az", "toto", "picaro", "zone", "kiwi"]The possibilities are −"(az, toto picaro zone kiwi)(az toto, picaro zone kiwi)(az toto picaro, zone kiwi)(az toto picaro zone, kiwi)"ExampleFollowing is the code − Live Democonst arr = ["az", "toto", "picaro", "zone", "kiwi"]; const findAllPossiblities = (arr = []) => {    let array;    const res = [];    for(let i = 1; i < arr.length; i++){   ... Read More

Calculating value of a sequence based on some input using JavaScript

AmitDiwan
Updated on 20-Apr-2021 09:16:37

66 Views

ProblemWe are required to write a JavaScript function that takes in a number n as the only input. Suppose a sequence u for which,u1 = 0 and u1 = 2Our function should find the of un for which,6unun+1- 5unun+2+un+1un+2 = 0ExampleFollowing is the code − Live Democonst num = 13; const sequenceSum = (num = 1) => {    const res = Math.pow(2, num);    return res; }; console.log(sequenceSum(num));Output8192

Finding the only unique string in an array using JavaScript

Aayush Mohan Sinha
Updated on 04-Aug-2023 09:33:28

520 Views

Unraveling the enigma of finding the solitary unique string within an array using JavaScript holds paramount importance for developers seeking to optimize their code. With the ability to handle complex data structures, JavaScript empowers programmers to solve intricate problems efficiently. In this article, we delve into the intricacies of identifying the lone unique string amidst an array, employing an arsenal of rarely used but indispensable techniques. By mastering the step-by-step approach presented herein, developers will acquire the prowess to sift through arrays, discerning the one string that stands distinct from the rest. Brace yourself for a journey into the depths ... Read More

Summing numbers present in a string separated by spaces using JavaScript

AmitDiwan
Updated on 20-Apr-2021 09:11:08

429 Views

ProblemWe are required to write a JavaScript function that takes in a string which has integers inside it separated by spaces.The task of our function is to convert each integer in the string into an integer and return their sum.ExampleFollowing is the code − Live Democonst str = '1 5 12 76 2'; const sumStringNumbers = (str = '') => {    const findSum = (arr = []) => {       const sum = arr.reduce((acc, val) => acc + val);       return sum;    };    let sum = 0;    const arr = str       .split(' ')       .map(Number);    sum = findSum(arr);    return sum; }; console.log(sumStringNumbers(str));Output96

Alternating sum of elements of a two-dimensional array using JavaScript

AmitDiwan
Updated on 20-Apr-2021 09:10:47

212 Views

ProblemWe are required to write a JavaScript function that takes in a 2-Dimensional array of m X n order of numbers containing the same number of rows and columns.For this array, our function should count and return the following sum−$\sum_{i=1}^m \sum_{j=1}^n (-1)^{i+j}a_{ij}$ExampleFollowing is the code − Live Democonst arr = [    [4, 6, 3],    [1, 8, 7],    [2, 5, 9] ]; const alternateSum = (arr = []) => {    let sum = 0;    for(let i = 0; i < arr.length; i++){       for(let j = 0; j < arr[i].length; j++){          const multiplier = (i + j) % 2 === 0 ? 1 : -1;          sum += (multiplier * arr[i][j]);       };    };    return sum; }; console.log(alternateSum(arr));Output7

Returning the first number that equals its index in an array using JavaScript

AmitDiwan
Updated on 20-Apr-2021 09:09:37

83 Views

ProblemWe are required to write a JavaScript function that takes in an array of number. Our function should return that first number from the array whose value and 0-based index are the same given that there exists at least one such number in the array.ExampleFollowing is the code − Live Democonst arr = [9, 2, 1, 3, 6, 5]; const findFirstSimilar = (arr = []) => {    for(let i = 0; i < arr.length; i++){       const el = arr[i];       if(el === i){          return i;       };    }; }; console.log(findFirstSimilar(arr));Output3

Advertisements