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 102 of 589
How to make a function that returns the factorial of each integer in an array JavaScript
We are here required to write a JavaScript function that takes in an array of numbers and returns another array with the factorial of corresponding elements of the array. We will first write a recursive method that takes in a number and returns its factorial and then we will iterate over the array, calculating the factorial of each element of array and then finally we will return the new array of factorials. Therefore, let's write the code for this Creating the Factorial Function First, we'll create a recursive function to calculate the factorial of a single number: ...
Read MoreHow to import and export a module/library in JavaScript?
Note − To run this example you will need to run a localhost server. JavaScript modules allow you to break code into separate files and share functionality between them using export and import statements. This promotes code reusability and better organization. Example INDEX.html Document body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; ...
Read MoreWhat are associative Arrays in JavaScript?
Associative arrays are basically objects in JavaScript where indexes are replaced by user-defined keys. They do not have a length property like normal arrays and cannot be traversed using a normal for loop. What are Associative Arrays? In JavaScript, what we call "associative arrays" are actually objects. Unlike traditional arrays that use numeric indexes, associative arrays use string keys to access values. This makes them perfect for storing key-value pairs where you need meaningful identifiers. Creating Associative Arrays You can create associative arrays using object literal syntax or by assigning properties to an empty object: ...
Read MoreExplain common code blocks in JavaScript switch statement?
Common code blocks in JavaScript switch statements allow multiple cases to share the same execution code. This is achieved by omitting the break statement, causing execution to "fall through" to subsequent cases. What are Common Code Blocks? Common code blocks occur when multiple case values need to execute the same code. Instead of duplicating code, you can group cases together by omitting break statements. Syntax switch (expression) { case value1: case value2: case value3: // ...
Read MoreExplain Strict Comparison in JavaScript switch statement?
The JavaScript switch statement performs strict comparison (===) to match values. This means both value and type must be identical for a case to execute. If no strict match is found, the default case runs. Understanding Strict Comparison Strict comparison checks both value and type. For example, the number 1 is not equal to the string "1" in strict comparison: Strict Comparison Demo // Demonstrating strict vs loose comparison ...
Read MoreExplain JavaScript Regular Expression modifiers with examples
JavaScript regular expression modifiers are optional flags that modify how the pattern matching behaves. These modifiers enable features like case-insensitive matching, global searching, and multiline matching. Available Modifiers Modifier Name Description ...
Read MoreHow to test and execute a regular expression in JavaScript?
JavaScript provides two main methods for testing and executing regular expressions: test() and exec(). The test() method returns a boolean indicating if a pattern matches, while exec() returns detailed match information or null. Regular Expression Methods There are two primary ways to work with regular expressions in JavaScript: test() - Returns true/false if pattern matches exec() - Returns match details or null Example: Using test() and exec() Regular Expression Testing ...
Read MoreFind n highest values in an object JavaScript
Let's say, we have an object that describes various qualities of a football player like this − const qualities = { defence: 82, attack: 92, heading: 91, pace: 96, dribbling: 88, tenacity: 97, vision: 91, passing: 95, shooting: 90 }; We wish to write a function that takes in such object and a number n (n ≤ no. of keys ...
Read MoreHow to preview an image before it is uploaded in JavaScript?
In JavaScript, you can preview an image before uploading using the FileReader API. This allows users to see their selected image immediately without needing to upload it to a server first. How FileReader Works The FileReader API reads file contents asynchronously. For image preview, we use readAsDataURL() which converts the image file into a base64 data URL that can be displayed in an element. Example Image Preview Example ...
Read MoreHow to separate alphabets and numbers from an array using JavaScript
We have an array that contains some number literals and some string literals mixed, and we are required to write a sorting function that separates the two and the two types should also be sorted within. This approach uses a custom comparator function with JavaScript's sort() method to first separate numbers from strings, then sort each type individually. Using Custom Sort Comparator The code for this sorting function will be: const arr = [1, 5, 'fd', 6, 'as', 'a', 'cx', 43, 's', 51, 7]; const sorter = (a, b) => { ...
Read More