Found 6683 Articles for Javascript

Find Smallest Letter Greater Than Target in JavaScript

AmitDiwan
Updated on 27-Feb-2021 17:27:05

314 Views

Suppose we are given an array of sorted characters letters containing only lowercase letters. And given a target letter target.We are required to write a JavaScript function that takes in the array as the first argument and the letter as the second. The function is supposed to find the smallest element in the list that is larger than the given target.We have to keep in mind that letters also wrap around. For example, if the target is target = 'z' and letters = ['a', 'b'], the answer is 'a'.For Example −If the input array and letter are −const arr = ... Read More

Finding all valid word squares in JavaScript

AmitDiwan
Updated on 27-Feb-2021 17:29:33

183 Views

Word Square:A word square consists of a set of words written out in a square grid, such that the same words can be read both horizontally and vertically.For instance, once valid word square is −H E A R T E M B E R A B U S E R E S I N T R E N DWe are required to write a JavaScript function that takes in an array of words. The function should return true if the array given as input forms a valid word square, false otherwise.For example −If the input word array is −const arr ... Read More

Finding gcd of two strings in JavaScript

AmitDiwan
Updated on 27-Feb-2021 17:32:38

2K+ Views

In the number system, the Greatest Common Divisor (GCD) of two numbers is that greatest number which divides both the numbers. Similarly, if we apply this concept to strings, the gcd of two strings is the greatest substring (greatest in length) that is present in both the strings.For example −If the two strings are −const str1 = 'abcabc'; const str2 = 'abc';Then the gcd of these strings will be −const gcd = 'abc';We are required to write a JavaScript function that takes in two strings str1 and str2 and computes and returns their gcd.ExampleThe code for this will be − Live ... Read More

Checking digit sum of smallest number in the array in JavaScript

AmitDiwan
Updated on 27-Feb-2021 17:35:00

150 Views

We are required to write a JavaScript function that takes in an array of numbers as the first and the only argument. The function should first pick the smallest number from the array and then calculate the sum of all the digits of the number.If the digit sum of that number is even, we should return true, false otherwise.For example −If the input array is −const arr = [12, 657, 23, 56, 34, 678, 42];Then the output should beconst output = false;because the smallest number in the array is 12 and its digit sum is 1 + 2 = 3, ... Read More

Finding confusing number within an array in JavaScript

AmitDiwan
Updated on 27-Feb-2021 17:37:17

167 Views

Confusing Numbers:A number in an array is confusing if it becomes another number which is also present in the array after we rotate the number by 180 degrees vertically and horizontally. For instance, if we rotate 6 by 180 degrees vertically and horizontally it becomes 9 and vice-versa.We have to keep in mind that only rotations of 0, 1, 6, 8, 9 yield a valid number.We are required to write a JavaScript function that takes in a natural number, num, as the first and the only argument. The function should first construct an array of all natural numbers upto num, ... Read More

Finding the missing number in an arithmetic progression sequence in JavaScript

AmitDiwan
Updated on 27-Feb-2021 17:38:48

291 Views

Arithmetic Progression:An arithmetic progression (AP) or arithmetic sequence is a sequence of numbers such that the difference between the consecutive terms is constant.For instance, the sequence 5, 7, 9, 11, 13...Suppose we have an array that represents elements of arithmetic progression in order. But somehow one of the numbers from the progression goes missing. We are required to write a JavaScript function that takes in one such array as the first and the only argument.Then our function, in one iteration, should find and return the number which is missing from the sequence.For example −If the input array is −const arr ... Read More

Performing shifts within a string in JavaScript

AmitDiwan
Updated on 27-Feb-2021 17:41:07

1K+ Views

Suppose we have a string str containing lowercase English letters, and an array of arrays arr, where arr[i] = [direction, amount] −direction can be 0 (for left shift) or 1 (for right shift).amount is the amount by which string s is to be shifted.A left shift by 1 means remove the first character of s and append it to the end.Similarly, a right shift by 1 means remove the last character of s and add it to the beginning.We are required to write a JavaScript function that takes in the string as the first argument and the array containing shift ... Read More

Shifting string letters based on an array in JavaScript

AmitDiwan
Updated on 27-Feb-2021 17:42:30

217 Views

Suppose we have a string that contains only lowercase english alphabets.For the purpose of this question, we define unit shifting of a letter as replacing that very letter to its succeeding letter in alphabets (including wrapping which means next to 'z' is 'a');We are required to write a JavaScript function that takes in a string str as the first argument and an array of numbers arr of the same length that of str as the second argument. Our function should prepare a new string in which the letters of the original string are shifted by the corresponding units present in ... Read More

Meeting Rooms 2 problem in JavaScript

AmitDiwan
Updated on 27-Feb-2021 17:44:11

272 Views

We will be given an array of arrays, each subarray consists of exactly two elements indicating the start and end time of a meeting.The task of our function is to find the maximum number of meetings one person can take avoiding the conflict of time. The function should finally return this number.For example −If the input array describing meeting times is −const arr = [[5, 40], [10, 20], [25, 35]];Then the output should be −const output = 2;because it's not possible to take all three meetings due to overlapping times but [10, 20] and [25, 35] can be attended.ExampleThe code ... Read More

Checking for majority element in a sorted array in JavaScript

AmitDiwan
Updated on 27-Feb-2021 17:45:54

107 Views

Majority Element:A majority element in an array arr of length l is an element that appears more than l/2 times and hence there is at most one such element.We need to write a JavaScript function, say isMajority() that takes an array arr which is always sorted in the increasing order as the first argument.The second argument of the function will be a number, for which we will search the array and return true if that number is the majority element or false otherwise.For example −If the input array and the number are −const arr = [5, 5, 5, 12, 15]; ... Read More

Advertisements