Found 6683 Articles for Javascript

Search in an array with Binary search using JavaScript

Aayush Mohan Sinha
Updated on 04-Aug-2023 10:09:56

390 Views

In the realm of JavaScript programming, the ability to efficiently search through an array using the Binary search algorithm holds tremendous importance. This algorithmic technique, often regarded as an elegant and powerful solution, offers developers a high-performance approach to locate desired elements within a sorted array. Mastery of Binary search using JavaScript not only demonstrates one's proficiency in algorithmic problem-solving but also empowers developers to optimize search operations in their applications. In this article, we delve into the intricacies of implementing Binary search in JavaScript, exploring its step-by-step process and uncovering the rarely used words within the realm of this ... Read More

Creating permutations by changing case in JavaScript

AmitDiwan
Updated on 24-Apr-2021 10:01:22

175 Views

ProblemWe are required to write a JavaScript function that takes in a string of characters, str, as the first and the only argument.Our function can transform every letter individually to be lowercase or uppercase to create another string. And we should return a list of all possible strings we could create.For example, if the input to the function isInputconst str = 'k1l2';Outputconst output = ["k1l2", "k1L2", "K1l2", "K1L2"];ExampleFollowing is the code − Live Democonst str = 'k1l2'; const changeCase = function (S = '') {    const res = []    const helper = (ind = 0, current = '') => ... Read More

Counting matching substrings in JavaScript

Aayush Mohan Sinha
Updated on 04-Aug-2023 09:52:29

96 Views

The ability to accurately count matching substrings within a given string is a pivotal skill in JavaScript programming, as it empowers developers to efficiently analyze and manipulate text data. Delving into the realm of string manipulation, this article explores the intricacies of counting matching substrings in JavaScript, employing a range of lesser-known techniques. By unraveling the underlying logic and employing these unconventional methods, developers can gain a deeper understanding of how to effectively tally occurrences of specific substrings, enabling them to extract meaningful insights from textual data. Join us on this enlightening journey as we unlock the potential of JavaScript's ... Read More

Making two sequences increasing in JavaScript

AmitDiwan
Updated on 24-Apr-2021 09:58:36

112 Views

Strictly Increasing SequenceA sequence is strictly increasing if and only if arr[0] < arr[1] < arr[2] < ... < arr[arr.length - 1].ProblemWe are required to write a JavaScript function that takes in two arrays of numbers, arr1 and arr2 as the first and the second argument respectively.We can swap any number of elements from arr1 to arr2, that happen to live on the same indices. It means we can swap arr1[i] with arr2[i]. Our function should return the minimum number of swaps to make both sequences strictly increasing.For example, if the input to the function isInputconst arr1 = [1, 3, ... Read More

Greatest sum of average of partitions in JavaScript

AmitDiwan
Updated on 24-Apr-2021 09:57:59

75 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers, arr, as the first argument, and a number, num, (num {    const sum = (arr = []) => arr.reduce((acc, num) => acc + num, 0)    let matrix = new Array(num + 1).fill(0).map(() => new Array(arr.length + 1).fill(0))    for (let index = arr.length; index >= 0; index--) {       const current = new Array(num + 1).fill(0).map(() => new Array(arr.length +    1).fill(0))       for (let currentK = num; currentK >= 0; currentK--) {          for ... Read More

Corresponding shortest distance in string in JavaScript

AmitDiwan
Updated on 24-Apr-2021 09:57:22

134 Views

ProblemWe are required to write a JavaScript function that takes in a string of English lowercase alphabets, str, as the first argument and a single character, char, which exists in the string str, as the second argument.Our function should prepare and return an array which, for each character in string str, contains its distance from the nearest character in the string specified by char.For example, if the input to the function isInputconst str = 'somestring'; const char = 's';Outputconst output = [0, 1, 2, 1, 0, 1, 2, 3, 4, 5]ExampleFollowing is the code − Live Democonst str = 'somestring'; const ... Read More

Implementing circular queue ring buffer in JavaScript

AmitDiwan
Updated on 24-Apr-2021 09:55:46

993 Views

Circular QueueThe circular queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position to make a circle. It is also called "Ring Buffer".One of the benefits of the circular queue is that we can make use of the spaces in front of the queue. In a normal queue, once the queue becomes full, we cannot insert the next element even if there is a space in front of the queue. But using the circular queue, we can use the ... Read More

Flip the matrix horizontally and invert it using JavaScript

AmitDiwan
Updated on 24-Apr-2021 09:55:05

1K+ Views

ProblemWe are required to write a JavaScript function that takes in a 2-D binary array, arr, (an array that consists of only 0 or 1), as the first and the only argument.Our function should first flip the matrix horizontally, then invert it, and return the resulting matrix.To flip the matrix horizontally means that each row of the matrix is reversed. For example, flipping [1, 1, 0] horizontally results in [0, 1, 1].To invert a matrix means that each 0 is replaced by 1, and each 1 is replaced by 0. For example, inverting [0, 1, 1] results in [1, 0, ... Read More

Maximum length of mountain in an array using JavaScript

AmitDiwan
Updated on 24-Apr-2021 09:53:01

177 Views

Mountain SubsequenceWe call any (contiguous) subarray sub (of arr) a mountain if the following properties hold −sub.length >= 3There exists some 0 < i < sub.length - 1 such that sub[0] < sub[1] < ... sub[i-1] < sub[i] > B[i+1] > ... > sub[sub.length - 1]ProblemWe are required to write a JavaScript function that takes in an array of numbers, arr, as the first and the only argument.Our function is supposed to return the length of the greatest mountain subsequence present in the array arr, if there exists any, 0 otherwise.For example, if the input to the function isInputconst arr ... Read More

Rearranging cards into groups in JavaScript

AmitDiwan
Updated on 24-Apr-2021 09:50:10

94 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers, arr, as the first argument and a number, num, as the second argument.The numbers in the array are in the range [1, 13], limits inclusive, representing the 1-based index of playing cards.Our function should determine whether there exists a way to rearrange the cards into groups so that each group is size num, and consists of num consecutive cards.For example, if the input to the function isInputconst arr = [1, 4, 3, 2]; const num = 2;Outputconst output = 2;Output ExplanationBecause the cards can be ... Read More

Advertisements