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
Articles by AmitDiwan
Page 487 of 840
Converting days into years months and weeks - JavaScript
We are required to write a JavaScript function that takes in a number (representing the number of days) and returns an object with four properties: years, months, weeks, days The properties should have proper values that can be made from the total number of days. We assume a standard year has 365 days and each month has 30 days for simplicity. Problem Example If the input is 738 days, the output should be: { years: 2, months: 0, weeks: 1, days: 1 } ...
Read MoreCan we check if a property is in an object with JavaScript?
JavaScript provides multiple ways to check if a property exists in an object. The most common approaches are using the in operator, hasOwnProperty() method, and Object.hasOwn() method. Using hasOwnProperty() Method The hasOwnProperty() method checks if the object has a specific property as its own (not inherited from the prototype chain). Property Check body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } ...
Read MoreReverse all the words of sentence JavaScript
We are required to write a JavaScript function that takes in a string and returns a new string which has all the words reversed from the original string. For example − If the original string is − "Hello World how is it outside" Then the output should be − "olleH dlroW woh si ti edistuo" Now, let's write the code for this function − Example const str = 'Hello World how is it outside'; const reverseSentence = str => { const arr = ...
Read MoreHow to run functions iteratively with async await in JavaScript?
Running functions iteratively with async/await allows you to execute asynchronous operations in sequence within loops. This is useful when you need to process items one by one rather than concurrently. Basic Syntax async function iterativeFunction() { for (let i = 0; i < count; i++) { await asyncOperation(); } } Example: Sequential Function Calls async function test(i) { while (i { setTimeout(() => { ...
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 MoreCheck Disarium number - JavaScript
A Disarium number is a number where the sum of its digits raised to their respective positions equals the original number. Definition For a number with digits xy...z, it's a Disarium number if: xy...z = x^1 + y^2 + ... + z^n Where n is the total number of digits in the number. Example Let's check if 175 is a Disarium number: 175 = 1^1 + 7^2 + 5^3 = 1 + 49 + 125 = 175 Since the sum equals the original number, 175 is a Disarium ...
Read MoreHow to set div position relative to another div in JavaScript?
Setting div position relative to another div in JavaScript can be achieved through several methods including CSS positioning, Flexbox, and dynamic JavaScript positioning. Here are the most effective approaches. Method 1: Using CSS Positioning The most common approach uses CSS position: relative and position: absolute to position elements relative to each other. Relative Positioning .parent-div { ...
Read MoreHow to split string when the value changes in JavaScript?
To split a string when the character value changes in JavaScript, you can use the match() method with a regular expression that captures consecutive identical characters. Syntax string.match(/(.)\1*/g) How the Regular Expression Works The pattern /(.)\1*/g breaks down as: (.) - Captures any single character \1 - Matches the same character as captured in group 1 * - Matches zero or more of the preceding element g - Global flag to find all matches Example var originalString = "JJJJOHHHHNNNSSSMMMIIITTTTHHH"; var regularExpression = /(.)\1*/g; console.log("The original string = ...
Read MoreProgram to pick out duplicate only once - JavaScript
We have an array of literals that contains some duplicate values appearing for many times like this: const arr = [1, 4, 3, 3, 1, 3, 2, 4, 2, 1, 4, 4]; We are required to write a JavaScript function that takes in this array and pick out all the duplicate entries from the original array and only once. So, for the above array, the output should be: [1, 4, 3, 2] Method 1: Using indexOf and lastIndexOf This approach checks if an element's first and last occurrence positions are ...
Read MoreHow to convert a node list to an array in JavaScript?
A NodeList is returned by DOM methods like querySelectorAll() and getElementsByClassName(). While it's array-like, it doesn't have all array methods. Converting it to a true array gives you access to methods like map(), filter(), and forEach(). Using Array.from() (Recommended) The Array.from() method is the modern and most readable way to convert a NodeList to an array: Convert NodeList to Array body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; padding: ...
Read More