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 112 of 589
JavaScript Compare two sentences word by word and return if they are substring of each other
The idea here is to take two strings as input and return true if one string is a substring of the other, otherwise return false. For example: isSubstr('hello', 'hello world') // true isSubstr('can I use', 'I us') // true isSubstr('can', 'no we are') // false In the function, we check which string is longer and then verify if the shorter string exists as a substring within the longer one. Syntax const isSubstr = (first, second) => { if (first.length > second.length) { ...
Read MoreHow can we validate decimal numbers in JavaScript?
Validating decimal numbers in JavaScript is essential for form validation and data processing. There are several reliable methods to check if a value is a valid decimal number. Method 1: Using Regular Expression Regular expressions provide precise control over decimal number validation patterns: Decimal Validation Decimal Number Validation Validate ...
Read MoreHow to find keys of a hash in JavaScript?
In JavaScript, objects act as hash tables where you can store key-value pairs. To extract all keys from an object, use the built-in Object.keys() method, which returns an array of the object's property names. Syntax Object.keys(object) Parameters object: The object whose keys you want to retrieve. Return Value Returns an array of strings representing the object's own enumerable property names (keys). Example Find Hash Keys ...
Read MoreHow to selectively retrieve value from json output JavaScript
We have the following data inside a JSON file data.json: data.json { "names": [{ "name": "Ramesh", "readable": true }, { "name": "Suresh", "readable": false }, { "name": "Mahesh", "readable": true }, { "name": "Gourav", "readable": true }, { "name": "Mike", "readable": false }] } ...
Read MoreJavaScript program to decrement a date by 1 day
JavaScript provides several methods to manipulate dates. To decrement a date by 1 day, we can use the setDate() and getDate() methods together. Syntax date.setDate(date.getDate() - 1); Parameters date.getDate() - Returns the current day of the month (1-31) date.setDate() - Sets the day of the month for the date object Example: Basic Date Decrement Decrement Date by 1 Day body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; ...
Read MoreEquivalent of Ruby's each cons in JavaScript
Ruby's each_cons() method iterates through consecutive N elements of an enumerable, creating subarrays of fixed size. JavaScript doesn't have this built-in, but we can implement it using Array prototype extension. Understanding each_cons Behavior The each_cons(n) function creates sliding windows of size n from an array. For each starting position, it takes n consecutive elements until there aren't enough remaining elements. const arr = [1, 2, 3, 4, 5]; // With n=2: creates pairs starting from each position // Result: [[1, 2], [2, 3], [3, 4], [4, 5]] // With n=3: creates triplets starting from ...
Read MoreHow to implement asynchronous loop in JavaScript?
Asynchronous loops in JavaScript allow you to perform time-delayed or async operations within loops without blocking the main thread. This is essential when working with APIs, file operations, or timed delays. What is an Asynchronous Loop? A regular loop executes all iterations instantly. An asynchronous loop waits for each iteration to complete before moving to the next, using await with async functions. Using async/await with for Loop Asynchronous Loop ...
Read MoreNo. of ways to empty an array in JavaScript
JavaScript provides several methods to empty an array. Each method has different use cases and behaviors, especially when multiple references to the same array exist. Method 1: Setting to New Array This method creates a new empty array and assigns it to the variable. However, it doesn't affect other references to the original array. Setting to New Array let arr = [1, 2, 3, ...
Read MorePassing parameters to callback functions JavaScript
Callback functions in JavaScript can accept parameters just like regular functions. When passing a callback to another function, you can provide parameters that the callback will use when executed. Basic Callback with Parameters A callback function receives parameters when it's invoked by the calling function: Callback Parameters Run Example function add2(a) { ...
Read MoreJavaScript function to accept a string and mirrors its alphabet
We need to write a function that accepts a string and mirrors its alphabet. This means each letter is replaced with its counterpart from the opposite end of the alphabet. If the input is 'abcd' The output should be 'zyxw' The function maps every character to the letter that is (26 - N) positions away from it, where N is the 1-based index of that alphabet (like 5 for 'e' and 10 for 'j'). How It Works We use the String.prototype.replace() method to match all English alphabets regardless of case. For each letter: ...
Read More