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 480 of 801
Generating n random numbers between a range - JavaScript
We are required to write a JavaScript function that takes in a number, say n, and an array of two numbers that represents a range. The function should return an array of n random elements all lying between the range provided by the second argument. Understanding the Problem The challenge is to generate unique random numbers within a specified range. We need two helper functions: one to generate a single random number between two values, and another to collect n unique random numbers. Example Implementation Following is the code − const num = 10; ...
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 MoreWriting a For Loop to Evaluate a Factorial - JavaScript
We are required to write a simple JavaScript function that takes in a Number, say n and computes its factorial using a for loop and returns the factorial. For example: factorial(5) = 120, factorial(6) = 720 The approach is to maintain a count and a result variable, keep multiplying the count into result, simultaneously decreasing the count by 1, until it reaches 1. Then finally we return the result. Syntax function factorial(n) { let result = 1; for (let i = n; i > ...
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 MoreGetting equal or greater than number from the list of numbers in JavaScript
We are required to write a JavaScript function that takes in an array of numbers as the first argument and a single number as the second argument. The function should return an array of all the elements from the input array that are greater than or equal to the number taken as the second argument. Example Following is the code − const arr = [56, 34, 2, 7, 76, 4, 45, 3, 3, 34, 23, 2, 56, 5]; const threshold = 40; const findGreater = (arr, num) => { const res = ...
Read MoreRemove the whitespaces from a string using replace() in JavaScript?
In JavaScript, the replace()
Read MoreSum of all prime numbers in an array - JavaScript
We are required to write a JavaScript function that takes in an array of numbers and returns the sum of all the prime numbers present in the array. A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. For example, 2, 3, 5, 7, 11 are prime numbers. Let's say the following is our array: const arr = [43, 6, 6, 5, 54, 81, 71, 56, 8, 877, 4, 4]; The function should sum the prime numbers: 43 + 5 + 71 ...
Read MoreCheck if string begins with punctuation in JavaScript
To check if a string begins with punctuation in JavaScript, we can use regular expressions to test the first character against common punctuation marks. The Problem Consider these example strings where we need to detect if they start with punctuation: var sentence1 = 'My Name is John Smith.'; var sentence2 = '? My Name is John Smith.'; console.log("Testing strings:"); console.log("1:", sentence1); console.log("2:", sentence2); Testing strings: 1: My Name is John Smith. 2: ? My Name is John Smith. Using Regular Expression with match() We can create a regular expression ...
Read MoreFinding reversed index of elements in arrays - JavaScript
We need to create a JavaScript function that finds the reversed index of an element in an array without actually reversing it. This function calculates what position an element would occupy if the array were reversed. If the element is not present in the array, the function returns -1. Otherwise, it returns the index position the element would have in a reversed array. Understanding Reversed Index The reversed index is calculated using the formula: length - originalIndex - 1 For example, in array [45, 74, 34, 32, 23, 65]: Element 23 is at index 4 ...
Read More