Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Javascript Articles
Page 341 of 534
How do I recursively remove consecutive duplicate elements from an array?
Suppose, we have an array of Number literals that contains some consecutive redundant entries like this: const testArr = [1, 1, 2, 2, 3, 3, 1, 1, 1]; We are supposed to write a function compress that takes in this array and removes all redundant consecutive entries in place. So that the output looks like this: const output = [1, 2, 3, 1]; Let's write the code for this function using recursion to remove consecutive duplicate elements. Recursive Approach The recursive function works by traversing the array and comparing each ...
Read MoreHow to set height equal to the dynamic width (CSS fluid layout) in JavaScript?
To set height equal to the dynamic width in JavaScript, you can calculate the element's width and apply it as the height. This creates a perfect square that maintains its aspect ratio as the parent container resizes. Using jQuery jQuery provides a simple way to get and set element dimensions: .wrap { width: 400px; ...
Read MoreWhat's the best way to detect a 'touch screen' device using JavaScript?
The best way to detect a touch screen device is to check whether touch events are supported in the browser's document model. This approach tests for the presence of touch event properties. Basic Touch Detection The most reliable method is to check for the ontouchstart property in the document element: function checkTouchDevice() { return 'ontouchstart' in document.documentElement; } // Test the function console.log("Touch device:", checkTouchDevice()); // Display result on page document.body.innerHTML = "Touch Device: " + checkTouchDevice() + ""; Enhanced Touch Detection Method For better compatibility ...
Read MoreCheck whether a series of operations yields a given number with JavaScript Recursion
By starting from the number 1 and repeatedly either adding 5 or multiplying by 3, an infinite amount of new numbers can be produced. We are required to write a function that, given a number, tries to find a sequence of such additions and multiplications that produce that number. And returns a boolean based on the fact whether or not there exists any such sequence. For example, the number 13 could be reached by first multiplying by 3 and then adding 5 twice (1 × 3 = 3, 3 + 5 = 8, 8 + 5 = 13), so ...
Read MoreHow to set the boldness of the font with JavaScript?
Use the fontWeight property in JavaScript to set the font boldness. This CSS property accepts values like "normal", "bold", "bolder", "lighter", or numeric values from 100 to 900. Syntax element.style.fontWeight = value; Parameters The fontWeight property accepts the following values: Value Description "normal" Default weight (equivalent to 400) "bold" Bold weight (equivalent to 700) "bolder" Bolder than parent element "lighter" Lighter than parent element 100-900 Numeric values (100 = thinnest, 900 = boldest) Example: Setting Font Weight with ...
Read MoreChanging an array in place using splice() JavaScript
The splice() method allows you to modify arrays in place by removing, adding, or replacing elements. This article demonstrates how to use splice() to remove duplicate elements that exceed a specified count limit. The Problem We need to write a function that takes an array and a number n, then removes elements that appear more than n times while preserving the order of remaining elements. Solution Using splice() We'll track element counts using a hashmap and use splice() to remove excess occurrences during iteration: const arr = [7, 26, 21, 41, 43, 2, 26, ...
Read MoreHow to set the height of an element with JavaScript?
Use the style.height property to dynamically set the height of HTML elements with JavaScript. This property accepts height values in pixels, percentages, or other CSS units. Syntax element.style.height = "value"; Where value can be in pixels (px), percentages (%), or other CSS units like em, rem, vh, etc. Example: Setting Button Height Heading 1 This is Demo Text. Update Height ...
Read MoreGroup array by equal values JavaScript
Let's say, we have an array of string/number literals that contains some duplicate values like this: const array = ['day', 'night', 'afternoon', 'night', 'noon', 'night', 'noon', 'day', 'afternoon', 'day', 'night']; We are required to write a function groupSimilar() that takes in this array and returns a new array where all the repeating entries are grouped together in a subarray as the first element and their total count in the original array as the second element. So, for this example, the output should be: [ [ 'day', 3 ], ...
Read MoreHow to preserve the readability of text when font fallback occurs with JavaScript?
The fontSizeAdjust property preserves text readability when font fallback occurs by maintaining consistent apparent size across different fonts. It adjusts the font size based on the aspect ratio (x-height to font-size ratio) to ensure uniform visual appearance. How fontSizeAdjust Works Different fonts have varying x-heights even at the same font-size. When a preferred font fails to load and falls back to another font, the text may appear too large or too small. The fontSizeAdjust property solves this by scaling the fallback font to match the aspect ratio of the preferred font. Syntax element.style.fontSizeAdjust = "value"; ...
Read MoreForm Object from string in JavaScript
We are required to write a function that takes in a string as the first and the only argument and constructs an object with its keys based on the unique characters of the string and value of each key being defaulted to 0. For example − // if the input string is: const str = 'hello world!'; // then the output should be: const obj = {"h": 0, "e": 0, "l": 0, "o": 0, " ": 0, "w": 0, "r": 0, "d": 0, "!": 0}; So, let's write the code for this function − ...
Read More