Found 6710 Articles for Javascript

Finding distance to next greater element in JavaScript

AmitDiwan
Updated on 24-Apr-2021 10:12:46

249 Views

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 should construct a new array for the input in which each corresponding element is the distance to the next greater element than the current element, and if there is no greater element to the right of the current element, we should push 0 for that corresponding element in the res array and finally we should return this array.For example, if the input to the function isInputconst arr = [12, 13, 14, 11, 16, 10, 12, 17, ... Read More

Convert mixed case string to lower case in JavaScript

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

381 Views

ProblemWe are required to write a JavaScript function convertToLower() that takes in a string method that converts the string it is being called upon into lowercase string and returns the new string.For example, if the input to the function isInputconst str = 'ABcD123';Outputconst output = 'abcd123';ExampleFollowing is the code − Live Democonst str = 'ABcD123'; String.prototype.convertToLower = function(){    let res = '';    for(let i = 0; i < this.length; i++){       const el = this[i];       const code = el.charCodeAt(0);       if(code >= 65 && code

Splitting a string into maximum parts in JavaScript

AmitDiwan
Updated on 24-Apr-2021 10:03:43

318 Views

ProblemWe are required to write a JavaScript function that takes in a string, str, as the first and the only argument.The purpose of our function is to partition this string into as many parts as possible so that each letter appears in at most one part, and return an array of integers representing the size of these parts.For example, if the input to the function isInputconst str = "ababcbacadefegdehijhklij";Outputconst output = [9, 7, 8];Output ExplanationThe partition is "ababcbaca", "defegde", "hijhklij". This is a partition so that each letter appears in at most one part. A partition like "ababcbacadefegde", "hijhklij" is ... Read More

Checking for particular types of matrix in JavaScript

AmitDiwan
Updated on 24-Apr-2021 10:02:55

81 Views

ProblemWe are required to write a JavaScript function that takes in a 2-D array of literals, arr, as the first and the only argument.Our function should check if every diagonal from top-left to bottom-right has the same element.If it is so, we should return true, false otherwise.For example, if the input to the function isInputconst arr = [    [6, 7, 8, 9],    [2, 6, 7, 8],    [1, 2, 6, 7], ];Outputconst output = true;Output ExplanationIn the above array, the diagonals are −[1], [2, 2], [6, 6, 6], [7, 7, 7], [8, 8], [9]In each diagonal all elements ... Read More

Count and return the number of characters of str1 that makes appearances in str2 using JavaScript

AmitDiwan
Updated on 24-Apr-2021 10:02:27

159 Views

ProblemWe are required to write a JavaScript function that takes in two strings, str1 and str2 as the first and second argument respectively.Our function should count and return the number of characters of str1 that makes appearances in str2 as well, and if there are repetitive appearances, we have to count them separately.For example, if the input to the function isInputconst str1 = 'Kk'; const str2 = 'klKKkKsl';Outputconst output = 5;ExampleFollowing is the code − Live Democonst str1 = 'Kk'; const str2 = 'klKKkKsl'; var countAppearances = (str1 = '', str2 = '') => {    const map = {}   ... Read More

Search in an array with Binary search using JavaScript

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

591 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

244 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

179 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

217 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

121 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

Advertisements