AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 474 of 840

Recursive multiplication in array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 437 Views

We need to write a JavaScript function that takes a nested array containing numbers, false values, zeros, and strings, then returns the product of all valid numbers. The function should ignore zeros and falsy values while recursively processing nested arrays. Problem Statement Given a nested array with mixed data types, we want to multiply only the truthy numeric values while ignoring zeros, null, undefined, false, and strings. Solution We'll use recursion to traverse nested arrays and multiply only valid numbers: const arr = [1, 5, 2, null, [ 2, 5, ...

Read More

How to embed JavaScript in HTML file?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 612 Views

There are three main ways to embed JavaScript in an HTML file: inline scripts, internal scripts, and external scripts. Each method serves different purposes and has its own advantages. Method 1: Inline JavaScript You can add JavaScript directly to HTML elements using event attributes: Inline JavaScript Inline JavaScript Example Click Me Method 2: Internal JavaScript Use the tag within the HTML document to ...

Read More

Find all subarrays with sum equal to number? JavaScript (Sliding Window Algorithm)

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 713 Views

We are given an array of numbers and a target sum. Our job is to write a function that returns an array of all the subarrays which add up to the target number using the sliding window algorithm. For example: const arr = [23, 5, 1, 34, 12, 67, 9, 31, 6, 7, 27]; const sum = 40; Should find these subarrays that sum to 40: [ [ 5, 1, 34 ], [ 9, 31 ], [ 6, 7, 27 ] ] The Sliding Window Algorithm The sliding window algorithm ...

Read More

HTML form action and onsubmit validations with JavaScript?

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

HTML form validation is essential for ensuring data integrity before submission. The onsubmit event allows you to validate form data and prevent submission if validation fails. How Form Validation Works The onsubmit event handler must return true to allow submission or false to prevent it. When validation fails, the form submission is blocked. Basic Validation Example Form Validation Example Enter "gmail" to proceed: ...

Read More

Find Second most frequent character in array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 659 Views

We are required to write a JavaScript function that takes in an array and returns the element that appears for the second most number of times. Let's say the following is our array: const arr = [1, 34, 4, 3, 2, 1, 4, 6, 4, 6, 5, 3, 6, 6]; The most frequent element is 6 (appears 4 times), but we want the second most frequent element, which is 4 (appears 3 times). Example const arr = [1, 34, 4, 3, 2, 1, 4, 6, 4, 6, 5, 3, 6, 6]; ...

Read More

Explain Enumerated Types in JavaScript.

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 204 Views

JavaScript doesn't support enums natively like other programming languages. However, you can implement enumerated types using objects to create named constants for better code readability and maintainability. Basic Enum Implementation The simplest way to create an enum is using a plain object with numeric values: JavaScript Enums Enumerated Types in JavaScript Show Color Values ...

Read More

Form Object from string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 311 Views

We are required to write a function that takes in a string as the first and the only argument and constructs an object with its keys based on the unique characters of the string and value of each key being defaulted to 0. For example − // if the input string is: const str = 'hello world!'; // then the output should be: const obj = {"h": 0, "e": 0, "l": 0, "o": 0, " ": 0, "w": 0, "r": 0, "d": 0, "!": 0}; So, let's write the code for this function − ...

Read More

Divide a string into n equal parts - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 697 Views

We are required to write a JavaScript function that takes in a string and a number n (such that n exactly divides the length of string). We need to return an array of string of length n containing n equal parts of the string. Let's write the code for this function − Example const str = 'this is a sample string'; const num = 5; const divideEqual = (str, num) => { const len = str.length / num; const creds = str.split("").reduce((acc, val) => { ...

Read More

JavaScript program to access browsing history

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 877 Views

In this article, we will learn to access browsing history using JavaScript. In web development, accessing a user's browsing history can improve user experience and provide personalized content. However, due to privacy concerns and security reasons, modern web browsers limit the amount of browsing history accessible via JavaScript. What is Browsing History? Browsing history refers to the list of web pages that a user has visited over a while. Browsers typically store this data locally, allowing users to navigate back and forth through the pages they've visited. The window.history Object In JavaScript, the window.history object allows ...

Read More

Sort array based on presence of fields in objects JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 319 Views

In JavaScript, you can sort arrays of objects based on the presence of specific fields using a custom comparator function. This technique is useful when you need to prioritize objects with complete information. Problem Statement Let's say we have an array of people objects where some have both firstName and lastName, some have only one of these properties, and others have neither: const people = [{ firstName: 'Ram', id: 301 }, { firstName: 'Shyam', lastName: 'Singh', ...

Read More
Showing 4731–4740 of 8,392 articles
« Prev 1 472 473 474 475 476 840 Next »
Advertisements