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 163 of 589
How to create a custom function similar to find() method in JavaScript?
Let's say we have the following records of studentId and studentName and want to check a specific student name: const studentDetails = [ { studentId: 101, studentName: "John" }, { studentId: 102, studentName: "David" }, { studentId: 103, ...
Read MoreFinding letter distance in strings - JavaScript
We are required to write a JavaScript function that takes in a string as first argument and two single element strings. The function should return the distance between those single letter strings in the string taken as first argument. For example − If the three strings are − const str = 'Disaster management'; const a = 'i', b = 't'; Then the output should be 4 because the distance between 'i' and 't' is 4 Understanding Letter Distance Letter distance is the absolute difference between the index positions of two characters in ...
Read MoreCounting elements of an array using a recursive function in JS?
A recursive function calls itself until a base condition is met. In JavaScript, we can use recursion to count array elements by reducing the array size in each recursive call. How Recursive Counting Works The recursive approach works by: Base case: If array is empty, return 0 Recursive case: Return 1 + count of remaining elements Example function countNumberOfElementsUsingRecursive(listOfMarks) { if (listOfMarks.length == 0) { return 0; } return 1 + countNumberOfElementsUsingRecursive(listOfMarks.slice(1)); } ...
Read MoreFunction that parses number embedded in strings - JavaScript
JavaScript's built-in functions like parseInt() and parseFloat() only parse numbers from the beginning of a string, stopping when they encounter non-numeric characters. When you need to extract all digits from anywhere within a string, you need a custom solution. The Problem with Built-in Methods Standard parsing methods fail with embedded numbers: console.log(parseInt('454ffdg54hg53')); // 454 (stops at first non-digit) console.log(parseFloat('12.34abc56.78')); // 12.34 (stops at 'a') 454 12.34 Method 1: Loop Through Characters This approach iterates through each character, extracting only digits: const numStr = '454ffdg54hg53'; ...
Read MoreDisplay whether the office is closed or open right now on the basis of current time with JavaScript ternary operator
Let's say we are matching the current date and time with the business hours. We need to display whether the office is closed or open right now on the basis of current time. We can get the current hour from the Date object and use the JavaScript ternary operator to determine if the office is open or closed based on business hours. Syntax const currentHour = new Date().getHours(); const status = (condition) ? 'Open' : 'Closed'; Example Office ...
Read MoreCreate new array without impacting values from old array in JavaScript?
In JavaScript, directly assigning an array to a new variable creates a reference to the original array, not a copy. This means changes to the new variable will affect the original array. To create a truly independent copy, you need to use specific cloning methods. The Problem with Direct Assignment When you assign an array directly, both variables point to the same array in memory: var originalArray = ["John", "Mike", "Sam", "Carol"]; var newArray = originalArray; // This creates a reference, not a copy newArray.push("David"); console.log("Original array:", originalArray); console.log("New array:", newArray); ...
Read MoreMatching strings for similar characters - JavaScript
We are required to write a JavaScript function that accepts two strings and a number n. The function matches the two strings i.e., it checks if the two strings contains the same characters. The function returns true if both the strings contain the same character irrespective of their order or if they contain at most n different characters, else the function should return false. Example Following is the code − const str = 'some random text'; const str2 = 'some r@ndom text'; const deviationMatching = (first, second, num) => { ...
Read MoreHow to display JavaScript array items one at a time in reverse on button click?
In this tutorial, we'll learn how to display JavaScript array items one at a time in reverse order when a button is clicked. This technique is useful for creating step-by-step displays or interactive presentations. The Array and Button Setup First, let's define our array of names: var listOfNames = [ 'John', 'David', 'Bob' ]; And create a button that will trigger the reverse display: Click The Button To get the Reverse Value How It Works The approach ...
Read MoreSplitting last n digits of each value in the array in JavaScript
We have an array of numbers and need to extract the last n digits from each value. If a number has fewer than n digits, it remains unchanged. const arr = [56768, 5465, 5467, 3, 878, 878, 34435, 78799]; console.log("Original array:", arr); Original array: [ 56768, 5465, 5467, 3, 878, 878, 34435, 78799 ] We need a function that takes an array and a number n, then returns the last n digits of each element. If an element has fewer than n digits, it stays the same. Example with n = 2 ...
Read MoreHTML form action and onsubmit validations with JavaScript?
HTML form validation is essential for ensuring data integrity before submission. The onsubmit event allows you to validate form data and prevent submission if validation fails. How Form Validation Works The onsubmit event handler must return true to allow submission or false to prevent it. When validation fails, the form submission is blocked. Basic Validation Example Form Validation Example Enter "gmail" to proceed: ...
Read More