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
Articles on Trending Technologies
Technical articles with clear explanations and examples
Switching positions of selected characters in a string in JavaScript
We are required to write a JavaScript function that takes in a string containing only the letters 'k', 'l' and 'm'. The task is to switch the positions of 'k' with 'l' while leaving all instances of 'm' at their original positions. Problem Statement Given a string with characters 'k', 'l', and 'm', we need to swap every occurrence of 'k' with 'l' and vice versa, keeping 'm' unchanged. Example Here's the implementation using a simple loop approach: const str = 'kklkmlkk'; const switchPositions = (str = '') => { ...
Read MoreError.prototype.toString() Method in JavaScript
JavaScript's Error.prototype.toString() method is a built-in method that converts error objects to readable string representations. This method is essential for error handling, logging, and debugging in JavaScript applications. Error.prototype.toString() Method The Error.prototype.toString() method converts an error object to a string format. It returns a string containing the error name, followed by a colon and space, then the error message. For example, a standard Error object will produce a string like "Error: something went wrong". Syntax errorObject.toString() Parameters: None Return Value: A string representation of the error object Basic Usage Example Here's ...
Read MoreHow to change the text of a label using JavaScript?
The HTML tag improves web accessibility by providing clickable text descriptions for form elements. In JavaScript, you can dynamically change label text using two properties: innerHTML and innerText. Using the innerHTML Property The innerHTML property allows you to set both text and HTML markup. It's useful when you need to include formatting like bold, italics, or other HTML elements. Syntax selectedElement.innerHTML = "new text with HTML tags"; Example Change Label Text with innerHTML Original label text ...
Read MoreHow to get only first word of object's value – JavaScript?
When working with objects that contain string values with multiple words, you often need to extract only the first word. The most effective approach is using the split() method with a space delimiter. Sample Data Let's work with an employee object containing names and technologies: const employeeDetails = [ { employeeName: "John Smith", employeeTechnology: "JavaScript HTML" }, { employeeName: "David Miller", employeeTechnology: "Java ...
Read MoreReverse alphabetically sorted strings in JavaScript
We are required to write a JavaScript function that takes in a lowercase string and sorts it in the reverse alphabetical order i.e., 'z' should come before 'y', 'b' before 'a', and so on. Example If the input string is: const str = "hello"; Then the output should be: const output = "ollhe"; Using Custom Comparator Function Here's how we can achieve reverse alphabetical sorting using a custom comparator: const string = 'hello'; const sorter = (a, b) => { const legend = ...
Read MoreFinding two numbers that produce equal to the sum of rest in JavaScript
We have a sequence of numbers from 1 to any arbitrary number. We need to find pairs of numbers (m and n) from this sequence such that the sum of all remaining numbers equals the product of these two numbers. Problem Statement Given a number num, find all pairs [m, n] where: sum(1 to num) - (m + n) = m * n For example, if num = 10: Sum of 1 to 10 = 55 For pair [6, 7]: 55 - (6 + 7) = 42 and 6 × 7 = 42 ...
Read MoreFinding smallest sum after making transformations in JavaScript
We need to write a JavaScript function that takes an array of positive integers and applies transformations until no more are possible. The transformation rule is: if arr[i] > arr[j], then arr[i] = arr[i] - arr[j]. After all transformations, we return the sum of the array. Problem The key insight is that this transformation process eventually reduces all numbers to their Greatest Common Divisor (GCD). When we repeatedly subtract smaller numbers from larger ones, we're essentially performing the Euclidean algorithm. if arr[i] > arr[j] then arr[i] = arr[i] - arr[j] How It Works ...
Read MoreError: Permission denied to access property 'target' in JavaScript
"Error − Permission denied to access property 'target'" is a common error message that occurs when working with JavaScript, especially when attempting to access the target property of an event object. This error occurs when a script tries to access a property or method of an object that it does not have permission to access. In this tutorial, we will discuss the causes of this error and how to fix it. Understanding the Error The "Error − Permission denied to access property 'target'" is typically caused by the use of the target property of an event object in ...
Read MoreCall a function with onclick() – JavaScript?
The onclick event is a fundamental feature for calling functions in JavaScript when users interact with HTML elements. This event allows you to execute JavaScript code in response to mouse clicks on buttons, links, or any clickable element. What is the Onclick Event? The onclick event is triggered when a user clicks on an HTML element. It's commonly used to execute JavaScript functions in response to user interactions, making web pages interactive and responsive. You can call a function using the onclick event in two main ways: Using the onclick Attribute ...
Read MoreConstructing an object from repetitive numeral string in JavaScript
Suppose, we have a string with digits like this − const str = '11222233344444445666'; We are required to write a JavaScript function that takes in this string and returns an object that represents the count of each number in the string. So, for this string, the output should be − const output = { "1": 2, "2": 4, "3": 3, "4": 7, "5": 1, "6": 3 }; Using a for Loop The most straightforward approach is to iterate ...
Read More