AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 401 of 840

How to remove blank (undefined) elements from JavaScript array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 793 Views

When working with JavaScript arrays, you may encounter sparse arrays containing empty slots (undefined elements). These gaps can occur when elements are deleted or when arrays are created with missing values. const arr = [4, 6, , 45, 3, 345, , 56, 6]; console.log(arr); console.log("Length:", arr.length); [ 4, 6, , 45, 3, 345, , 56, 6 ] Length: 9 We need to remove only the undefined and empty values, not all falsy values like 0, false, or empty strings. Method 1: Using splice() with for loop Use a for loop to ...

Read More

Program to retrieve the text contents of the user selection using JavaScript.

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 183 Views

In JavaScript, you can retrieve the text contents of a user's selection using the window.getSelection() method. This is useful for creating text highlighting features, copy functionality, or interactive reading applications. How It Works The window.getSelection() method returns a Selection object representing the range of text selected by the user. You can convert this to a string using the toString() method. Example Text Selection Demo body ...

Read More

How to create a function which returns only even numbers in JavaScript array?

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

Here, we need to write a function that takes one argument, which is an array of numbers, and returns an array that contains only the numbers from the input array that are even. So, let's name the function as returnEvenArray, the code for the function will be − Example const arr = [3, 5, 6, 7, 8, 4, 2, 1, 66, 77]; const returnEvenArray = (arr) => { return arr.filter(el => { return el % 2 === 0; }) }; ...

Read More

How to add properties from one object into another without overwriting in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 700 Views

When merging objects in JavaScript, you often want to add properties from one object to another without overwriting existing values. This preserves the original object's data while incorporating new properties. The Problem Consider these two objects where some properties overlap: var first = {key1: 100, key2: 40, key3: 70}; var second = {key2: 80, key3: 70, key4: 1000}; console.log("First object:", first); console.log("Second object:", second); First object: { key1: 100, key2: 40, key3: 70 } Second object: { key2: 80, key3: 70, key4: 1000 } We want to add key4 from ...

Read More

Sum similar numeric values within array of objects - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 955 Views

Suppose, we have an array of objects like this — const arr = [ {"firstName":"John", "value": 89}, {"firstName":"Peter", "value": 151}, {"firstName":"Anna", "value": 200}, {"firstName":"Peter", "value": 22}, {"firstName":"Anna", "value": 60} ]; We are required to write a JavaScript function that takes in one such array and combines the value property of all those objects that have similar value for the firstName property. Therefore, for the above array, the output should look like — const output = [ ...

Read More

JavaScript JSON Arrays

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 339 Views

JSON arrays are ordered lists of values enclosed in square brackets. In JavaScript, you can access and manipulate JSON arrays just like regular JavaScript arrays. Syntax { "arrayName": ["value1", "value2", "value3"] } Basic JSON Array Structure Here's how a JSON object with an array property looks: JSON Array Example let obj = { ...

Read More

Circle coordinates to array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 527 Views

In JavaScript, you can generate circle coordinates by using trigonometric functions to calculate points around a circle's circumference. This is useful for animations, graphics, and positioning elements in circular patterns. How Circle Coordinates Work A circle's coordinates are calculated using the parametric equations: X coordinate: centerX + radius × cos(angle) Y coordinate: centerY + radius × sin(angle) By dividing the circle into equal steps and calculating coordinates at each angle, we can create an array of points around the circle. Example ...

Read More

Reverse a number in JavaScript

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

Our aim is to write a JavaScript function that takes in a number and returns its reversed number. For example, reverse of 678 is: 876 There are multiple approaches to reverse a number in JavaScript. Let's explore the most common methods. Method 1: Using String Conversion The most straightforward approach converts the number to a string, reverses it, and converts back to a number: const num = 124323; const reverse = (num) => parseInt(String(num) .split("") .reverse() .join(""), 10); console.log(reverse(num)); ...

Read More

Update array of objects with JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 498 Views

In JavaScript, you can update an array of objects by modifying existing objects or adding new ones. This is commonly done using methods like push(), splice(), or array methods like map() and find(). Let's say we have the following array of objects: var studentDetails = [ { firstName: "John", listOfSubject: ['MySQL', 'MongoDB']}, {firstName: "David", listOfSubject: ['Java', 'C']} ]; console.log("Initial array:", studentDetails); Initial array: [ { firstName: 'John', listOfSubject: [ 'MySQL', 'MongoDB' ] }, { firstName: 'David', listOfSubject: [ 'Java', 'C' ] } ] ...

Read More

Squared sum of n odd numbers - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 340 Views

We are required to write a JavaScript function that takes in a Number, say n, and finds the sum of the square of first n odd natural Numbers. For example, if the input number is 3, we need to find the first 3 odd numbers (1, 3, 5) and calculate the sum of their squares: 1² + 3² + 5² = 1 + 9 + 25 = 35 Understanding the Pattern The first n odd natural numbers follow the pattern: 1, 3, 5, 7, 9... The formula for the i-th odd number is (2 * i) - 1. Example const num = 3; const squaredSum = num => { let sum = 0; for(let i = 1; i

Read More
Showing 4001–4010 of 8,392 articles
« Prev 1 399 400 401 402 403 840 Next »
Advertisements