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 426 of 840
Simplest way to detect keypresses in JavaScript?
The simplest way to detect keypresses in JavaScript is using the onkeypress event handler. However, modern JavaScript also provides addEventListener for better event handling. document.onkeypress The key press is matched with the keyCode property, which returns the Unicode character code of the key that triggered the onkeypress event. Using document.onkeypress Keypress Detection Press any key to see the detection in action! ...
Read MoreDetermining rightness of a triangle – JavaScript
We are required to write a JavaScript function that takes in three numbers say a, b and c representing the length of three sides of a triangle. The function should return true if those three sides represent a right-angle triangle, false otherwise. Right Angle Triangle A triangle is a right-angle triangle if one of the three angles in the triangle is 90 degrees. And one angle in the triangle is 90 degrees when the square of the longest side is equal to the sum of squares of the other two sides. This is known as the Pythagorean ...
Read MoreObject.keys().map() VS Array.map() in JavaScript
In JavaScript, Object.keys().map() and Array.map() serve different purposes. Object.keys() extracts object keys as an array, then applies map(), while Array.map() directly transforms array elements. Understanding Object.keys() Object.keys() returns an array of an object's property names (keys). When chained with map(), it allows you to transform these keys. Object Keys Example let obj = { ...
Read MoreMerge objects in array with similar key JavaScript
When working with arrays of objects in JavaScript, you might need to merge objects that share a common key. This is useful when consolidating data from multiple sources or restructuring existing data. Let's say we have the following array of objects where each object has an id and various heading properties: const arr = [ {id: 1, h1: 'Daily tests'}, {id: 2, h1: 'Details'}, {id: 1, h2: 'Daily classes'}, {id: 3, h2: 'Results'}, {id: 2, h3: 'Admissions'}, ...
Read MoreStrictly increasing or decreasing array - JavaScript
In Mathematics, a strictly increasing function is one where values always increase, while a strictly decreasing function has values that always decrease. In JavaScript, we can check if an array follows either pattern. We need to write a function that takes an array of numbers and returns true if it's either strictly increasing or strictly decreasing, otherwise returns false. Understanding the Logic The key insight is to check if consecutive elements maintain the same slope (all increasing or all decreasing). We use a helper function sameSlope that compares three consecutive numbers to ensure they follow the same ...
Read MoreHow to share private members among common instances in JavaScript?
In JavaScript, private members are variables that cannot be accessed directly from outside a constructor function or class. However, you can share private members among instances using closures and static variables. Understanding Private Members Private members are created using local variables inside constructor functions. They can only be accessed through public methods defined within the same scope. Example: Basic Private Members Private Members Example Share Private Members Among Common Instances ...
Read MoreInverse operation in JavaScript
The inverse operation on a binary string involves flipping each bit: converting all 0s to 1s and all 1s to 0s. This is a common operation in computer science and digital logic. Understanding Binary Inverse Binary inverse (also called bitwise NOT or complement) transforms each digit in a binary string to its opposite value. For example, '1101' becomes '0010'. Method 1: Using Array Methods We can split the string into individual characters, transform each one, and join them back: const num = '1101'; const n = '11010111'; const inverseBinary = (binary) => { ...
Read MoreChecking smooth sentences in JavaScript
We are required to write a JavaScript function that checks whether a sentence is smooth or not. A sentence is smooth when the first letter of each word in the sentence is same as the last letter of its preceding word. How It Works A smooth sentence follows this pattern: if word A ends with letter 'x', then word B must start with letter 'x'. For example, "this stringt tries" is smooth because "this" ends with 's' and "stringt" starts with 's', "stringt" ends with 't' and "tries" starts with 't'. Example Following is the code ...
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 duplicate elements of an array in the same array with JavaScript?
Duplicating array elements means creating copies of each element within the same array. JavaScript provides several methods to achieve this, with concat() being the most straightforward approach. Using concat() Method The concat() method merges arrays and returns a new array. When you concatenate an array with itself, you effectively duplicate all elements. Duplicate Array Elements body { ...
Read More