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 210 of 801
Implement Bubble sort with negative and positive numbers – JavaScript?
Bubble sort is a simple sorting algorithm that works by repeatedly comparing adjacent elements and swapping them if they're in the wrong order. It works equally well with arrays containing both negative and positive numbers. Let's say the following is our unsorted array with negative and positive numbers: var arr = [10, -22, 54, 3, 4, 45, 6]; How Bubble Sort Works Bubble sort compares each pair of adjacent elements and swaps them if the first element is greater than the second. This process continues until no more swaps are needed, meaning the array ...
Read MoreMaximum absolute difference of the length of strings from two arrays in JavaScript
Problem We are required to write a JavaScript function that takes in two arrays, a1 and a2 of strings. Each string is composed with letters from a to z. Let x be any string in the first array and y be any string in the second array. Our function should find the value of − max(abs(length(x) − length(y))) Approach To find the maximum absolute difference, we need to: Find the longest string from both arrays Find the shortest string from both arrays ...
Read MoreHide a video tag on a web page - JavaScript
In JavaScript, you can hide a video element on a web page by setting its display CSS property to "none" using the style.display property. Let's say we have the following sample video tag on a web page: You cannot play video here...... Syntax To hide a video element, use the following syntax: element.style.display = "none"; Method 1: Hide by Class Name This method selects the video element by its class name and hides it: ...
Read MoreHow to change the color of a button when the input field is filled – JavaScript?
In JavaScript, you can change a button's color dynamically when an input field is filled by using event listeners and checking the input's value. This creates an interactive user interface that responds to user input in real-time. Basic HTML Structure First, let's set up the HTML elements we'll be working with: Press Me Using onkeyup Event The onkeyup event triggers every time a key is released in the input field, allowing us to check the input's value and update the button color accordingly. ...
Read MoreTrigger an event IMMEDIATELY on mouse click, not after I let go of the mouse - JavaScript?
By default, the click event fires when you press and then release the mouse button. To trigger an event immediately when the mouse button is pressed down, use the mousedown event instead. The Difference Between click and mousedown The click event requires a complete click cycle (press down + release), while mousedown fires immediately when the button is pressed. Using mousedown Event Here's how to trigger an event immediately on mouse press: Mousedown Event Example ...
Read MoreRepeating each character number of times their one based index in a string using JavaScript
We need to write a JavaScript function that takes a string of lowercase English letters and transforms it according to specific rules: each character should be repeated based on its 1-based index position, with the first letter capitalized, and character groups separated by dashes. Problem Statement Given a string of lowercase English alphabets, construct a new string where: Each character is repeated according to its 1-based index (1st char repeated 1 time, 2nd char repeated 2 times, etc.) The first letter of each repeated group is capitalized Different character groups are separated by dashes ('-') ...
Read MoreHow to remove duplicate property values in array – JavaScript?
In JavaScript, you may need to remove duplicate values from array properties within an array of objects. This is common when working with data that contains repeated values that should be unique. Problem Example Consider an array of student objects where some students have duplicate marks in their studentMarks array: var details = [ { studentName: "John", studentMarks: [78, 98] }, { ...
Read MoreFinding two numbers given their sum and Highest Common Factor using JavaScript
We are required to write a JavaScript function that takes in two numbers. The first number represents the sum of two numbers and second represents their HCF (GCD or Greatest Common Divisor). Our function should find and return those two numbers. Problem Given the sum and HCF of two numbers, we need to find the original two numbers. For two numbers to have a specific HCF, both numbers must be multiples of that HCF. Mathematical Approach If two numbers a and b have HCF h, then: a = h × m and b = ...
Read MoreSet the value in local storage and fetch – JavaScript?
JavaScript's localStorage provides a simple way to store data persistently in the user's browser. Use localStorage.setItem() to store values and localStorage.getItem() to retrieve them. Syntax // Set a value localStorage.setItem("keyName", "value"); // Get a value let value = localStorage.getItem("keyName"); // Remove a value localStorage.removeItem("keyName"); // Clear all localStorage data localStorage.clear(); Setting Values in localStorage The setItem() method accepts two parameters: a key name and the value to store. Values are always stored as strings. ...
Read MoreHow to automate this object using JavaScript to set one key on each iteration as null?
When working with JavaScript objects, you might need to systematically set each property to null one at a time. This can be useful for testing, validation, or creating variations of an object. The most efficient approach is using Object.keys() with the spread operator. Understanding the Problem Given an object with multiple properties, we want to create new objects where exactly one property is set to null in each iteration, while keeping all other properties unchanged. Using Object.keys() with Spread Operator The Object.keys() method returns an array of property names, which we can iterate through. The spread ...
Read More