AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 446 of 840

Explain 'Not a constructor function' error in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 401 Views

The "Not a constructor function" error occurs when we try to use the new keyword with a value that isn't a constructor function. This TypeError is thrown when JavaScript expects a constructor but receives a primitive value, object, or non-constructor function. What Causes This Error The error occurs when we attempt to instantiate something that cannot be used as a constructor: Primitive values (numbers, strings, booleans) Regular objects Arrow functions (in strict mode) Built-in methods that aren't constructors Example: Using Primitive as Constructor Constructor Error Demo ...

Read More

How to turn words into whole numbers JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 2K+ Views

We need to write a function that takes a string of number words as input and converts them into their equivalent whole number. Problem Overview Given a string containing number words separated by spaces, we want to convert each word to its corresponding digit and combine them into a single number. "one five seven eight" -------> 1578 "two eight eight eight" -------> 2888 Approach The solution involves creating a mapping of number words to digits, then iterating through each word to build the final number. We split the input string by whitespace ...

Read More

Counting the number of redundant characters in a string - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 327 Views

We are required to write a JavaScript function that takes in a string and returns the count of redundant characters in the string. A redundant character is any character that appears more than once, where all duplicate occurrences (except the last one) are considered redundant. For example − If the string is − const str = 'abcde' Then the output should be 0 because each character appears only once. If the string is − const str = 'aaacbfsc'; Then the output should be 3 because 'a' appears 3 times (2 ...

Read More

Check if a string is sorted in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 598 Views

We are required to write a JavaScript function that takes in a string and checks whether it is sorted or not. A string is considered sorted if its characters are arranged in either ascending or descending order. For example: isSorted('adefgjmxz') // true (ascending) isSorted('zxmfdba') // true (descending) isSorted('dsfdsfva') // false (mixed order) Method 1: Character by Character Comparison This approach compares adjacent characters and tracks whether the string is ascending or descending: const str = 'abdfhlmxz'; const findDiff = (a, b) => a.charCodeAt(0) - b.charCodeAt(0); ...

Read More

JavaScript Error message Property

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 273 Views

The message property of JavaScript Error objects contains a human-readable description of the error. It's automatically set when an error occurs and can also be customized when creating custom errors. Syntax error.message Example: Accessing Error Message JavaScript Error message Property body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .sample { font-size: 18px; ...

Read More

How to create a draggable HTML element with JavaScript and CSS?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 749 Views

To create a draggable HTML element with JavaScript and CSS, you need to handle mouse events and update the element's position dynamically. This technique allows users to click and drag elements around the page. Basic HTML Structure Start with an HTML element that has absolute positioning and a move cursor to indicate it's draggable: body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .dragDiv { position: absolute; ...

Read More

How to de-structure an imported object in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 223 Views

Destructuring allows you to extract specific properties from imported objects in JavaScript modules. This is particularly useful when you only need certain properties from a larger object. Basic Destructuring Syntax When importing an object, you can destructure it immediately or after import: // Method 1: Destructure after import import person from "./sample.js"; let {firstName, lastName, age} = person; // Method 2: Direct destructuring (named exports) import {firstName, lastName, age} from "./sample.js"; Example: Complete Implementation sample.js (Module file) export default { firstName: 'Rohan', ...

Read More

Display the Asian and American Date Time with Date Object in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 2K+ Views

JavaScript's Date object provides powerful timezone handling through the toLocaleString() method. You can display dates and times for different regions by specifying timezone identifiers. Syntax new Date().toLocaleString("en-US", {timeZone: "timezone_identifier"}); Parameters locale: Language and region format (e.g., "en-US", "en-GB") timeZone: IANA timezone identifier (e.g., "Asia/Kolkata", "America/New_York") Asian Time Zone Example // Display current time in Asian timezone (India) var asianDateTime = new Date().toLocaleString("en-US", { timeZone: "Asia/Kolkata" }); console.log("Asian Date Time (India):"); console.log(asianDateTime); // Convert to Date object for further processing var asianDateObject = new ...

Read More

JavaScript Error name Property

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 232 Views

The name property of JavaScript Error objects identifies the type of error that occurred. It returns a string representing the error's name, which helps in debugging and error handling. Syntax error.name Common Error Names Different error types have specific names: ReferenceError - Variable or function not defined TypeError - Wrong data type used SyntaxError - Invalid syntax RangeError - Number out of range Example: Displaying Error Names Error Name Property ...

Read More

How to use media queries with JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 401 Views

JavaScript provides the window.matchMedia() method to detect and respond to CSS media query changes dynamically. This allows you to create responsive behavior that adapts to screen size, device orientation, and other media features. Basic Syntax let mediaQuery = window.matchMedia("(max-width: 768px)"); // Check if query matches if (mediaQuery.matches) { // Screen is 768px or smaller } // Listen for changes mediaQuery.addEventListener('change', function(e) { if (e.matches) { // Query now matches } }); Example: Responsive ...

Read More
Showing 4451–4460 of 8,392 articles
« Prev 1 444 445 446 447 448 840 Next »
Advertisements