AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 458 of 840

JavaScript recursive loop to sum all integers from nested array?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 482 Views

JavaScript recursive functions can process nested arrays by calling themselves repeatedly to handle arrays within arrays. This technique is essential for working with complex data structures of unknown depth. Example: Basic Recursive Sum function sumOfTotalArray(numberArray) { var total = 0; for (var index = 0; index < numberArray.length; index++) { if (numberArray[index] instanceof Array) { total = total + sumOfTotalArray(numberArray[index]); } ...

Read More

Sorting an associative array in ascending order - JavaScript

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

In JavaScript, you can sort an array of objects (associative array) in ascending order using the sort() method with a custom comparison function. Suppose we have an array of objects like this: const people = [ {"id":1, "name":"Andrew", "age":30, "gender":"m", "category":"G"}, {"id":2, "name":"Brandon", "age":25, "gender":"m", "category":"G"}, {"id":3, "name":"Christine", "age":20, "gender":"m", "category":"G"}, {"id":4, "name":"Elena", "age":29, "gender":"W", "category":"M"} ]; We need to sort this array by the age property in ascending order. The expected output should be: [ ...

Read More

Undeclared vs Undefined? In JavaScript

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

In JavaScript, undeclared and undefined are two different concepts that developers often confuse. Understanding the distinction is crucial for debugging and writing reliable code. Key Differences Undeclared occurs when you try to access a variable that hasn't been declared using var, let, or const. This results in a ReferenceError. Undefined occurs when a variable has been declared but hasn't been assigned a value. The variable exists but contains the special value undefined. Example: Undefined Variable Undefined Variable ...

Read More

How to generate array of n equidistant points along a line segment of length x with JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 407 Views

To generate an array of n equidistant points along a line segment of length x, we divide the segment into equal intervals and calculate each point's position based on its proportional distance. Syntax for (let i = 0; i < n; i++) { let ratio = (i + 1) / (n + 1); let point = ratio * segmentLength; // Add point to array } Example function generateEquidistantPoints(n, segmentLength) { const points = []; ...

Read More

Finding average word length of sentences - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 531 Views

We are required to write a JavaScript function that takes in a string of words joined by whitespaces. The function should calculate and return the average length of all the words present in the string rounded to two decimal places. Understanding the Problem To find the average word length, we need to: Split the string into individual words Calculate the total length of all words (excluding spaces) Divide by the number of words Round the result to two decimal places Example Following ...

Read More

innerHTML vs innerText in JavaScript.

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

The innerHTML and innerText properties are two different ways to access and manipulate the content of HTML elements in JavaScript. innerHTML - Returns or sets the HTML markup inside an element, including all tags, formatting, and spacing. It preserves the complete HTML structure. innerText - Returns or sets only the visible text content, stripping out all HTML tags and normalizing whitespace. Key Differences Property HTML Tags Whitespace Hidden Elements innerHTML Preserved Preserved Included innerText Removed Normalized Excluded Example ...

Read More

JavaScript filter an array of strings, matching case insensitive substring?

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

To filter an array of strings with case-insensitive matching, use JavaScript's filter() method combined with toLowerCase() and indexOf(). Setting Up the Data Let's start with an array of student objects containing names with different cases: let studentDetails = [ {studentName: "John Smith"}, {studentName: "john smith"}, {studentName: "Carol Taylor"}, {studentName: "JOHN TAYLOR"}, {studentName: "alice johnson"} ]; console.log("Original array:", studentDetails); Original array: [ { studentName: 'John Smith' }, { studentName: ...

Read More

How to read a cookie using JavaScript?

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

In this article, we will learn how to read cookies in JavaScript and use them for features like user tracking, user preferences, and personalized experiences. Cookies are small pieces of data stored in a user's web browser. In JavaScript, we can read cookies using the document's cookie property and extract specific values using string manipulation techniques. Understanding document.cookie The document.cookie property returns all cookies as a single semicolon-separated string. Each cookie appears as name=value pairs. Example 1: Reading All Cookies This example demonstrates how to access all cookies stored for the current domain: ...

Read More

Check whether Enter key is pressed or not and display the result in console with JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 383 Views

In JavaScript, you can detect when the Enter key is pressed using the onkeypress event handler. This is useful for form submissions, search functionality, or triggering actions when users press Enter. Key Code for Enter Key The Enter key has a key code of 13. We can check this using the keyCode property of the event object. Basic Implementation First, create an input field with an onkeypress event handler: Then, create a function to detect the Enter key: function checkEnterKey(event) { if (event.keyCode == 13) ...

Read More

Shift certain array elements to front of array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 275 Views

We are required to write a JavaScript function that takes in an array of numbers. The function should bring all the 3-digit integers to the front of the array. Let's say the following is our array of numbers: const numList = [1, 324, 34, 3434, 304, 2929, 23, 444]; Understanding 3-Digit Numbers A 3-digit number is any integer between 100 and 999 (inclusive). We can check this using a simple condition: const isThreeDigit = num => num > 99 && num < 1000; console.log(isThreeDigit(324)); // true console.log(isThreeDigit(34)); // ...

Read More
Showing 4571–4580 of 8,392 articles
« Prev 1 456 457 458 459 460 840 Next »
Advertisements