Web Development Articles

Page 483 of 801

Return 5 random numbers in range, first number cannot be zero - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 198 Views

We are required to write a JavaScript function that generates an array of exactly five unique random numbers. The condition is that all the numbers have to be in the range [0, 9] and the first number cannot be 0. Example Following is the code − const fiveRandoms = () => { const arr = [] while (arr.length < 5) { const random = Math.floor(Math.random() * 10); if (arr.indexOf(random) > -1){ ...

Read More

How to transform object of objects to object of array of objects with JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 840 Views

To transform an object of objects into an object of array of objects, you can use Object.fromEntries() along with Object.entries() and map(). This transformation wraps each nested object in an array while preserving the original keys. Syntax Object.fromEntries( Object.entries(originalObject).map(([key, value]) => [key, [value]]) ) Example const studentDetails = { 'details1': {Name: "John", CountryName: "US"}, 'details2': {Name: "David", CountryName: "AUS"}, 'details3': {Name: "Bob", CountryName: "UK"}, }; const transformedObject = Object.fromEntries( Object.entries(studentDetails).map(([key, value]) => ...

Read More

Validating email and password - JavaScript

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

Suppose, we have this dummy array that contains the login info of two of many users of a social networking platform − const array = [{ email: 'usman@gmail.com', password: '123' }, { email: 'ali@gmail.com', password: '123' }]; We are required to write a JavaScript function that takes in an email string and a password string. The function should return a boolean based on the fact whether or not the user exists in the database. Example Following is the code − ...

Read More

Is an empty iframe src valid in JavaScript?

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

An empty iframe src can be handled in JavaScript using about:blank or by leaving the src attribute empty. The about:blank approach is more reliable and creates a valid, blank document. Valid Approaches for Empty iframe src There are several ways to create an empty iframe: Using about:blank (Recommended) The about:blank value creates a valid empty document that can be safely manipulated with JavaScript: Empty iframe Example ...

Read More

Repeat even number inside the same array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 154 Views

We are required to write a JavaScript function that should repeat the even number inside the same array. Therefore, for example given the following array − const arr = [1, 2, 5, 6, 8]; We should get the output − const output = [1, 2, 2, 5, 6, 6, 8, 8]; Example Following is the code − const arr = [1, 2, 5, 6, 8]; const repeatEvenNumbers = arr => { let end = arr.length - 1; for(let i = ...

Read More

Looping in JavaScript to count non-null and non-empty values

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

In JavaScript, counting non-null and non-empty values in an array is a common task. This article demonstrates how to use forEach() to iterate through an array and count valid values. Basic Array Example Let's start with an array containing subject names: let subjectNames = ['JavaScript', 'Angular', 'AngularJS', 'Java']; console.log("Original array:", subjectNames); Original array: [ 'JavaScript', 'Angular', 'AngularJS', 'Java' ] Using forEach() to Count Valid Values The forEach() method executes a function for each array element. Here's the syntax: yourArrayName.forEach(element => { // Your logic ...

Read More

Remove and add new HTML Tags with JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 804 Views

JavaScript provides several methods to dynamically remove and add HTML elements to the DOM. You can hide/show existing elements or completely remove/create new elements using vanilla JavaScript or jQuery. Method 1: Using jQuery hide() and show() The simplest approach is using jQuery's hide() and show() methods to toggle element visibility: Hide/Show Elements Test JavaScript This content can be hidden and shown. ...

Read More

How can I instantiate a dictionary in JavaScript where all keys map to the same value?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 324 Views

In JavaScript, you can create a dictionary (object) where all keys map to the same value using several approaches. This is useful when you need to initialize multiple properties with identical values. Method 1: Using a for...of Loop The most straightforward approach is to iterate through an array of keys and assign the same value to each: const keys = ['Name1', 'Name2', 'Name3']; const dictionary = {}; for (const key of keys) { dictionary[key] = 'John'; } console.log(dictionary); { Name1: 'John', Name2: 'John', Name3: 'John' } ...

Read More

How would I add a newline in an Unordered List (UL) from JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 402 Views

To add a new line (list item) to an Unordered List in JavaScript, use document.querySelector().append() or appendChild(). This allows you to dynamically add elements to an existing . Basic Syntax // Create a new list item let listItem = document.createElement('li'); listItem.textContent = 'Your content here'; // Append to the ul document.querySelector('ul').append(listItem); Complete Example Add Items to List Demo h1 { ...

Read More

Getting an HTML H1 value to JavaScript variable?

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

To get the value of an HTML H1 element into a JavaScript variable, you can use document.getElementById().innerHTML to access the text content inside the heading. Basic Syntax var headingText = document.getElementById('elementId').innerHTML; Example: Getting H1 Content Let's say we have the following H1 heading with an id: This is the demo program of JavaScript Here's how to get the H1 value using JavaScript: Getting H1 Value ...

Read More
Showing 4821–4830 of 8,010 articles
« Prev 1 481 482 483 484 485 801 Next »
Advertisements