Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Web Development Articles
Page 483 of 801
Return 5 random numbers in range, first number cannot be zero - JavaScript
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 MoreHow to transform object of objects to object of array of objects with JavaScript?
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 MoreValidating email and password - JavaScript
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 MoreIs an empty iframe src valid in JavaScript?
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 MoreRepeat even number inside the same array - JavaScript
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 MoreLooping in JavaScript to count non-null and non-empty values
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 MoreRemove and add new HTML Tags with JavaScript?
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 MoreHow can I instantiate a dictionary in JavaScript where all keys map to the same value?
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 MoreHow would I add a newline in an Unordered List (UL) from JavaScript?
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 MoreGetting an HTML H1 value to JavaScript variable?
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