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 151 of 589
Remove all whitespaces from string - JavaScript
We are required to write a JavaScript function that takes in a string and returns a new string with all the characters of the original string but with whitespaces removed. Method 1: Using a For Loop This approach iterates through each character and builds a new string excluding spaces: const str = "This is an example string from which all whitespaces will be removed"; const removeWhitespaces = str => { let newStr = ''; for(let i = 0; i < str.length; i++){ ...
Read MoreJavaScript get the length of select options(dropdown)?
In JavaScript, you can get the length of select options (dropdown) using different methods. This is useful for validation, dynamic content management, or determining how many options are available. HTML Structure Let's start with a basic select element: John Mike Sam David Carol Using Vanilla JavaScript The most straightforward approach uses the options.length property: ...
Read MoreReplacing upperCase and LowerCase in a string - JavaScript
We are required to write a JavaScript function that takes in a string and constructs a new string with all the uppercase characters converted to lowercase and all the lowercase characters converted to uppercase. Let's write the code for this function — Example Following is the code — const str = 'The Case OF tHis StrinG Will Be FLiPped'; const isUpperCase = char => char.charCodeAt(0) >= 65 && char.charCodeAt(0) char.charCodeAt(0) >= 97 && char.charCodeAt(0) { let newStr = ''; const margin = 32; ...
Read MoreJavaScript Sum function on the click of a button
In JavaScript, you can create a sum function that executes when a button is clicked. This involves defining a function and attaching it to the button's onclick event. Basic Button Setup Here's how to create a button that calls a function with a parameter: Sum When clicked, this button calls the addTheValue() function with the value 10 as a parameter. Complete Example Sum Function Example Adding 10 each ...
Read MoreFinding duplicate "words" in a string - JavaScript
We need to write a JavaScript function that takes a string and returns only the words that appear more than once in the original string. For example, if the input string is: const str = "big black bug bit a big black dog on his big black nose"; Then the output should be: "big black" Solution Using indexOf() and lastIndexOf() We can identify duplicates by comparing the first and last occurrence positions of each word: const str = "big black bug bit a big black dog on his ...
Read MoreaddEventListener() not working more than once with a button in JavaScript?
The addEventListener() method works multiple times by design. If it seems to work only once, the issue is usually in your event handler function or how you're setting up the listener. Common Problem: Removing the Element A frequent mistake is removing or replacing the button element inside the event handler, which destroys the event listener: addEventListener Problem Bad Example - Works Once ...
Read MoreJavaScript How to get all 'name' values in a JSON array?
In JavaScript, extracting specific values from a JSON array is a common task. When working with nested objects, you can use the map() method to iterate through the array and extract the desired properties. Sample JSON Data Let's consider the following JSON array with customer information: var details = [ { "customerDetails": [ { "customerName": "John ...
Read MoreFinding roots of a quadratic equation – JavaScript
A quadratic equation has the form ax² + bx + c = 0, where a, b, and c are coefficients. To find the roots, we use the quadratic formula and check if the discriminant is non-negative for real roots. Quadratic Formula The quadratic formula is: x = (-b ± √(b² - 4ac)) / (2a) The discriminant (b² - 4ac) determines the nature of roots: If discriminant > 0: Two distinct real roots If discriminant = 0: One repeated real root If discriminant < 0: No real roots (complex roots) Example ...
Read MoreBest way to find two numbers in an array whose sum is a specific number with JavaScript?
Finding two numbers in an array that sum to a target value is a common programming problem. We'll explore an efficient solution using JavaScript. Problem Statement Given an array of numbers, find two elements whose sum equals a specific target value: var numbers = [10, 3, 40, 50, 20, 30, 100] Target sum = 80 We need to find pairs like (50, 30) where 50 + 30 = 80. Efficient Solution Using Hash Map The optimal approach uses a hash map to store visited numbers and their complements, achieving O(n) time complexity: ...
Read MoreNearest power 2 of a number - JavaScript
We need to write a JavaScript function that takes a number and returns the nearest power of 2. A power of 2 is any number that can be expressed as 2n where n is a whole number (1, 2, 4, 8, 16, 32, 64, 128, 256, etc.). For example, if the input is 365, the output should be 256 because 256 (28) is closer to 365 than the next power of 2, which is 512 (29). Algorithm Explanation The algorithm works by: Starting with base = 1 (20) Doubling the base until it exceeds the input ...
Read More