AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 488 of 840

Check for Ugly number in JavaScript

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

In the decimal number system, ugly numbers are positive integers whose only prime factors are 2, 3, or 5. This means an ugly number can be expressed as 2i × 3j × 5k where i, j, and k are non-negative integers. For example, the integers 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 are all ugly numbers because they only contain the prime factors 2, 3, and 5. However, 7, 11, 13, 14 are not ugly numbers as they contain other prime factors. Algorithm To check if a number is ugly, we repeatedly divide it ...

Read More

Create global variable in jQuery outside document.ready function?

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

To create a global variable in jQuery that can be accessed outside the document.ready function, you need to declare the variable in the global scope (outside any function) within the tag. Key Concepts Global variables in JavaScript are accessible from anywhere in your code. When working with jQuery, you can initialize global variables inside document.ready and use them in other functions. Example Global Variable in jQuery ...

Read More

Update JavaScript object with another object, but only existing keys?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 983 Views

To update a JavaScript object with values from another object while only affecting existing keys, you can use hasOwnProperty() to check if a key exists in the source object before updating it. Example var markDetails1 = { 'marks1': 78, 'marks2': 65 }; var markDetails2 = { 'marks2': 89, 'marks3': 90 }; function updateJavaScriptObject(details1, details2) { const outputObject = {}; Object.keys(details1) .forEach(obj => outputObject[obj] ...

Read More

Transpose of a two-dimensional array - JavaScript

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

The transpose of a matrix (2-D array) is simply a flipped version of the original matrix. We can transpose a matrix by switching its rows with its columns. For example, if we have a 3×3 matrix where the first row is [1, 1, 1], after transposing, the first column becomes [1, 1, 1]. Understanding Matrix Transpose Let's visualize how transposition works with a simple example: Original Matrix: [1, 1, 1] [2, 2, 2] [3, 3, 3] Transposed Matrix: [1, 2, 3] [1, 2, 3] [1, 2, 3] Method 1: In-Place Transpose ...

Read More

Array of adjacent element's average - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 323 Views

Let's say, we have an array of numbers: const arr = [3, 5, 7, 8, 3, 5, 7, 4, 2, 8, 4, 2, 1]; We are required to write a function that returns an array with the average of the corresponding element and its predecessor. For the first element, as there are no predecessors, so that very element should be returned. Let's write the code for this function, we will use the Array.prototype.map() function to solve this problem: Example const arr = [3, 5, 7, 8, 3, 5, 7, 4, 2, 8, ...

Read More

TypeError: 'undefined' is not an object in JavaScript

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

The "TypeError: 'undefined' is not an object" error occurs when you try to access properties or call methods on an undefined variable. This error message is specific to Safari browser, while other browsers show similar but differently worded errors. What Causes This Error This error happens when: A variable is declared but not assigned a value An object property doesn't exist A function returns undefined and you try to access its properties Example: Accessing Property of Undefined Variable ...

Read More

Build maximum array based on a 2-D array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 168 Views

Let's say, we have an array of arrays of Numbers like below − const arr = [ [1, 16, 34, 48], [6, 66, 2, 98], [43, 8, 65, 43], [32, 98, 76, 83], [65, 89, 32, 4], ]; We are required to write a function that maps over this array of arrays and returns an array that contains the maximum (greatest) element from each subarray. So, for the above array, the output should be − [48, 98, 65, 98, 89] Using ...

Read More

How to sort strings with accented characters using JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 469 Views

JavaScript's default string sorting doesn't handle accented characters properly. The localeCompare() method provides locale-aware string comparison that correctly sorts accented characters. The Problem with Default Sort Using the default sort() method on strings with accented characters produces incorrect results because it compares Unicode code points rather than the actual alphabetical order. Default Sort Problem Default Sort (Incorrect) ...

Read More

Calling asynchronous code from index.html page in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 333 Views

Calling asynchronous code when an HTML page loads requires using async/await with event handlers. This allows you to run asynchronous operations during page initialization. Using onload with Async Function The most straightforward approach is to use the onload attribute with an async function: Async Code on Page Load Async Page Loading Example async function ...

Read More

Simplest code for array intersection in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 225 Views

Array intersection finds common elements between two or more arrays. In JavaScript, the simplest approach uses filter() combined with includes(). Basic Example Let's find common elements between two arrays: var firstNamesArray = ["John", "David", "Bob", "Sam", "Carol"]; var secondNamesArray = ["Mike", "Carol", "Adam", "David"]; var intersectionOfArray = firstNamesArray.filter(v => secondNamesArray.includes(v)); console.log("Intersection of two arrays:"); console.log(intersectionOfArray); Intersection of two arrays: [ 'David', 'Carol' ] How It Works The filter() method creates a new array with elements that pass the test. For each element in ...

Read More
Showing 4871–4880 of 8,392 articles
« Prev 1 486 487 488 489 490 840 Next »
Advertisements