Found 6710 Articles for Javascript

Deleting desired node from a Binary Search Tree in JavaScrip

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

265 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

812 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

How to Add CSS Rules to a Stylesheet with JavaScript?

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

420 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

459 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

Typing and Deleting Effect with JavaScript and CSS

AmitDiwan
Updated on 02-Jan-2024 17:52:35

2K+ Views

With the help of CSS animations, we can create a typewriter typing and deleting effect using JavaScript. The infinite effect is also set. The custom function will get called and the words will get display with the effect. At the end, those words will get deleted using another custom function. Set a div for the text and cursor First, a parent div container is set with the element. One of the will have text and another the cursorL | Style the element A professional font is ... Read More

Get and Set CSS Variables with JavaScript

AmitDiwan
Updated on 28-Oct-2024 14:54:50

14K+ Views

To get and set CSS variables with JavaScript, we can use various methods. The getComputedStyle() method gives an object which includes all the styles applied to the target element. The getPropertyValue() method is used to obtain the desired property from the computed styles. The setProperty() method is used to change the value of CSS variable. In this article we are having a div and a button, our task is to get and set CSS variables with JavaScript. By getting and setting the CSS variable, we will change the background color of div upon clicking the button. Steps to Get ... Read More

Unique substrings in circular string in JavaScript

AmitDiwan
Updated on 04-Mar-2021 11:26:29

207 Views

ProblemSuppose we have a S, str. which is an infinite wraparound string of the string −"abcdefghijklmnopqrstuvwxyz".Therefore, S will look like this −"...zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd....".We are required to write a JavaScript function that takes in str, let’s call that string str, as the only argument.Our function should find out how many unique non-empty substrings of str are present in S.Our function should finally return the number of different non-empty substrings of str in the string S.For example, if the input to the function is −const str = "zab";Then the output should be −const output = 6;Output ExplanationThere are six substrings "z", "a", "b", ... Read More

Deriving Random10() function from Random7() in JavaScript

AmitDiwan
Updated on 04-Mar-2021 11:34:44

259 Views

Problemconst random7 = () => Math.ceil(Math.random() * 7);Suppose we have the above fat arrow function. This function yields a random number between 0 (exclusive) and 7 (inclusive) everytime we make a call to it.We are required to write a similar random10() JavaScript function that takes no argument and makes no use of the JavaScript library or any third party library. And only making use of this random7() function, our function should return random number between 0 (exclusive) and 10(inclusive).ExampleThe code for this will be − Live Democonst random7 = () => Math.ceil(Math.random() * 7); const random10 = () => {   ... Read More

Checking for convex polygon in JavaScript

AmitDiwan
Updated on 04-Mar-2021 11:38:16

454 Views

Convex PolygonA convex polygon is defined as a polygon with all its interior angles less than 180°.ProblemWe are required to write a JavaScript function that takes in an array of coordinates, basically the array will be an array of arrays each subarray containing exactly two numbers, specifying a point on a 2-D plane.Our function should determine whether the polygon formed by these points is a convex polygon or not. If yes, the function should return true, false otherwise.For example, if the input to the function is −const arr = [[0, 0], [0, 1], [1, 1], [1, 0]];Then the output should be ... Read More

Advertisements