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 291 of 840
Encoding decimal to factorial and back in JavaScript
The factorial number system uses factorials as bases instead of powers. Each digit position represents a factorial base, where the rightmost digit is base 0!, the next is base 1!, then base 2!, and so on. How Factorial Encoding Works In factorial representation, the nth digit from the right can range from 0 to n. For example: Position 0 (rightmost): 0 to 0, multiplied by 0! = 1 Position 1: 0 to 1, multiplied by 1! = 1 Position 2: 0 to 2, multiplied by 2! = ...
Read MoreHow to change an element's class with JavaScript?
In JavaScript, you can change an element's class using several methods. The most common approaches are using the className property for complete replacement and classList methods for more flexible manipulation. How to change the element's class using className property How to toggle between classes using className and classList Using className Property The className property allows you to get or set the entire class attribute of an element. When you assign a new value, it replaces all existing classes. Example ...
Read MoreHow to find a nearest higher number from a specific set of numbers: JavaScript ?
We have a set of numbers and our requirement is to find the same or the nearest higher number key to a specific number provided as the input to the function. The set of numbers is defined as: const numbers = { A: 107, B: 112, C: 117, D: 127, E: 132, F: 140, G: 117, H: 127, I: 132, J: 132, K: 140, ...
Read MoreReturning the highest value from an array in JavaScript
We are required to write a JavaScript function that takes in an array of Numbers. Our function should iterate through the array and pick the greatest (largest) element from the array and return that element. Using a Custom Loop Function Here's a manual approach that iterates through the array to find the maximum value: const arr = [5, 3, 20, 15, 7]; const findGreatest = (arr = []) => { let greatest = -Infinity; if (!arr?.length) { return null; ...
Read MoreDifference between numbers and string numbers present in an array in JavaScript
In JavaScript, arrays can contain both numbers and string representations of numbers. This article demonstrates how to differentiate between these types and perform different operations based on their type. Problem We need to create a JavaScript function that takes a mixed array of numbers and string representations of integers. The function should add up all numeric values and subtract the sum of string numeric values from this total. Approach We'll use typeof to identify data types and the unary plus operator (+) to convert strings to numbers while checking if they're valid numeric strings. Example ...
Read MoreHow to create every combination possible for the contents of two arrays in JavaScript
Creating every possible combination from two arrays is known as a Cartesian product. This involves pairing each element from the first array with every element from the second array. Problem Setup Given two arrays of literals: const arr1 = ["A", "B", "C"]; const arr2 = ["1", "2", "3"]; console.log("Array 1:", arr1); console.log("Array 2:", arr2); Array 1: [ 'A', 'B', 'C' ] Array 2: [ '1', '2', '3' ] We need to create all possible combinations by pairing each element from the first array with each element from the second array. ...
Read MoreSorting an array by price in JavaScript
Sorting an array of objects by price is a common requirement in JavaScript applications. This article demonstrates how to sort house data by price values stored as strings. Sample Data Let's start with an array of house objects where prices are stored as strings: const arr = [ { "h_id": "3", "city": "Dallas", "state": "TX", "zip": "75201", ...
Read MoreSeparating digits of a number in JavaScript
In JavaScript, separating digits of a number is a common programming task. This tutorial shows how to extract and display each digit of a number individually using different approaches. Problem Statement We need to create a program that takes a number as input and displays each digit separately. For example, if the input is 43354, the output should be: 4 3 3 5 4 Method 1: Using Mathematical Operations This approach uses the modulo operator (%) and integer division to extract digits from right to left: ...
Read MoreSort by index of an array in JavaScript
Sorting an array of objects by a specific property is a common task in JavaScript. This tutorial demonstrates how to sort objects by their index property and extract specific values. Problem Statement Suppose we have the following array of objects: const arr = [ { 'name': 'd', 'index': 3 }, { 'name': 'c', ...
Read MoreGet the total number of same objects in JavaScript
When working with arrays of objects in JavaScript, you often need to count how many objects share the same property values. This is useful for analyzing data like flight routes, user preferences, or categorizing items. Suppose we have an array of objects describing flight routes: const routes = [ { flyFrom: "CDG", flyTo: "DUB", return: 0, }, { ...
Read More