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 422 of 801
How to toggle text with JavaScript?
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 MoreConstruct string via recursion JavaScript
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 MoreHow to toggle between adding and removing a class name from an element with JavaScript?
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 MoreHow to remove a class name from an element with JavaScript?
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 MoreHow to multiply odd index values JavaScript
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 MoreHow to add an active class to the current element with JavaScript?
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 MoreHow to find a group of three elements in an array whose sum equals some target sum JavaScript
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 MoreHow to detect whether the browser is online or offline with JavaScript?
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 MoreCounting how many times an item appears in a multidimensional array in JavaScript
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 MoreGet minimum number without a Math function JavaScript
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