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 160 of 589
How to remove li elements on button click in JavaScript?
In JavaScript, you can remove list items dynamically by attaching event listeners to buttons within each element. This tutorial shows how to remove specific list items when their corresponding "Remove" buttons are clicked. HTML Structure First, let's look at the basic HTML structure for our unordered list: JavaScript Remove MySQL Remove MongoDB Remove Java Remove Each list item contains a subject name and a "Remove" button. When clicked, the button will remove its parent ...
Read MoreBeginning and end pairs in array - JavaScript
We are required to write a JavaScript function that takes in an array of Number / String literals and returns another array of arrays. With each subarray containing exactly two elements, the nth element from start and nth from last. For example, if we have an array: const arr = [1, 2, 3, 4, 5, 6]; Then the output should be: const output = [[1, 6], [2, 5], [3, 4]]; How It Works The algorithm pairs elements from both ends of the array moving inward: ...
Read MoreJavaScript creating an array from JSON data?
To create an array from JSON data in JavaScript, you can use the map() method to extract specific values from each object. This is particularly useful when working with API responses or complex data structures. Basic JSON Data Structure Let's start with a simple JSON array containing student information: const studentDetails = [ { name: "John" }, { name: "David" }, ...
Read MoreRandom name generator function in JavaScript
We are required to write a JavaScript function that takes in a number n and returns a random string of length n containing no other than the 26 English lowercase alphabets. Example Let us write the code for this function: const num = 8; const randomNameGenerator = num => { let res = ''; for(let i = 0; i < num; i++){ const random = Math.floor(Math.random() * 26); res += String.fromCharCode(97 + ...
Read MoreFilter array of objects by a specific property in JavaScript?
Filtering arrays of objects by specific properties is a common task in JavaScript. While the article shows using map() with a ternary operator for comparison, the filter() method is typically more appropriate for filtering operations. Basic Array Filtering with filter() The filter() method creates a new array with elements that pass a test condition: let customers = [ {firstName: 'John', amount: 100}, {firstName: 'David', amount: 50}, {firstName: 'Bob', amount: 80}, {firstName: 'Alice', amount: 120} ]; // Filter customers with ...
Read MoreNearest Prime to a number - JavaScript
We are required to write a JavaScript function that takes in a number and returns the first prime number that appears after n. For example: If the number is 24, then the output should be 29. Understanding Prime Numbers A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. Examples include 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, etc. Implementation We'll create two functions: one to check if a number is prime, and another to find the nearest prime after a given ...
Read MoreAny way to solve this without concatenating these two arrays to get objects with higher value?
To find objects with higher property values from two arrays without concatenation, use the reduce() method on both arrays individually. This approach compares objects by a specific property and keeps only those with the highest values. Problem Setup Consider two arrays containing student objects with names and marks from different sections: var sectionAStudentDetails = [ {studentName: 'John', studentMarks: 78}, {studentName: 'David', studentMarks: 65}, {studentName: 'Bob', studentMarks: 98} ]; let sectionBStudentDetails = [ {studentName: 'John', studentMarks: 67}, ...
Read MoreRemove '0','undefined' and empty values from an array in JavaScript
To remove '0', 'undefined' and empty values from an array in JavaScript, you can use several methods. The most common approaches are using splice() with a loop or the filter() method. Method 1: Using splice() with for loop This method modifies the original array by removing falsy values in place: var allValues = [10, false, 100, 150, '', undefined, 450, null, 0]; console.log("Original Array:"); console.log(allValues); for (var index = 0; index < allValues.length; index++) { if (!allValues[index]) { allValues.splice(index, 1); ...
Read MoreReturning poker pair cards - JavaScript
We are required to write a function that takes in an array of exactly five elements representing the five cards of a poker player drawn randomly. If the five cards contain at least one pair, our function should return the card number of the highest pair (trivial if there only exists a single pair). Else our function should return false. For example: If the array is − const arr = ['A', 'Q', '3', 'A', 'Q']; Then our function should return − 'A' (as 'A' > 'Q' in card games) Card Ranking System ...
Read MoreUn-nesting array of object in JavaScript?
To un-nest an array of objects in JavaScript, you can use the map() method to restructure nested data into a flatter format. This is useful when you have deeply nested objects and want to extract specific properties to create a simpler structure. Problem: Nested Object Structure Let's say you have an array of student objects where subject details are nested inside each student: const studentDetails = [ { "studentId": 101, "studentName": "John", ...
Read More