Found 6710 Articles for Javascript

Encoding string to reduce its size in JavaScript

AmitDiwan
Updated on 04-Mar-2021 11:41:31

1K+ Views

ProblemWe are required to write a JavaScript function that takes in a string of characters, str, as the only argument. Our function should encode the input string and compare its size with the original string and return the string which is smaller in size.The rule to encode a particular string is −n[s], where the s inside the square brackets is being repeated exactly k times.For instance, ddd can be encoded to 3[d] but 3[d] has a length of 4 whereas ddd is only 3 characters long so our function should eventually return ddd.For example, if the input to the function ... Read More

Forming string using 0 and 1 in JavaScript

AmitDiwan
Updated on 04-Mar-2021 11:44:25

250 Views

ProblemWe are required to write a JavaScript function that takes in an array of strings, arr, formed using 0 and 1 only as the first argument.The function takes two numbers as the second and third argument, m and respectively. The task of our function is to find how many strings from the array arr can be formed using at most m 0s and n 1s.For example, if the input to the function is −const arr = ["10", "0001", "111001", "1", "0"]; const m = 5, n = 3;Then the output should be −const output = 4;Output Explanation:There are in total ... Read More

Finding median for every window in JavaScript

AmitDiwan
Updated on 04-Mar-2021 11:47:31

852 Views

MedianMedian in mathematics, median is the middle value in an ordered(sorted) integer list.If the size of the list is even, and there is no middle value. Median is the mean (average) of the two middle values.ProblemWe are required to write a JavaScript function that takes in an array of Integers, arr, as the first argument and a number num (num {    while (l < r) {       const mid = Math.floor((l + r) / 2);       if (arr[mid] < target) l = mid + 1;       else if (arr[mid] > target) r ... Read More

Magical String: Question in JavaScript

AmitDiwan
Updated on 04-Mar-2021 11:49:25

497 Views

ProblemA magical string str consists of only '1' and '2' and obeys the following rules −The string str is magical because concatenating the number of contiguous occurrences of characters '1' and '2' generates the string str itself.The first few elements of string str is the following −str = "1221121221221121122……"If we group the consecutive '1's and '2's in str, it will be −1 22 11 2 1 22 1 22 11 2 11 22 ......and the occurrences of '1's or '2's in each group are −1 2 2 1 1 2 1 2 2 1 2 2 ......We can see that ... Read More

Formatting Software License Key in JavaScript

AmitDiwan
Updated on 04-Mar-2021 12:15:05

225 Views

ProblemWe are required to write a JavaScript function that takes in a string str, as the first argument and an Integer, n as the second argument. The string str is composed of alphanumeric characters and dashes.The dashes split the alphanumeric characters within the string into groups. (i.e. if there are n dashes, the string is split into n+1 groups). The dashes in the given string are possibly misplaced.We want each group of characters to be of length K (except for possibly the first group, which could be shorter, but still must contain at least one character).To satisfy this requirement, we ... Read More

Finding the smallest good base in JavaScript

AmitDiwan
Updated on 04-Mar-2021 06:01:59

149 Views

Good BaseFor an integer num, we call k (k >= 2) a good base of num, if all digits of num base k are 1.For instance: 13 base 3 is 111, hence 3 is a good base for num = 13ProblemWe are required to write a JavaScript function that takes in string str that represents a number as the only argument. The function should return the string representation of the smallest possible number which is a good base for str.For example, if the input to the function is −const str = "4681";Then the output should be −const output = "8";Output ... Read More

Consecutive ones with a twist in JavaScript

AmitDiwan
Updated on 04-Mar-2021 12:48:15

183 Views

ProblemWe are required to write a JavaScript function that takes in a binary array (an array that consists of only 0 and 1), arr, as the only argument. Our function should find the maximum number of consecutive 1s in this array if we can flip at most one 0.For example, if the input to the function is −const arr = [1, 0, 1, 1, 0];Then the output should be −const output = 4;Output ExplanationIf we flip the 0 at index 1 in the array, we will get 4 consecutive 1s.ExampleThe code for this will be − Live Democonst arr = [1, ... Read More

Finding peculiar pairs in array in JavaScript

AmitDiwan
Updated on 04-Mar-2021 12:49:54

135 Views

ProblemWe are required to write a JavaScript function that takes in an array of Integers, arr, as the first and the only argument.Our function should count the occurrence of all such index pairs (i, j) that satisfy the following conditions −I < j, andarr[i] > 2 * arr[j]For example, if the input to the function is −const input = [2, 4, 3, 5, 1];Then the output should be −const output = 3;Output Explanation:Because the three desired pairs are −[4, 1], [3, 1] and [5, 1]ExampleThe code for this will be − Live Democonst arr = [2, 4, 3, 5, 1]; const ... Read More

Ways to get to a specific sum in JavaScript

AmitDiwan
Updated on 04-Mar-2021 12:51:08

137 Views

ProblemWe are required to write a JavaScript function that takes in an array of Integers, arr, as the first argument and a single integer, target, as the second argument.For each Integer in the array, our function can either assign ‘+’ or ‘-’ to it.Our function should find out how many ways in total exist to assign ‘+’, ‘-’ to make the sum of integers of the array equal to the target sum, target.For example, if the input to the function is −const arr = [1, 1, 1, 1, 1]; const target = 3;Then the output should be −const output = ... Read More

Traversing Diagonally in a matrix in JavaScript

AmitDiwan
Updated on 04-Mar-2021 12:52:53

848 Views

Problem:We are required to write a JavaScript function that takes in a square matrix (an array of arrays having the same number of rows and columns). The function should traverse diagonally through that array of array and prepare a new array of elements placed in that order it encountered while traversing.For example, if the input to the function is −const arr = [    [1, 2, 3],    [4, 5, 6],    [7, 8, 9] ];Then the output should be −const output = [1, 2, 4, 7, 5, 3, 6, 8, 9];Example Live DemoThe code for this will be −const arr ... Read More

Advertisements