Found 6683 Articles for Javascript

Bring number down to 1 in JavaScript

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

68 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

254 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

226 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

296 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

159 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

183 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

633 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

251 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

How to Add CSS Rules to a Stylesheet with JavaScript?

AmitDiwan
Updated on 15-Nov-2023 14:10:30

327 Views

The insertRule() helps us add a rule at a defined position in the stylesheet while deleteRule() deletes a specific style on a web page. The following examples illustrate CSS rules that can be added to a stylesheet using JavaScript. Insert a Rule To insert a rule at a defined position, use the insertRule() method. The margin, padding, and box-shadow is also set. First, the custom id is set using the getElementById() − let newSheet = document.getElementById('custom').sheet let cs = 'p {'; cs += 'margin: 4%;'; cs += 'padding: 2%;'; cs += 'font-size: 22px;'; cs += 'box-shadow: ... Read More

Auto Grow a Textarea with JavaScript in CSS

AmitDiwan
Updated on 27-Oct-2023 14:38:37

377 Views

Using JavaScript, we can set our TextArea element to automatically grow with its content. The following examples illustrate how we can achieve the above scenario. Let us say the following is our TextArea before adding content − The following is the TextArea after adding content − The following is the same TextArea that auto grows itself after adding more content − Auto Grow a Textarea Example Let us see how to auto grow a textarea − * { ... Read More

Advertisements