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
Object Oriented Programming Articles
Page 167 of 589
Assigning values to a computed property in JavaScript?
In JavaScript, computed properties allow you to create object properties with dynamic names. This article shows how to extract data from an array of objects and assign values to computed properties using array methods. Sample Data Let's start with an array of customer details objects: const customerDetails = [ {customerFirstName: "David"}, {customerLastName: "Miller"}, {customerCountryName: "US"}, {customerAge: "29"}, {isMarried: false}, {customerCollegeName: null} ]; console.log("Original data:", customerDetails); Original data: [ ...
Read MoreChecking an array for palindromes - JavaScript
We are required to write a JavaScript function that takes in an array of String / Number literals and returns a subarray of all the elements that were palindrome in the original array. A palindrome is a word, number, or sequence that reads the same forward and backward. For example, "dad", "racecar", and 12321 are palindromes. Problem Statement If the input array is: const arr = ['carecar', 1344, 12321, 'did', 'cannot']; Then the output should be: const output = [12321, 'did']; Approach We will create a helper function ...
Read MoreGenerate random characters and numbers in JavaScript?
Generating random characters and numbers is a common requirement in JavaScript applications. You can create random strings by combining characters from a predefined set using Math.random(). Setting Up Character Pool First, define a string containing all possible characters you want to include: var storedCharacters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; Basic Random String Generator Here's a function that generates random strings of any specified length: function generateRandomString(size) { var generatedOutput = ''; var storedCharacters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; var totalCharacterSize = storedCharacters.length; ...
Read MoreSorting objects by numeric values - JavaScript
Suppose we have an object like this: const obj = { key1: 56, key2: 67, key3: 23, key4: 11, key5: 88 }; We are required to write a JavaScript function that takes in this object and returns a sorted array like this: const arr = [11, 23, 56, 67, 88]; Here, we sorted the object values and placed them in an array. Method 1: Using Object.keys() and map() This approach extracts the keys, maps them ...
Read MoreTrigger a button click and generate alert on form submission in JavaScript
In JavaScript, you can trigger a button click event and generate an alert on form submission using event handlers. This tutorial demonstrates how to attach a click event listener to a button and display an alert when the form is submitted. HTML Structure First, let's create a basic HTML form with a button: Submit JavaScript Event Handler Using jQuery, you can attach a click event handler to the button based on its ID: $("#submitForm").click(function() { alert("The Form has been Submitted."); }); Complete Example Here's ...
Read MoreStore count of digits in order using JavaScript
When working with strings containing digits, you often need to count how many times each digit appears. This is a common programming task that can be solved efficiently using JavaScript objects. Suppose we have a string with digits like this: const str = '11222233344444445666'; We need to write a JavaScript function that takes this string and returns an object representing the count of each digit in the string. For this string, the expected output should be: { "1": 2, "2": 4, "3": 3, "4": ...
Read MoreHow to find the second largest element in a user-input JavaScript array?
Finding the second largest element in a JavaScript array requires comparing elements and tracking the two highest values. Here are several approaches to accomplish this task. Method 1: Using Single Pass Iteration This approach iterates through the array once, maintaining variables for the largest and second largest elements: var numbers = [10, 50, 80, 60, 89]; var firstLargerNumber = Number.MIN_SAFE_INTEGER; var secondlargerNumber = firstLargerNumber; for(var tempNumber of numbers){ if(tempNumber > firstLargerNumber){ secondlargerNumber = firstLargerNumber; firstLargerNumber ...
Read MoreGenerating n random numbers between a range - JavaScript
We are required to write a JavaScript function that takes in a number, say n, and an array of two numbers that represents a range. The function should return an array of n random elements all lying between the range provided by the second argument. Understanding the Problem The challenge is to generate unique random numbers within a specified range. We need two helper functions: one to generate a single random number between two values, and another to collect n unique random numbers. Example Implementation Following is the code − const num = 10; ...
Read MoreToggle hide class only on selected div with JavaScript?
To toggle hide class only on selected div, you need to set event on click button. Let's say you need to hide a specific div on the click of + sign. To get font + or - icon, you need to link font-awesome: HTML Structure The HTML contains multiple customer sections, each with a toggle button that can show/hide specific content: Toggle Hide Class Example ...
Read MoreFind the number of times a value of an object property occurs in an array with JavaScript?
When working with arrays of objects, you often need to count how many times a specific property value appears. JavaScript's reduce() method combined with Map provides an efficient solution for this task. Using reduce() with Map The reduce() method processes each array element to build a frequency count. We use a Map to store the counts because it maintains insertion order and provides efficient lookups. const subjectDetails = [ { subjectId: '101', subjectName: 'JavaScript' ...
Read More