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
Articles by AmitDiwan
Page 411 of 840
Partially reversing an array - JavaScript
Suppose, we have an array of literals like this: const arr = [3, 5, 5, 2, 23, 4, 7, 8, 8, 9]; console.log("Original array:", arr); Original array: [3, 5, 5, 2, 23, 4, 7, 8, 8, 9] We are required to write a JavaScript function that takes in such an array and a number, say n (n must be less than or equal to the length of array). And the function should reverse the first n elements of the array within. For example, if for this array, the number is 4, then ...
Read MoreHow to create a filter list with JavaScript?
JavaScript filter lists allow users to search and filter through data dynamically. This is commonly used for search functionality in websites and applications. Complete HTML Example * { box-sizing: border-box; } .searchInput { width: 100%; font-size: 16px; ...
Read MoreJavaScript Promises
Promises in JavaScript allow us to handle asynchronous operations where the value is not known in advance when the promise is being created. A promise can have three states: pending, fulfilled, and rejected. Promise States A promise represents an asynchronous operation and can be in one of three states: Pending: Initial state, neither fulfilled nor rejected Fulfilled: The operation completed successfully Rejected: The operation failed Creating a Basic Promise Here's how to create a simple promise: ...
Read MoreEffective Function Signatures with Default and Rest Parameters in JavaScript
JavaScript function signatures with default and rest parameters allow you to create flexible functions that handle variable arguments and provide fallback values when parameters are missing. Default Parameters Default parameters provide fallback values when arguments are undefined or not passed: Default Parameters function greet(name = "Guest", greeting = "Hello") { return `${greeting}, ...
Read MoreJavaScript Return the lowest index at which a value should be inserted into an array once it has been sorted (either in ascending or descending order).
We have to write a function that returns the lowest index at which a value (second argument) should be inserted into an array (first argument) once it has been sorted (either in ascending or descending order). The returned value should be a number. For example, Let's say, we have a function getIndexToInsert() − getIndexToInsert([1, 2, 3, 4], 1.5, 'asc') should return 1 because it is greater than 1 (index 0), but less than 2 (index 1). Likewise, getIndexToInsert([20, 3, 5], 19, 'asc') should return 2 because once the array has been sorted in ...
Read MoreAppending a key value pair to an array of dictionary based on a condition in JavaScript?
In JavaScript, you can append key-value pairs to objects within an array or dictionary based on conditions using Object.assign() or the spread operator. This is useful when you need to add properties conditionally. Problem Statement Given a dictionary of student objects, we want to add a lastName property based on whether the student's name appears in a specific array. Using Object.assign() with Conditional Logic The following example demonstrates how to append a lastName property to each student object based on a condition: const details = { john: {'studentName': 'John'}, ...
Read MoreHow to remove every Nth element from an array JavaScript?
Removing every Nth element from an array is a common task in JavaScript. This can be accomplished using the Array.prototype.splice() method to modify the array in-place, or by creating a new array with the filtered elements. Method 1: Using splice() (In-Place Modification) The splice() method removes elements from the array and modifies the original array: const arr = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']; console.log("Original array:", arr); const removeNth = (arr, n) => { // Start from n-1 index and remove every nth element ...
Read MoreThe best way to remove duplicates from an array of objects in JavaScript?
When working with arrays of objects in JavaScript, removing duplicates requires checking specific properties rather than comparing entire objects. Here are the most effective approaches. Sample Data Let's use this array of student objects with duplicate IDs: var studentDetails = [ {studentId: 101}, {studentId: 104}, {studentId: 106}, {studentId: 104}, {studentId: 110}, {studentId: 106} ]; console.log("Original array:", studentDetails); Original array: [ { studentId: 101 }, ...
Read MoreFinding tidy numbers - JavaScript
A tidy number is a number whose digits are in non-decreasing order. We are required to write a JavaScript function that takes in a number and checks whether its a tidy number or not. For example: 489 is a tidy number 234557 is also a tidy number 34535 is not a tidy number Understanding Tidy Numbers In a tidy number, each digit should be less than or equal to the next digit when reading from left to right. For instance, in 234789, we have 2 ≤ 3 ≤ 4 ≤ 7 ≤ 8 ≤ ...
Read MoreHow to import an Object with Sub Objects and Arrays in JavaScript?
Importing objects with nested sub-objects and arrays in JavaScript using ES6 modules allows for modular code organization. This technique is essential for sharing complex data structures between different JavaScript files. What are ES6 Modules? ES6 modules use export and import statements to share code between files. The export default syntax allows exporting a single object, function, or value as the default export from a module. Example: Importing Complex Objects Let's create a complete example showing how to import an object containing nested objects and arrays. sample.js (Module file) export default { ...
Read More