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 19 of 589
Enter values with prompt and evaluate on the basis of conditions in JavaScript?
JavaScript's prompt() function allows you to collect user input and evaluate it using conditional statements. This is useful for creating interactive web applications that respond based on user-provided values. Basic Syntax To get user input and convert it to a number: var value = parseInt(prompt("Enter a value")); Example: Conditional Evaluation Based on User Input Here's a complete example that prompts for two values and evaluates their product: Prompt and Evaluate ...
Read MoreFiltering array within a limit JavaScript
We are required to write a JavaScript function that takes in an array of numbers as the first argument and an upper limit and lower limit number as second and third argument respectively. Our function should filter the array and return a new array that contains elements between the range specified by the upper and lower limit (including the limits). Syntax const filterByLimits = (arr, upper, lower) => { return arr.filter(element => element >= lower && element { let res = []; res = arr.filter(el => { return el >= lower && el { return arr.filter(element => element >= lower && element = lower && element
Read MoreRemove/ filter duplicate records from array - JavaScript?
JavaScript arrays often contain duplicate records that need to be removed. There are several effective methods to filter duplicates, depending on whether you're working with primitive values or objects. Sample Data with Duplicates Let's start with an array of nationality objects containing duplicate values: var objectOfNationality = [ { nationality: "Indian" }, { nationality: "American" }, { nationality: "Emirati" }, { nationality: "Indian" }, { nationality: "American" } ]; console.log("Original array:", objectOfNationality); ...
Read MoreConstruct objects from joining two strings JavaScript
We are required to write a JavaScript function that takes in two comma separated strings. The first string is the key string and the second string is the value string, the number of elements (commas) in both the strings will always be the same. Our function should construct an object based on the key and value strings and map the corresponding values to the keys. Example const str1= '[atty_hourly_rate], [paralegal_hourly_rate], [advanced_deposit]'; const str2 = '250, 150, 500'; const mapStrings = (str1 = '', str2 = '') => { const keys = ...
Read MoreNumber prime test in JavaScript by creating a custom function?
A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. In JavaScript, we can create a custom function to check if a number is prime. Basic Prime Number Function Here's a simple approach to check if a number is prime: function checkNumberIsPrime(number) { if (number
Read MoreFinding the longest valid parentheses JavaScript
Given a string containing just the characters '(' and ')', we find the length of the longest valid (well-formed) parentheses substring. A set of parentheses qualifies to be a well-formed parentheses, if and only if, for each opening parentheses, it contains a closing parentheses. For example: '(())()' is a well-formed parentheses '())' is not a well-formed parentheses '()()()' is a well-formed parentheses Algorithm Approach The solution uses a stack-based approach to track invalid parentheses positions. We push indices of unmatched characters onto the stack, then calculate the longest valid substring between these invalid ...
Read MoreComparing adjacent element and swap - JavaScript?
Bubble sort is a simple sorting algorithm that compares adjacent elements and swaps them if they are in the wrong order. This process repeats until the array is sorted. How Bubble Sort Works The algorithm repeatedly steps through the array, compares adjacent elements, and swaps them if they are in the wrong order. Each pass "bubbles" the largest element to its correct position. Bubble Sort Process Initial: 10 30 5 ...
Read MoreChecking a Checkbox with JavaScript
In JavaScript, you can programmatically check a checkbox by setting its checked property to true. This is useful for form validation, user interactions, or initializing form states. HTML Structure First, let's look at a basic checkbox structure: John David Using the checked Property The checked property is a boolean that determines whether a checkbox is selected. Set it to true to check the checkbox, or false to uncheck it. Checkbox Example ...
Read MoreCompare two arrays of single characters and return the difference? JavaScript
We are required to compare, and get the difference, between two arrays containing single character strings appearing multiple times in each array. Example of two such arrays are: const arr1 = ['A', 'C', 'A', 'D']; const arr2 = ['F', 'A', 'T', 'T']; We will check each character at the same position and return only the parts who are different. Approach The algorithm compares elements at the same index position. When characters differ, both are added to the result array. Any remaining elements from the longer array are also included. Example ...
Read MoreDifference between two times using Dayjs JavaScript library?
Day.js is a lightweight JavaScript library for date manipulation. The diff() method calculates the difference between two time instances and returns the result in the specified unit. Syntax dayjs().diff(date, unit) Parameters date - The date to compare with unit - The unit of measurement: 'milliseconds', 'seconds', 'minutes', 'hours', 'days', 'weeks', 'months', 'years' Example: Basic Time Difference Day.js Time Difference ...
Read More