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
Object Oriented Programming Articles
Page 114 of 589
Construct 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 MoreJavaScript code to de-select text on HTML page.
In JavaScript, you can deselect or clear text selections on an HTML page using the window.getSelection().removeAllRanges() method. This is useful for creating interactive interfaces where you want to programmatically clear user text selections. The Selection API The Selection API provides methods to work with text selections in the browser. The window.getSelection() object represents the current text selection, and removeAllRanges() clears all selected ranges. Syntax window.getSelection().removeAllRanges(); Example Deselect Text Example ...
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 MoreSelect and Deselect Text Inside an Element using JavaScript
In JavaScript, you can programmatically select and deselect text inside DOM elements using the Selection API. This is useful for creating interactive features like highlighting content or implementing custom text selection controls. Core Methods The key methods for text selection are: window.getSelection().selectAllChildren(element) - Selects all text inside an element window.getSelection().removeAllRanges() - Clears all current selections Example Select and Deselect Text body { ...
Read MoreProgram to retrieve the text contents of the user selection using JavaScript.
In JavaScript, you can retrieve the text contents of a user's selection using the window.getSelection() method. This is useful for creating text highlighting features, copy functionality, or interactive reading applications. How It Works The window.getSelection() method returns a Selection object representing the range of text selected by the user. You can convert this to a string using the toString() method. Example Text Selection Demo body ...
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 MoreCircle coordinates to array in JavaScript
In JavaScript, you can generate circle coordinates by using trigonometric functions to calculate points around a circle's circumference. This is useful for animations, graphics, and positioning elements in circular patterns. How Circle Coordinates Work A circle's coordinates are calculated using the parametric equations: X coordinate: centerX + radius × cos(angle) Y coordinate: centerY + radius × sin(angle) By dividing the circle into equal steps and calculating coordinates at each angle, we can create an array of points around the circle. Example ...
Read MoreJavaScript code to find the coordinates of every link in a page
JavaScript provides several methods to find the position and coordinates of elements on a page. For links, we can use properties like offsetLeft, offsetTop, offsetWidth, and offsetHeight to get their position and dimensions. Understanding Element Coordinates In JavaScript, element coordinates are measured from the top-left corner of the page: offsetLeft - Distance from the left edge of the page offsetTop - Distance from the top edge of the page offsetWidth - Width of the element including padding and borders offsetHeight - Height of the element including padding and borders Example ...
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 MoreAlert for unsaved changes in form in JavaScript
Preventing data loss is crucial in web forms. JavaScript's beforeunload event allows you to warn users when they attempt to leave a page with unsaved changes. Basic Implementation The simplest approach uses the beforeunload event to show a browser confirmation dialog: Form Unsaved Changes Alert body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; ...
Read More