Web Development Articles

Page 421 of 801

Equivalent of Ruby's each cons in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 414 Views

Ruby's each_cons() method iterates through consecutive N elements of an enumerable, creating subarrays of fixed size. JavaScript doesn't have this built-in, but we can implement it using Array prototype extension. Understanding each_cons Behavior The each_cons(n) function creates sliding windows of size n from an array. For each starting position, it takes n consecutive elements until there aren't enough remaining elements. const arr = [1, 2, 3, 4, 5]; // With n=2: creates pairs starting from each position // Result: [[1, 2], [2, 3], [3, 4], [4, 5]] // With n=3: creates triplets starting from ...

Read More

JavaScript Cursor property

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 235 Views

The JavaScript cursor property sets or returns the cursor type that will be displayed when hovering over an element. This property allows you to change the mouse cursor appearance dynamically using JavaScript. Syntax element.style.cursor = "cursorType"; Example: Changing Cursor on Button Click body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .sample { ...

Read More

JavaScript function to accept a string and mirrors its alphabet

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 377 Views

We need to write a function that accepts a string and mirrors its alphabet. This means each letter is replaced with its counterpart from the opposite end of the alphabet. If the input is 'abcd' The output should be 'zyxw' The function maps every character to the letter that is (26 - N) positions away from it, where N is the 1-based index of that alphabet (like 5 for 'e' and 10 for 'j'). How It Works We use the String.prototype.replace() method to match all English alphabets regardless of case. For each letter: ...

Read More

JavaScript DataView()

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 240 Views

The JavaScript DataView provides a low-level interface for reading and writing multiple number types in binary ArrayBuffer. You cannot manipulate ArrayBuffer directly without using DataView(). Syntax new DataView(buffer) new DataView(buffer, byteOffset) new DataView(buffer, byteOffset, byteLength) Parameters buffer - The ArrayBuffer to create a view for byteOffset - (Optional) Starting byte offset, default is 0 byteLength - (Optional) Number of bytes to include, default is buffer's length Basic Example DataView Example Run ...

Read More

Sort the numbers so that the even numbers are ahead JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 268 Views

We have an array of numbers that contains some positive and negative even and odd numbers. We need to sort the array in ascending order but with a special requirement: all even numbers should appear before any odd number, and both groups should be sorted internally in ascending order. For example, if we have an array like [-2, 3, 6, -12, 9, 2, -4, -11, -8], the result should be [-12, -8, -4, -2, 2, 6, -11, 3, 9]. How It Works The solution uses a custom comparator function that: Checks if numbers are even ...

Read More

How to make a fullscreen window with JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 628 Views

The Fullscreen API allows you to display HTML elements in fullscreen mode. This is commonly used for videos, images, or interactive content that benefits from maximum screen real estate. Syntax element.requestFullscreen() // Standard element.webkitRequestFullscreen() // WebKit (Safari, Chrome) element.mozRequestFullScreen() // Firefox element.msRequestFullscreen() // IE/Edge Example: Video Fullscreen body { ...

Read More

How to draw on scroll using JavaScript and SVG?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 599 Views

Drawing on scroll using JavaScript and SVG creates an engaging animation effect where an SVG path gradually appears as the user scrolls down the page. This technique uses stroke-dasharray and stroke-dashoffset properties to control path visibility. How It Works The technique uses SVG's stroke-dasharray and stroke-dashoffset properties to hide the path initially, then gradually reveals it based on scroll position. The path length is calculated and used to synchronize the drawing with scroll progress. SVG Path Drawing Animation Initial State (Hidden) ...

Read More

How to toggle between a like/dislike button with CSS and JavaScript?

Vivek Verma
Vivek Verma
Updated on 15-Mar-2026 2K+ Views

Creating a toggle between like and dislike buttons is a common interactive feature on websites. This article demonstrates how to implement this functionality using HTML, CSS, and JavaScript with Font Awesome icons. HTML Structure The HTML file creates the basic structure with a button containing a thumbs-up icon and display elements for both like and dislike states. Toggle Toggle between a like/dislike button with CSS and JavaScript? ...

Read More

How to toggle between hiding and showing an element with JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

To toggle between hiding and showing an element with JavaScript, you can manipulate the element's display CSS property. This technique is commonly used for creating interactive user interfaces where content needs to be dynamically shown or hidden. Basic Toggle Example body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } button { ...

Read More

Find and return array positions of multiple values JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 4K+ Views

We have to write a function, say findPositions() that takes in two arrays as argument. And it should return an array of the indices of all the elements of the second array present in the first array. For example − If the first array is ['john', 'doe', 'chris', 'snow', 'john', 'chris'], And the second array is ['john', 'chris'] Then the output should be − [0, 2, 4, 5] Therefore, let's write the code for this function. We will use a forEach() loop here. Example const values = ['michael', ...

Read More
Showing 4201–4210 of 8,010 articles
« Prev 1 419 420 421 422 423 801 Next »
Advertisements