Found 6683 Articles for Javascript

Binary subarrays with desired sum in JavaScript

AmitDiwan
Updated on 23-Apr-2021 07:26:00

59 Views

ProblemWe are required to write a JavaScript function that takes in a binary array, arr, as the first argument, and a number, target, as the second argument.Our function is supposed to count the number of subarrays that exists in the array arr, the sum of whose elements is equal to count. We should finally return this count.For example, if the input to the function isInputconst arr = [1, 0, 1, 0, 1]; const target = 2;Outputconst output = 4;Output ExplanationBecause the desired subarrays are:[1, 0, 1][1, 0, 1, 0] [0, 1, 0, 1] [1, 0, 1]Example Live Democonst arr = [1, ... Read More

Finding minimum flips in a binary string using JavaScript

AmitDiwan
Updated on 23-Apr-2021 07:35:10

123 Views

Monotonically Increasing String:A string of '0's and '1's is monotonically increasing if it consists of some number of '0's (possibly 0), followed by some number of '1's (also possibly 0.)ProblemWe are required to write a JavaScript function that takes in a binary string, str, as the first and the only argument.We can flip any ‘0’ to ‘1’ or any ‘1’ to ‘0’ present in the string. Our function should return the minimum number of flips to make S monotonically increasing.For example, if the input to the function isInputconst str = '00110';Outputconst output = 1;Output ExplanationBecause if we flip the last ... Read More

Validating alternating vowels and consonants in JavaScript

AmitDiwan
Updated on 22-Apr-2021 11:57:13

149 Views

ProblemWe are required to write a JavaScript function that takes in a string of English alphabets, str, as the first and the only argument. Our function should return true if and only if the vowels and consonants appear alternatingly in the input string, false otherwise.For example, if the input to the function is −Inputconst str = 'amazon';Outputconst output = true;Output ExplanationBecause the vowels and consonants appear alternatingly in the string ‘amazon’.ExampleFollowing is the code − Live Democonst str = 'amazon'; const appearAlternatingly = (str = '') => {    return str.split('').every((v, i)=>{     if (/[aeiou]/.test(str[0])){       ... Read More

Checking for squared similarly of arrays in JavaScript

AmitDiwan
Updated on 22-Apr-2021 11:49:05

72 Views

ProblemWe are required to write a JavaScript function that takes in two arrays of numbers, arr1 and arr2, as the first and second argument respectively.Our function should return true if and only if every element in arr2 is the square of any element of arr1 irrespective of their order of appearance.For example, if the input to the function is −Inputconst arr1 = [4, 1, 8, 5, 9]; const arr2 = [81, 1, 25, 16, 64];Outputconst output = true;ExampleFollowing is the code − Live Democonst arr1 = [4, 1, 8, 5, 9]; const arr2 = [81, 1, 25, 16, 64]; const isSquared ... Read More

Converting any case to camelCase in JavaScript

AmitDiwan
Updated on 22-Apr-2021 11:46:46

585 Views

ProblemWe are required to write a JavaScript function that takes in a string, str, which can be any case (normal, snake case, pascal case or any other).Our function should convert this string into camelCase string.For example, if the input to the function is −Inputconst str = 'New STRING';Outputconst output = 'newString';ExampleFollowing is the code − Live Democonst str = 'New STRING'; const toCamelCase = (str = '') => {    return str       .replace(/[^a-z0-9]/gi, ' ')       .toLowerCase()       .split(' ')       .map((el, ind) => ind === 0 ? el : el[0].toUpperCase() + el.substring(1, el.length))       .join(''); }; console.log(toCamelCase(str));OutputnewString

Alternatingly combining array elements in JavaScript

AmitDiwan
Updated on 22-Apr-2021 11:45:42

55 Views

ProblemWe are required to write a JavaScript function that takes in any number of arrays of literals as input.Our function should prepare a new array that contains elements picked alternatingly from all the input arrays.For example, if the input to the function is −Inputconst arr1 = [1, 2, 3, 4]; const arr2 = [11, 12, 13, 14]; const arr3 = ['a', 'b', 'c'];Outputconst output = [1, 11, 'a', 2, 12, 'b', 3, 13, 'c', 4, 14];ExampleFollowing is the code − Live Democonst arr1 = [1, 2, 3, 4]; const arr2 = [11, 12, 13, 14]; const arr3 = ['a', 'b', 'c']; ... Read More

Arranging lexicographically and removing whitespaces in JavaScript

AmitDiwan
Updated on 22-Apr-2021 11:42:08

78 Views

ProblemWe are required to write a JavaScript function that takes in a string, str, that contains alphabets and whitespacesOur function should iterate over the input string and perform actions so that the characters are concatenated into a new string in "case-insensitively-alphabetical-order-of-appearance" order. Whitespace and punctuation shall simply be removed!For example, if the input to the function is −Inputconst str = 'some simple letter combination!';Outputconst output = 'abceeeeiiillmmmnnoooprssttt';ExampleFollowing is the code − Live Democonst str = 'some simple letter combination!'; const orderString = (str = '') => {    let res = '';       for(let i = 97; i < ... Read More

Crack Alphabets fight problem in JavaScript

AmitDiwan
Updated on 22-Apr-2021 11:39:35

74 Views

ProblemConsider a situation where armies of two bunch of alphabets are fighting each other. The soldiers of both and their weight are as follows −TeamASoldierWeightA1B2C3D4TeamBSoldierWeightW1X2Y3Z4Other than the soldiers, there are bombs as well in the arena denoted by ‘!’, and a bomb kills soldiers placed at its adjacent sides.For instance: ‘A!BC’ will result in ‘C’ and ‘!!CC!!’ will result in ‘’.Our function should need to find out when all the bombs in the arena explode which team wins or if both the teams end up with the same weight.For example, if the input to the function is −Inputconst str = ... Read More

Splitting number into n parts close to each other in JavaScript

AmitDiwan
Updated on 22-Apr-2021 11:34:55

899 Views

ProblemWe are required to write a JavaScript function that takes in a number, num, as the first argument and another number, parts, as the second argument.Our function should split the number num into exactly (parts) numbers and we should keep these two conditions in mind −The numbers should be as close as possibleThe numbers should even (if possible).And the ordering of numbers is not important.For example, if the input to the function is −Inputconst num = 20; const parts = 6;Outputconst output = [3, 3, 3, 3, 4, 4];ExampleFollowing is the code − Live Democonst num = 20; const parts = ... Read More

Counting adjacent pairs of words in JavaScript

AmitDiwan
Updated on 22-Apr-2021 11:30:50

225 Views

ProblemWe are required to write a JavaScript function that takes in a string str that represents a sentence as the only argument.Our function should count and return the adjacent pair of identical words present in the string str. Our function should check the words ignoring their case, which means ‘it’ and ‘It’ should be counted as identical.For example, if the input to the function is −Inputconst str = 'This this is a a sample string';Outputconst output = 2;Output ExplanationBecause the repeating words are ‘this’ and ‘a’.ExampleFollowing is the code − Live Democonst str = 'This this is a a sample string'; ... Read More

Advertisements