Found 10483 Articles for Web Development

Counting the number of letters that occupy their positions in the alphabets for array of strings using JavaScript

AmitDiwan
Updated on 20-Apr-2021 09:48:14

306 Views

ProblemWe are required to write a JavaScript function that takes in an array of strings of english lowercase alphabets.Our function should map the input array to an array whose corresponding elements are the count of the number of characters that had the same 1-based index in the index as their 1-based index in the alphabets.For instance−This count for the string ‘akcle’ will be 3 because the characters ‘a’, ‘c’ and ‘e’ have 1-based index of 1, 3 and 5 respectively both in the string and the english alphabets.ExampleFollowing is the code − Live Democonst arr = ["abode", "ABc", "xyzD"]; const findIndexPairCount ... Read More

Breaking a string into chunks of defined length and removing spaces using JavaScript

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

260 Views

ProblemWe are required to write a JavaScript function that takes in a string sentence that might contain spaces as the first argument and a number as the second argument.Our function should first remove all the spaces from the string and then break the string into a number of chunks specified by the second argument.All the string chunks should have the same length with an exception of last chunk, which might, in some cases, have a different length.ExampleFollowing is the code − Live Democonst num = 5; const str = 'This is an example string'; const splitString = (str = '', num ... Read More

Finding the count of numbers divisible by a number within a range using JavaScript

AmitDiwan
Updated on 20-Apr-2021 09:45:22

356 Views

ProblemWe are required to write a JavaScript function that takes in a range of two integers as the first argument and a number as the second argument.Our function should find all the numbers divisible by the input number in the specified range and return their count.ExampleFollowing is the code − Live Democonst range = [6, 57]; const num = 3; const findDivisibleCount = (num = 1, [l, h]) => {    let count = 0;    for(let i = l; i

Constructing an array of first n multiples of an input number in JavaScript

AmitDiwan
Updated on 20-Apr-2021 09:44:45

372 Views

ProblemWe are required to write a JavaScript function that takes in two numbers, let say m and n.Our function should construct and return an array of first n natural multiples of m.ExampleFollowing is the code − Live Democonst m = 6; const n = 14; const firstNMultiple = (m = 1, n = 1) => {    const res = [];    for(let i = 1; i

Finding the character with longest consecutive repetitions in a string and its length using JavaScript

AmitDiwan
Updated on 20-Apr-2021 09:44:26

923 Views

ProblemWe are required to write a JavaScript function that takes in a string. Our function should return an array of exactly two elements the first element will be characters that makes the most number of consecutive appearances in the string and second will be its number of appearances.ExampleFollowing is the code − Live Democonst str = 'tdfdffddffsdsfffffsdsdsddddd'; const findConsecutiveCount = (str = '') => {    let res='';    let count=1;    let arr = []    for (let i=0;iv

Returning an array containing last n even numbers from input array in JavaScript

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

172 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers as the first argument and a number as the second argument.Our function should pick and return an array of last n even numbers present in the input array.ExampleFollowing is the code − Live Democonst arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; const num = 3; const pickEvens = (arr = [], num = 1) => {    const res = [];    for(let index = arr.length - 1; index >= 0; index -= 1){       if (res.length === num){          break;       };       const number = arr[index];       if (number % 2 === 0){          res.unshift(number);       };    };    return res; }; console.log(pickEvens(arr, num));Output[4, 6, 8]

Finding two numbers given their sum and Highest Common Factor using JavaScript

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

225 Views

ProblemWe are required to write a JavaScript function that takes in two numbers. The first number represents the sum of two numbers and second represents their HCF (GCD or Greatest Common Divisor).Our function should find and return those two numbers.ExampleFollowing is the code − Live Democonst sum = 12; const gcd = 4; const findNumbers = (sum, gcd) => {    const res = [];    if (sum % gcd !== 0){       return -1;    }else{       res.push(gcd);       res.push(sum - gcd);       return res;    }; }; console.log(findNumbers(sum, gcd));Output[4, 8]

Finding array number that have no matching positive or negative number in the array using JavaScript

AmitDiwan
Updated on 20-Apr-2021 09:36:22

173 Views

ProblemWe are required to write a JavaScript function that takes in an array of integers. For each number in the array there will also be its negative or positive compliment present in the array, but for exactly one number, there will be no compliment.Our function should find and return that number from the array.ExampleFollowing is the code − Live Democonst arr = [1, -1, 2, -2, 3]; const findOddNumber = (arr = []) => {    let count = 0;    let number = arr.reduce((total, num) => {       if (num >= 0)          count++       else          count--       return total + num;    }, 0)    return number / Math.abs(count); }; console.log(findOddNumber(arr));Output3

Repeating each character number of times their one based index in a string using JavaScript

AmitDiwan
Updated on 20-Apr-2021 09:42:34

546 Views

ProblemWe are required to write a JavaScript function that takes in a string of english lowercase alphabets.Our function should construct a new string in which each character is repeated the number of times their 1-based index in the string in capital case and different character sets should be separated by dash ‘-’.Therefore, the string ‘abcd’ should become −"A-Bb-Ccc-Dddd"ExampleFollowing is the code − Live Democonst str = 'abcd'; const repeatStrings = (str) => {    const res = [];    for(let i = 0; i < str.length; i++){       const el = str[i];       let temp = el.repeat(i ... Read More

Maximum absolute difference of the length of strings from two arrays in JavaScript

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

275 Views

ProblemWe are required to write a JavaScript function that takes in two arrays, a1 and a2 of strings. Each string is composed with letters from a to z. Let x be any string in the first array and y be any string in the second array.Our function should find the value of −max(abs(length(x) − length(y)))ExampleFollowing is the code − Live Democonst arr1 = ["hoqq", "bbllkw", "oox", "ejjuyyy", "plmiis", "xxxzgpsssa", "xxwwkktt", "znnnnfqknaz", "qqquuhii", "dvvvwz"]; const arr2 = ["cccooommaaqqoxii", "gggqaffhhh", "tttoowwwmmww"]; const findMaxAbsDiff = (arr1 = [], arr2 = []) => {    if(arr1.length === 0 || arr2.length === 0){       ... Read More

Advertisements