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
Web Development Articles
Page 421 of 801
Equivalent of Ruby's each cons in JavaScript
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 MoreJavaScript Cursor property
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 MoreJavaScript function to accept a string and mirrors its alphabet
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 MoreJavaScript DataView()
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 MoreSort the numbers so that the even numbers are ahead JavaScript
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 MoreHow to make a fullscreen window with JavaScript?
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 MoreHow to draw on scroll using JavaScript and SVG?
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 MoreHow to toggle between a like/dislike button with CSS and JavaScript?
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 MoreHow to toggle between hiding and showing an element with JavaScript?
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 MoreFind and return array positions of multiple values JavaScript
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