Web Development Articles

Page 422 of 801

How to toggle text with JavaScript?

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

Text toggling allows you to dynamically change displayed content based on user interaction. This is commonly used for showing/hiding information, switching between states, or creating interactive UI elements. Basic Toggle Example Here's a complete example that toggles between two text values when a button is clicked: body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; ...

Read More

Construct string via recursion JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 387 Views

We are required to write a recursive function, say pickString that takes in a string that contains a combination of alphabets and numbers and returns a new string consisting of only alphabets. For example, If the string is 'dis122344as65t34er', The output will be: 'disaster' Therefore, let's write the code for this recursive function āˆ’ Syntax const pickString = (str, len = 0, res = '') => { if(len < str.length){ const char = parseInt(str[len], 10) ? '' : str[len]; ...

Read More

How to toggle between adding and removing a class name from an element with JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 300 Views

JavaScript provides several methods to toggle class names on HTML elements. The most efficient approach is using the classList.toggle() method, which automatically adds a class if it doesn't exist or removes it if it does. Using classList.toggle() Method The toggle() method is the simplest way to switch between adding and removing a class: .highlight { background-color: #3498db; ...

Read More

How to remove a class name from an element with JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 488 Views

To remove a class name from an element with JavaScript, you can use the classList.remove() method. This method provides a clean and efficient way to manipulate CSS classes dynamically. Syntax element.classList.remove('className'); Example .newStyle { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; width: 100%; padding: 25px; background-color: rgb(147, 80, 255); color: white; ...

Read More

How to multiply odd index values JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 432 Views

We are required to write a function that takes in an array of Number literals as one and the only argument. The numbers that are situated at even index should be returned as it is. But the numbers situated at the odd index should be returned multiplied by their corresponding indices. For example: If the input is: [5, 10, 15, 20, 25, 30, 50, 100] Then the function should return: [5, 10, 15, 60, 25, 150, 50, 700] Understanding the Logic Let's break down what happens at each ...

Read More

How to add an active class to the current element with JavaScript?

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

Adding an active class to the current element allows you to highlight the selected item in navigation menus, tabs, or button groups. This technique uses JavaScript event listeners to dynamically manage CSS classes. Complete Example .btn { border: none; outline: none; padding: 10px 16px; background-color: #6ea2f0; cursor: pointer; color: white; ...

Read More

How to find a group of three elements in an array whose sum equals some target sum JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 628 Views

We need to write a function that finds three elements in an array whose sum equals a target value. The function should return the indices of these three elements, or -1 if no such triplet exists. Approach We'll use a two-step approach: Create a twoSum() helper function that finds two numbers adding up to a target sum Iterate through each element and use twoSum() to find the remaining two elements This approach has O(N²) time complexity, where N is the array length. How It Works The twoSum() function uses a hash map ...

Read More

How to detect whether the browser is online or offline with JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 491 Views

JavaScript provides the navigator.onLine property to detect whether the browser is online or offline. This property returns true when connected to the internet and false when offline. The navigator.onLine Property The navigator.onLine property is a read-only boolean that indicates the browser's online status: // Basic syntax if (navigator.onLine) { // Browser is online } else { // Browser is offline } Example: Manual Check Online/Offline Status Checker Check Status ...

Read More

Counting how many times an item appears in a multidimensional array in JavaScript

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

We have a nested array of strings and we have to write a function that accepts the array and a search string and returns the count of the number of times that string appears in the nested array. Therefore, let's write the code for this, we will use recursion here to search inside of the nested array and the code for this will be āˆ’ Example const arr = [ "apple", ["banana", "strawberry", "dsffsd", "apple"], "banana", ["sdfdsf", "apple", ["apple", ["nonapple", "apple", ["apple"]]]] ...

Read More

Get minimum number without a Math function JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 311 Views

We need to find the smallest number from a set of numbers without using JavaScript's built-in Math.min() function. This requires implementing our own comparison logic. Approach Using While Loop We'll iterate through all numbers and keep track of the smallest value found so far, updating it whenever we encounter a smaller number. Example const numbers = [12, 5, 7, 43, -32, -323, 5, 6, 7, 767, 23, 7]; const findMin = (...numbers) => { let min = Infinity, len = 0; while(len < numbers.length) { ...

Read More
Showing 4211–4220 of 8,010 articles
« Prev 1 420 421 422 423 424 801 Next »
Advertisements