Front End Technology Articles

Page 209 of 652

Implement Bubble sort with negative and positive numbers – JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 987 Views

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 More

Finding two numbers given their sum and Highest Common Factor using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 291 Views

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 More

Hide a video tag on a web page - JavaScript

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

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 More

How to change the color of a button when the input field is filled – JavaScript?

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

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 More

Trigger an event IMMEDIATELY on mouse click, not after I let go of the mouse - JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 343 Views

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 More

Returning an array containing last n even numbers from input array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 236 Views

Problem We are required to write a JavaScript function that takes in an array of numbers as the first argument and a number as the second argument. Our function should pick and return an array of last n even numbers present in the input array. Example Following is the code − const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; const num = 3; const pickEvens = (arr = [], num = 1) => { const res = []; for(let index = ...

Read More

How to remove duplicate property values in array – JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 875 Views

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 More

Constructing an array of first n multiples of an input number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 426 Views

We are required to write a JavaScript function that takes in two numbers, let say m and n. Our function should construct and return an array of first n natural multiples of m. Problem Statement Given two numbers m and n, we need to create a function that returns an array containing the first n multiples of m. For example, if m = 6 and n = 5, the function should return [6, 12, 18, 24, 30]. Method 1: Using a For Loop The most straightforward approach is to use a for loop to iterate ...

Read More

Set the value in local storage and fetch – JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 655 Views

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 More

How to automate this object using JavaScript to set one key on each iteration as null?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 207 Views

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
Showing 2081–2090 of 6,519 articles
« Prev 1 207 208 209 210 211 652 Next »
Advertisements