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 429 of 840
How to create a random number between a range JavaScript
Our job is to create a function, say createRandom, that takes in two arguments and returns a pseudorandom number between the range (max exclusive). Syntax const createRandom = (min, max) => { const diff = max - min; const random = Math.random(); return Math.floor((random * diff) + min); } Example const min = 3; const max = 9; const createRandom = (min, max) => { const diff = max - min; const random ...
Read MoreVerification if a number is Palindrome in JavaScript
A palindrome number reads the same forwards and backwards. In JavaScript, we can check if a number is palindrome without converting it to a string by mathematically extracting and comparing digits. Palindrome numbers are those numbers which read the same from both backward and forward. For example: 121 343 12321 Algorithm Approach The algorithm works by: Finding a factor to extract the first digit Comparing first and last digits Removing both digits and repeating until all digits are checked Example const isPalindrome = (num) => { ...
Read MoreCheck if value is empty in JavaScript
In JavaScript, checking if a value is empty is essential for form validation and data processing. You can check for empty strings, null values, and undefined variables using various approaches. Basic Empty Check The most common approach is to check for empty strings and null values: Check Empty Value USERNAME: ...
Read MoreExpressing numbers in expanded form - JavaScript
Suppose we are given a number 124 and are required to write a function that takes this number as input and returns its expanded form as a string. The expanded form of 124 is − '100+20+4' How It Works The algorithm converts each digit to its place value by multiplying it with the appropriate power of 10, then joins non-zero values with '+' signs. Example Following is the code − const num = 125; const expandedForm = num => { const numStr = String(num); ...
Read MoreHow to convert MySQL DATETIME value to JSON format in JavaScript?
To convert MySQL DATETIME values to JSON format in JavaScript, you can parse the datetime string into a Date object and then use JSON.stringify() to convert it to JSON. This is useful when working with MySQL data in web applications. Understanding MySQL DATETIME Format MySQL DATETIME format is typically YYYY-MM-DD HH:MM:SS. JavaScript's Date constructor can parse various date formats, making conversion straightforward. Method 1: Converting to JSON Object with Individual Components This approach extracts individual date components and creates a structured JSON object: // Simulate MySQL DATETIME string var mySQLDateTime = new Date("2019-09-06 ...
Read MoreGet number from user input and display in console with JavaScript
Getting user input numbers in JavaScript involves capturing values from HTML input elements and converting them to numeric types for processing. This tutorial shows how to get a number from user input and display it in the console. HTML Structure First, create the HTML form with an input field and button: Get Number Input Number Input Example ...
Read MoreExplain equality of objects in JavaScript.
In JavaScript, primitives like strings, numbers, and booleans are compared by their values, while objects are compared by their reference. Reference comparison checks whether two or more objects point to the same location in memory, not whether they have the same content. Reference vs Value Comparison When you compare objects with == or ===, JavaScript checks if they reference the same object in memory, not if their properties are identical. Object Equality Object ...
Read MoreFinding out the Harshad number JavaScript
Harshad numbers are those numbers which are exactly divisible by the sum of their digits. Like the number 126, it is completely divisible by 1+2+6 = 9. All single digit numbers are harshad numbers. Harshad numbers often exist in consecutive clusters like [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [110, 111, 112], [1010, 1011, 1012]. Our job is to write a function that takes in ...
Read MoreSum of even numbers up to using recursive function in JavaScript
We have to write a recursive function that takes in a number n and returns the sum of all even numbers up to n. A recursive function calls itself with modified parameters until it reaches a base case. For summing even numbers, we'll start from the largest even number ≤ n and work our way down. How It Works The algorithm follows these steps: If the input number is odd, we adjust it to the nearest even number below it Add the current even number to the sum and recursively call with the next smaller ...
Read MoreRemove same values from array containing multiple values JavaScript
In JavaScript, arrays often contain duplicate values that need to be removed. The most efficient modern approach is using Set with the spread operator to create a new array with unique values only. Example Array with Duplicates Let's start with an array containing duplicate student names: const listOfStudentName = ['John', 'Mike', 'John', 'Bob', 'Mike', 'Sam', 'Bob', 'John']; console.log("Original array:", listOfStudentName); Original array: [ 'John', 'Mike', 'John', 'Bob', 'Mike', 'Sam', 'Bob', 'John' ] Using Set with Spread Operator (Recommended) The Set object automatically removes duplicates, and the spread operator converts it ...
Read More