Found 6683 Articles for Javascript

Typing and Deleting Effect with JavaScript and CSS

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

1K+ 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 02-Nov-2023 11:56:21

12K+ Views

To get and set the 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() is used to change the value of CSS variable. The following example illustrate how we can get and set CSS variables using JavaScript. First, we have called the two custom methods − The showColor() method use the getPropertyValue() method to get the property − function showColor() { let ... Read More

Unique substrings in circular string in JavaScript

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

123 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

171 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

345 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

Encoding string to reduce its size in JavaScript

AmitDiwan
Updated on 04-Mar-2021 11:41:31

921 Views

ProblemWe are required to write a JavaScript function that takes in a string of characters, str, as the only argument. Our function should encode the input string and compare its size with the original string and return the string which is smaller in size.The rule to encode a particular string is −n[s], where the s inside the square brackets is being repeated exactly k times.For instance, ddd can be encoded to 3[d] but 3[d] has a length of 4 whereas ddd is only 3 characters long so our function should eventually return ddd.For example, if the input to the function ... Read More

Forming string using 0 and 1 in JavaScript

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

149 Views

ProblemWe are required to write a JavaScript function that takes in an array of strings, arr, formed using 0 and 1 only as the first argument.The function takes two numbers as the second and third argument, m and respectively. The task of our function is to find how many strings from the array arr can be formed using at most m 0s and n 1s.For example, if the input to the function is −const arr = ["10", "0001", "111001", "1", "0"]; const m = 5, n = 3;Then the output should be −const output = 4;Output Explanation:There are in total ... Read More

Finding median for every window in JavaScript

AmitDiwan
Updated on 04-Mar-2021 11:47:31

688 Views

MedianMedian in mathematics, median is the middle value in an ordered(sorted) integer list.If the size of the list is even, and there is no middle value. Median is the mean (average) of the two middle values.ProblemWe are required to write a JavaScript function that takes in an array of Integers, arr, as the first argument and a number num (num {    while (l < r) {       const mid = Math.floor((l + r) / 2);       if (arr[mid] < target) l = mid + 1;       else if (arr[mid] > target) r ... Read More

Magical String: Question in JavaScript

AmitDiwan
Updated on 04-Mar-2021 11:49:25

358 Views

ProblemA magical string str consists of only '1' and '2' and obeys the following rules −The string str is magical because concatenating the number of contiguous occurrences of characters '1' and '2' generates the string str itself.The first few elements of string str is the following −str = "1221121221221121122……"If we group the consecutive '1's and '2's in str, it will be −1 22 11 2 1 22 1 22 11 2 11 22 ......and the occurrences of '1's or '2's in each group are −1 2 2 1 1 2 1 2 2 1 2 2 ......We can see that ... Read More

Formatting Software License Key in JavaScript

AmitDiwan
Updated on 04-Mar-2021 12:15:05

139 Views

ProblemWe are required to write a JavaScript function that takes in a string str, as the first argument and an Integer, n as the second argument. The string str is composed of alphanumeric characters and dashes.The dashes split the alphanumeric characters within the string into groups. (i.e. if there are n dashes, the string is split into n+1 groups). The dashes in the given string are possibly misplaced.We want each group of characters to be of length K (except for possibly the first group, which could be shorter, but still must contain at least one character).To satisfy this requirement, we ... Read More

Advertisements