Found 8591 Articles for Front End Technology

Any possible combination to add up to target in JavaScript

AmitDiwan
Updated on 18-Mar-2021 13:34:40

346 Views

ProblemWe are required to write a JavaScript function that takes in an array of unique integers, arr, as the first argument, and target sum as the second argument.Our function should count the number of all pairs (with repetition allowed) that can add up to the target sum and return that count.For example, if the input to the function is −const arr = [1, 2, 3]; const target = 4;Then the output should be −const output = 7;Output Explanation:Because, the possible combination ways are −(1, 1, 1, 1) (1, 1, 2) (1, 2, 1) (1, 3) (2, 1, 1) (2, 2) ... Read More

Nth smallest element in sorted 2-D array in JavaScript

AmitDiwan
Updated on 18-Mar-2021 13:37:47

371 Views

ProblemSuppose, we have a sorted array of arrays of Numbers (sorted in increasing order) like this −const arr = [    [ 1, 5, 9],    [10, 11, 13],    [12, 13, 15] ];We are required to write a JavaScript function that takes in in one such array as the first argument and a single Integer, num, as the second argument.Our function is supposed to return the numth smallest element that exists in the array arr.For example, if the input to the function is −const arr = [    [ 1, 5, 9],    [10, 11, 13],    [12, 13, ... Read More

Bring number down to 1 in JavaScript

AmitDiwan
Updated on 18-Mar-2021 13:40:00

93 Views

ProblemWe are required to write a JavaScript function that takes in number, num as the only argument.Our function can do only these two operations on num: If num is even, we can replace num with num/2If num is odd, we can replace num with either num + 1 or num - 1.Using only a combination of these two operations our function is required to compute how many minimum operations it requires to bring num down to 1. The function should return the minimum number of operations.For example, if the input to the function is −const num = 7;Then the output ... Read More

Picking index randomly from array in JavaScript

Aayush Mohan Sinha
Updated on 04-Aug-2023 10:07:19

673 Views

The process of randomly selecting an index from an array holds significant importance in various JavaScript applications, as it enables developers to introduce an element of unpredictability and diversity to their code. By incorporating the capability to pick an index randomly, developers can enhance the dynamism and versatility of their applications, resulting in a more engaging user experience. In this article, we will explore the intricacies of selecting an index randomly from an array in JavaScript, unraveling the underlying techniques and lesser-known methods that empower developers to introduce randomness into their array-based operations. Through a comprehensive understanding of these concepts, ... Read More

Smallest number after removing n digits in JavaScript

AmitDiwan
Updated on 18-Mar-2021 13:43:19

306 Views

ProblemWe are required to write a JavaScript function that takes in two numbers, let’s call them m and n as first and the second argument respectively.The task of our function is to remove n digits from the number m so that the number m is the smallest possible number after removing n digits. And finally, the function should return the number m after removing digits.For example, if the input to the function is −const m = '45456757'; const n = 3;Then the output should be −const output = '44557';Output Explanation:We removed 5, 6 and 7 digit to get the smallest ... Read More

Finding Sum of Left Leaves of a BST in JavaScript

AmitDiwan
Updated on 18-Mar-2021 13:45:48

342 Views

ProblemWe are required to write a JavaScript function that takes in the root of a Binary Search Tree as the only argument.The function should simply calculate the sum of data stored in the left leaves of the BST.For example, if the Tree looks like this −8 / \ 1 10 / \ 5 17Then the output should be −const output = 6;Output Explanation:Because there are two left leaves in the Tree with values 1 and 5.ExampleThe code for this will be − Live Democlass Node{    constructor(data) {       this.data = data;       this.left = null;   ... Read More

Largest sum of subarrays in JavaScript

Aayush Mohan Sinha
Updated on 04-Aug-2023 09:57:58

325 Views

The quest for finding the largest sum of subarrays in JavaScript is a pursuit of paramount importance for developers seeking to optimize algorithmic efficiency and unravel the hidden potential within their data sets. In the realm of computational problem-solving, the ability to identify and compute the maximal cumulative sum of subsets within an array holds the key to unlocking insights and driving impactful decision-making processes. In this article, we embark upon a comprehensive exploration of the methodologies and techniques required to tackle this intricate challenge, employing a diverse array of rarely used words to elucidate the intricacies of JavaScript programming. ... Read More

Deleting desired node from a Binary Search Tree in JavaScrip

AmitDiwan
Updated on 18-Mar-2021 08:45:50

266 Views

ProblemSuppose, we have the following code that creates a Binary Search Tree DS and provides us with the functionality to insert node −class Node{    constructor(data) {       this.data = data;       this.left = null;       this.right = null;    }; }; class BinarySearchTree{    constructor(){       // root of a binary seach tree       this.root = null;    }    insert(data){       var newNode = new Node(data);       if(this.root === null){          this.root = newNode;       }else{       ... Read More

Sorting string characters by frequency in JavaScript

AmitDiwan
Updated on 18-Mar-2021 13:49:54

815 Views

ProblemWe are required to write a JavaScript function that takes in the string of characters as the only argument.Our function should prepare and a new string based on the original string in which the characters that appear for most number of times are placed first followed by number with decreasing frequencies.For example, if the input to the function is −const str = 'free';Then the output should be −const output = 'eefr';Output Explanation:Since e appears twice it is placed first followed by r and f.ExampleThe code for this will be − Live Democonst str = 'free'; const frequencySort = (str = '') ... Read More

Is the string a combination of repeated substrings in JavaScript

AmitDiwan
Updated on 18-Mar-2021 13:51:18

297 Views

ProblemWe are required to write a JavaScript function that takes in a string of characters as the only argument. Our function needs to check if the string str can be constructed by taking a substring of it and appending multiple copies of the substring together.For example, if the input to the function is −const str = 'thisthisthisthis';Then the output should be −const output = true;Output Explanation:Because the string is made by appending ‘this’ string repeatedly.ExampleThe code for this will be − Live Democonst str = 'thisthisthisthis'; const repeatedSubstring = (str = '') => {    const {length} = str;    const ... Read More

Advertisements