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
Front End Technology Articles
Page 200 of 652
Decimal count of a Number in JavaScript
Given there is a number with digits after decimal point and the task is to find the count of digits after the decimal point. Input Output Scenario Let's look into the input output scenario, where there is a floating point number having some digits after decimal point. Input = 45.36346323 Output = 8 As we can see in the above snippet there are 8 digits in the floating point number after the decimal point. To achieve this task, we'll explore three different methods using isInteger(), toString(), and split() methods. Method 1: Using ...
Read MoreConverting two arrays into a JSON object in JavaScript
Given two arrays and the task is to convert those two arrays into a JSON object. Where the first array elements are keys of the object and the second array elements are values of the object. Input-Output Scenario Let's look into the scenario of how two arrays will be converted into a JSON object. Consider two arrays are having some elements. Now, we need to convert these two arrays into a JSON object. Array1 = [1, 2, 3, 4]; Array2 = ['A', 'B', 'C', 'D']; Output = {"1":"A", "2":"B", "3":"C", "4":"D"} //JSON object ...
Read MoreUsing one array to help filter the other in JavaScript
When working with arrays of objects, you often need to filter one array based on values from another array. This is a common requirement when you have a list of items and want to keep only those that match specific criteria. Problem Statement Given an array of objects and an array of values, we need to filter the objects array to include only those objects whose property matches values in the second array. Sample Data Let's start with these arrays: const main = [ {name: "Karan", age: 34}, {name: "Aayush", ...
Read MoreConverting decimal to binary or hex based on a condition in JavaScript
Problem We need to write a JavaScript function that takes in a number n and converts it based on a condition: If a number is even, convert it to binary. If a number is odd, convert it to hex. Solution We can use JavaScript's toString() method with different radix values to perform the conversion. For binary conversion, we use radix 2, and for hexadecimal conversion, we use radix 16. Example Here's the implementation: const num = 1457; const conditionalConvert = (num = 1) ...
Read MoreRemoving property from a JSON object in JavaScript
A JSON (JavaScript Object Notation) object is a data structure surrounded by curly braces {}. JSON objects contain key-value pairs where keys must be strings and values can be numbers, strings, objects, booleans, arrays, or null. JavaScript provides several methods to remove properties from objects. Each method has different characteristics - some modify the original object, while others create new objects without the unwanted properties. Basic JSON Object Structure Here's the basic syntax for a JSON object: const jsonObject = { "name": "John", "age": 30, "city": ...
Read MoreBest way to find length of JSON object in JavaScript
In JavaScript, objects don't have a built-in length property like arrays. However, there are several reliable methods to find the number of properties in a JSON object. Input-Output Scenario Consider an object with some keys and values. We need to get the count of its properties: const MyObj = { Name: "Mike", Age: 34 }; console.log(Object.keys(MyObj).length); // Output: 2 Using Object.keys() (Recommended) The Object.keys() method returns an array of a given object's own enumerable property names. It returns the keys in the same order as they ...
Read MoreCounting smaller and greater in JavaScript
In JavaScript, we often need to count elements in an array that are greater than or smaller than a specific value. This is useful for data analysis, filtering, and statistical operations. Let's say we have an array of numbers and want to count how many elements are greater than and smaller than a given number n. const arr = [3, 5, 5, 2, 23, 4, 7, 8, 8, 9]; console.log("Array:", arr); Array: [3, 5, 5, 2, 23, 4, 7, 8, 8, 9] Using reduce() Method The most elegant approach uses the ...
Read MoreCutting off number at each digit to construct an array in JavaScript
We need to write a JavaScript function that takes a number and returns an array of strings, where each string represents the number cut off at each digit position. Problem Given a number like 246, we want to create an array containing: First digit: "2" First two digits: "24" All digits: "246" Example Here's the implementation: const num = 246; const cutOffEach = (num = 1) => { const str = String(num); const res = []; let temp = ''; ...
Read MoreIs element repeated more than n times in JavaScript
In JavaScript, you may need to check if any element appears more than a specified number of times in an array. This is useful for data validation, duplicate detection, and enforcing constraints on array contents. Problem Definition We need to write a function that takes two arguments: An Array of literals that may contain repeating elements A number representing the maximum allowed occurrences (limit) The function should return false if any element appears more than the limit, and true otherwise. Using reduce() and every() Methods The ...
Read MoreFinding two missing numbers that appears only once and twice respectively in JavaScript
We need to write a JavaScript function that finds two numbers in an array where all other numbers appear three times, except one number that appears twice and another that appears only once. Problem Statement Given an array where most numbers appear three times, find the two numbers that appear exactly twice and exactly once respectively. Example Let's solve this step by step: const arr = [1, 1, 1, 2, 2, 3]; const findMissing = (arr = []) => { let x = 0; // number appearing once ...
Read More